RE: Syntax of &-operator

new topic     » topic index » view thread      » older message » newer message

Hi Lutz,
I'm not sure what the issue is here? When I tried these examples I got
this...

> a =  1,     b =  3     results in a & b = append({a}, b)
{1,3}

> a = {1, 2}, b =  3     results in a & b = append(a, b)
{1,2,3}

> a =  1,     b = {3, 4} results in a & b = prepend(b, a)
{1,3,4}

> a = {1, 2}, b = {3, 4} results in a & b = concat(a, b) -- {1, 2, 3, 4}
{1,2,3,4}

in each case this is exactly what I expected.

> -----Original Message-----
> From: Lutz_Heitmann at NF.MAUS.DE [mailto:Lutz_Heitmann at NF.MAUS.DE]
> Sent: Monday, 2 April 2001 8:18 AM
> To: EUforum
> Subject: Syntax of &-operator
>
>
> Hi Euphorians,
>
> I don't like the syntax of the &-operator. & is a fine tool
> to concatenate
> sequences, but its use is error prone. If you combine
> variables a and b you
> get different results, depending on the types of a and b. E.g.:

The '&' operator always returns the same data type - a sequence, regardless
of the data types that the two operands are. Atoms are treated *as if* they
were single element sequences and '&' works by adding every element of the
second operator to the end of the first operator and returning the resulting
sequence.

Thus, to use your examples...

> a =  1,     b =  3

 a & b ==> 1 & 3 ==> {1} & {3} ==> {1,3}

> a = {1, 2}, b =  3

 a & b ==> {1,2} & 3 ==> {1,2} & {3} ==> {1,2,3}

> a =  1,     b = {3, 4}
 a & b ==> 1 & {3,4} ==> {1} & {3, 4} ==> {1,3,4}

> a = {1, 2}, b = {3, 4}
 a & b ==> {1,2} & {3,4} ==> {1,2,3,4}

>
> I'm using "concat" for the only usage of "&" that makes sense
> to me. Of
> course I don't want something like a & b & c & d (all atoms)
> to be written
> like this:
>
> append(append(append({a},b),c),d)
>
> It could be coded like this:
>
> {a} & {b} & {c} & {d}

or like this ...
  {a,b,c,d}

>
> -- "concat" is just too long as a word
>
> A little bit more typing but a lot clearer. A lot of programs
> are using "&"
> the "old" way, and that can't be changed. But I still don't like it...

the "old" way? What is the "new" way then? How have things changed? Are you
advocating that a new function be included in Euphoria called 'concat' and
that it should work the same as '&' operation?

-------------
 global function concat( object a, object b)
     return a & b
 end function
-------------

Now, append() and prepend() do not work the same as '&'.

These also always return a sequence, but they work by adding the second
parameter - as a single thing - to the end/beginning of the first parameter.
This is done regardless of how many elements are in the second parameter.
The net result is the the first parameter gains one, and only one, new
element. Another subtle difference is that only the first parameter, if an
atom, is treated as if it were a single element sequence, the second
parameter is always treated as its own datatype.

Again using the examples supplied...
> a =  1,     b =  3

 append(a , b) ==> append(1 , 3) ==> append({1} , 3) ==> {1,3}

> a = {1, 2}, b =  3

 append(a,b) ==> append({1,2}, 3) ==>  {1,2,3}

> a =  1,     b = {3, 4}
 append(a,b) ==> append(1, {3,4}) ==> append({1},{3, 4}) ==> {1,{3,4}}
 !! Note the different behaviour with that one.

 prepend(b,a) ==> prepend({3,4}, 1) ==> {1,3,4}
 (This is why prepend() seems to "work" like '&' here)

> a = {1, 2}, b = {3, 4}
 append(a, b) ==> append({1,2} ,{3,4}) ==> {1,2,{3,4}}
 !! Note the different behaviour with this too.

-----------
cheers,
Derek Parnell
Senior Design Engineer
Global Technology Australasia Ltd
dparnell at glotec.com.au

---------------------

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu