Re: Using insert()

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

o☰

o

OpenEuphoria means atom|sequence.

┌───┐ 
│o☰ │ 
└───┘ 


Legend: ○ atom, ☰ sequence, □ object.

An atom ● symbol is any single number value.
A sequence ☰ symbol is any sequence { ■ , ■ } or string "●●".
An object ■ symbol is any value ● or ☰.

-- typical objects 
{ ■ , ■ , ■ , ■ }   { □ , □ , ☰ }           "●●●" 
 
3.1  { 1,  2,  3,  4 }   {  "hi", 5, "hello" }   "cat" 

Ideas Grow in Pairs

Ideas, values, and actions in oE|Phix often come as pairs:

atom | sequence

item | slice

remove | insert

introduce | join

append | concatenate

object | { object }

...

If write code featuring one idea and you get an error or unexpected result--you only have to check on the opposite pair. You are always 50% or 100% correct in Euphoria thinking.

Replace | Remove

An assignment action "gives an object its value." Use an assignment set the original value, or to change the value of an object. The result of an assignment is to replace one value with another.

atom a = ●    -- initial value 
 
     a = ○    -- new assignment, new value 
 
sequence s = {} 
         s = { □ , □ , ☰ } 

An item is "one object in a sequence list."

-- third item is □ 
 
{ ■ , ■ , □ , ■ } 

A slice is "a sub-sequence within a sequence."

-- original sequence 
{ □ , ▧ , ■ , ■ , ■  } 

permits many slicing choices

 
-- a few possible slices 
{ }  { }  
 
{ □ , ▧ } { ▧ , ■ }  
 
{  □ , ▧ , ■ }  { ■ , ■ , ■  } 
 
... 
 
{ □ , ▧ , ■ , ■ , ■   } 

An assginment in oE can not destroy or create top-level items in a sequence list. The length of a sequence does not change after any assignment. You can assign any value to a single item in a sequence. You can assign a slice to a slice, as long as their lengths are identical.

The original oE slice and the replacement oE slice must have the same length.

sequence s = { ■ ,  ■ , ■ ,     ■ ,       ■  } 
        s[1] = ▧  
 
       s[2..3] =  { □ , □  } 
 
            s[3..4] = { "●●●", "○○○" } 
 
? s 
--> { ▧ , □ , "●●●", "○○○" , ■ } 
sequence s = { 1,2,3,4,5 } 
s[1] = 10 
s[2..3] = { 200, 300 } 
s[3..4] = { "cat", "dog" } 
? s 
 
--> {10,200,"cat","dog",5} 

oE is strict about the conservation of items when you make an assignment to a sequence.

-- oE will issue error messages 
 
sequence s  
 
s = {1,2,3,4,5} 
s[1..3] = { 100, 200, 300, 400 } 
-- error 
-- lengths do not match on assignment to slice (3 != 4) 
 
s = {1,2,3,4,5} 
s[1..5] = {} 
? s 
-- error 
-- lengths do not match on assignment to slice (5 != 0) 

But, Phix will allow these assignments.

Remove

A remove "destroys an item or slice from a sequence." The new length is now shorter by the one (if item removed) or by the length of the removed slice.

An item is removed:

s = { ■ , □ , ■ , ■ } 
 
s = remove(s, 2 ) 
? s 
    --> { ■ , ■ , ■ } 

A slice is removed:

s = { ■ , □ , □ , ■ } 
s = remove(s, 2, 3 ) 
? s 
  --> { ■ , ■ } 

Warning: object | { object }

An object ■ is NOT THE SAME as a sequence with one object { ■ }. An atom ● and sequence { ● } are clearly different.

The trick is, after a concatenation of an atom ● or sequence { ● } the result looks the same. You can not tell what the starting object was.

 
s = { ■ , ■ , ■ , ■ } 
s = s & ○ 
? s 
    --> { ■ , ■ , ■ , ■ , ○ } 
     
s = { ■ , ■ , ■ , ■ } 
s = s &  { }  
? s 
    --> { ■ , ■ , ■ , ■ , ○ } 
sequence s 
 
s = {1,2,3} 
s = s & 999 
? s 
    --> {1,2,3, 999 } 
     
s = {1,2,3} 
s = s & {999} 
? s 
    --> {1,2,3, 999 } 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu