Re: Discuss changing the behaviour of append()

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

Here is my first draft at documenting the proposed changes.

_tom


Sequence Manipulations

The length function "counts top level items and returns the count as a length value." Nested items are not part of the top level count. (Counting is as good as measuring a length. To get the width of a road count the number of steps you take to get across.)

? length( {} ) 
    --> 0 
? length( 4 ) 
    --> 1 
? length( {4,5,"hello"} ) 
    --> 3 
Length Example Note
0 {} No items to count; length zero
1 a One potential item, an atom; length one
1 {x} One item, an object; length one
3 {x,x,x} Three items; length three

You can argue that an atom (a number or character) has no length. It is convenient to recognize that an atom adds one to the length of a sequence after a prepend, append, or concatenation.

You can not use length to distinguish an atom from a sequence.

There are various ways to increase the length of an object:

  • The coarse-grained functions (prepend, append) increase length by exactly one.
  • The fine-grained operator (&) increases length by the length of the object added.

In a few cases a function or the concatenation operator can be used to produce identical results. However, there are significant differences most of the time.

Number and text data behave the same way.

Length Increasing Functions

The prepend function "adds one entire object to the head of the target object." The append function "adds one entire object to the tail of the target object."

Coarse-grained means "the entire object is added as one item."

It is idiomatic in Euphoria to write these functions so the first argument is the target and the second argument is what gets added.

To change a target variable you have to explicitly assign the new value to it:

x = prepend(x, new_item ) 
 
x = append(x, new_item  ) 
Coarse-Grained Prepend Append
Target Argument
Entire Object
To Head To Tail
atom
5
{}

? prepend(5, {})
-->  {{}, 5} 

? append(5, {})
-->     {5, {}} 
6

? prepend(5, 6)
-->   {6, 5} 

? append(5, 6)
-->     {5, 6} 
{6}

? prepend(5,{6} )
--> {{6}, 5} 

? append(5, {6})
-->     {5, {6}} 
{6,7}

?   prepend(5, {6,7})
--> {{6,7}, 5} 

? append(5, {6,7})
-->     {5, {6,7}} 
sequence
{4,5}
{}

? prepend({4,5}, {})
-->   {{}, 4,5} 

?   append({4,5}, {})
-->        {4,5,  {}} 
6

? prepend({4,5}, 6)
-->    {6, 4,5} 

? append({4,5}, 6)
-->      {4,5,  6} 
{6}

? prepend({4,5},{6})
-->  {{6}, 4,5} 

? append({4.5}, {6})
-->      {4,5,  {6}} 
{6,7}

?  prepend({4,5}, {6,7})
--> {{6,7}, 4,5} 

? append({4,5}, {6,7})
-->      {4,5,  {6,7}} 

Length Increasing Operator &

The concatenation operator & "increases the length of the target object by the length of the object added."

Fine-grained means "all items are added."

Fine-Grained Concatenate Concatenate
Target Argument
All Items
To Head To Tail
atom
5
  {}

? {} & 5
-->    5 

?   5 & {}
--> 5 
6

?  6 & 5
--> {6,5} 

?    5 & 6
--> {5,6} 
{6}

?  {6} & 5
-->   {6,5} 

?    5 & {6}
--> {5,6} 
{6,7}

? {6,7} & 5
-->  {6,7,5} 

?   5 & {6,7}
--> {5,6,7} 
sequence
{4,5}
  {}

? {} & {4,5}
-->    {4,5} 

?   {4,5} & {}
--> {4,5} 
6

?  6 & {4,5}
-->  {6,4,5} 

?   {4,5} & 6
--> {4,5,6} 
{6}

?  {6} & {4,5}
-->    {6,4,5} 

?   {4,5} & {6}
--> {4,5,6} 
{6,7}

? {6,7} & {4,5}
-->   {6,7,4,5} 

?    {4,5} & {6,7}
-->  {4,5,6,7} 

Idiomatic Concatenation

You can combine the concatenation & operator with the assignment = statement.

object x=5, y=6 
x &= y 
? x 
--> {5,6} 
 
x &= y    -- short form 
x = x & y -- long form  

One use for the &= shortcut is to make concatenation & behave like an append function:

object x, y 
x &= {y} 
Target Argument Result
x y Concatenation Append

x -->

x &= {y}

x = append(x,y)
atom
5
{}

x --> {5,{}}
6

x --> {5,6}
{6}

x --> {5,{6}}
{6,7}

x --> {5,{6,7}}
sequence
{4,5}
{}

x --> {4,5,{}}
6

x --> {4,5,6}
{6}

x --> {4,5,{6}}
{6,7}

x --> {4,5,{6,7}}

Legacy Notes

Early versions (O[ 3.1 and before) of length did not recognize the length of an atom as one.

Early versions (O[ 4.0 and before) of prepend and append would not allow the target to be an atom.

See Also

insert, splice, remove

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

Search



Quick Links

User menu

Not signed in.

Misc Menu