bug-apl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-apl] box and unbox that work uniformly and without exceptions


From: Frederick H. Pitts
Subject: Re: [Bug-apl] box and unbox that work uniformly and without exceptions
Date: Tue, 20 May 2014 11:48:33 -0500

Dear Blake,

        If the purpose of the ,'' needs documenting, add a comment to the
code and/or assign ,'' to a variable, say
                        PRESERVE_EMBEDDED_SCALARS
and use the variable where needed.  And I personally would have been
disappointed if
        gg←,'' 'a' 'b'
        ⍴gg
did not yield 3.  It was the entire point of the exercise.  Maybe I'm
not a programmer after all.  Really, is
        kk←box 6
        ⍴kk
yielding nothing at all intuitive?  I did put a 6 in the box.
        Match (≡), per the APL2 Programming: Language Reference, p. 173,
yields a 1 if the (evaluated) arguments are the same in structure and
data, and a 0 otherwise.  When evaluated
        ( unbox unbox unbox box box box 6 ) ≡ ⊃ ⊃ ⊃ ⊂ ⊂ ⊂ 6
yields 1, so the right and left arguments are equivalent in the sense
that they both yield identical results in terms of structure and data.
They are also equivalent in that both encapsulate the number 6 three
levels deep in a data structure before unencapsulating it again.  But
you are definitely right that the arguments are not equivalent.  The
left hand argument takes 32 characters to express; the right argument
takes a minimum of 7 (but I like white space).  The time and space
requirements for evaluating the right and left arguments are different
too.  I bet the right argument has smaller requirements by both measures
and will scale better as the argument grows in size and complexity.

Have a good day,

Fred
Retired Chemical Engineer
On Mon, 2014-05-19 at 23:44 -0500, Blake McBride wrote:
> Dear Fred,
> 
> 
> While your code does help in one area, it has a problem in another,
> i.e.:
>       gg←,'' 'a' 'b'
>       ⍴gg
> 3
> 
> 
> Your code says there are three but to the programmer there are only
> two.  The '' is just there to make the rest work.  I suppose you can
> just remember that and change your indexes everywhere and hope someone
> else reading the code gets it.
> 
> 
> Also, in:
> 
> 
> ( unbox unbox unbox box box box 6 ) ≡ ⊃ ⊃ ⊃ ⊂ ⊂ ⊂ 6
> 
> 
> They are not equivalent at all.  Each box adds additional levels of
> nesting as the programmer is specifying.  Each ⊂ above is a null
> function.  This is actually the main point.  box always gives you an
> additional level of nesting regardless of data.  ⊂ always gives you an
> additional level of nesting unless your data happens to be a scalar.
>  What you end up with is dependent on the data.  It is almost like
> saying that:
> 
> 
> x times y gives the result of multiplying x by y, unless x is 88 then
> we add.
> 
> 
> The operation shouldn't be dependent on the data.
> 
> 
> Additionally, you would have to either encapsulate your idiom into a
> function or remember the extra steps everywhere - and remember when to
> use it and when not to use it.
> 
> 
> I am sure there are many ways to skin a cat, or solve this problem.  I
> think mine is cleaner.
> 
> 
> Thanks for your input!
> 
> 
> Blake
> 
> 
> 
> 
> 
> 
> On Mon, May 19, 2014 at 10:34 PM, Frederick H. Pitts
> <address@hidden> wrote:
>         Blake,
>         
>                 Since box and unbox are implemented directly in terms
>         of enclose and
>         disclose, I have to believe that the latter can handle the
>         parameters
>         you require and in a less verbose fashion.
>         
>                 Consider the following code:
>         
>               gg ← ,'' 'a' 'b'
>               gg
>         ┌→─────┐
>         │┌⊖┐ ab│
>         ││ │   │
>         │└─┘   │
>         └∊─────┘
>               hh ← ,'' 'ab'
>               hh
>         ┌→───────┐
>         │┌⊖┐ ┌→─┐│
>         ││ │ │ab││
>         │└─┘ └──┘│
>         └∊───────┘
>         
>                 If the first element of a nested array is a vector,
>         any vector,
>         subsequent scalar character elements remain scalar and are not
>         coalesced
>         into a vector.  hh is presented to highlight the difference
>         between a
>         sequence of scalar character elements and a character vector.
>          There are
>         3 elements in gg and only 2 in hh.  I'm of the opinion that
>         box/unbox
>         work because the box function stuffs a vector in the first
>         element of
>         the nested array.  If not, sequential homogeneous scalars are
>         going to
>         be coalesced to form a vector element.  A requirement for a
>         nested array
>         to exist is that it contain at least one vector element.  The
>         remaining
>         elements can be scalar.  enclose is not going to produce a
>         nested array
>         unless a vector element is supplied and if one is not
>         supplied, enclose
>         will attempt to create one.
>         
>                 Please consider the attached code.  It demonstrates
>         that enclose
>         (actually implicit enclose), dyadic pick and parallel
>         assignment can
>         more succinctly do what the box/unbox pair do, but admittedly
>         at the
>         slight complication of having to supply and account for the
>         vector first
>         element in the nested array and its nested array elements.
>          That
>         complication seems a small price to pay for not having to box
>         and unbox
>         every element of a nested array.
>         
>         Regards,
>         
>         Fred
>         Retired Chemical Engineer
>         
>         





reply via email to

[Prev in Thread] Current Thread [Next in Thread]