1. [tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 22, 2019
- 1576 views
Trying some new definitions for properties and functions.
_tom
Sequence Basics
A sequence is "a list of objects." A string is "a flat sequence of integer values."
All of these examples work on oE|Phix (Phix has additional features).
s = { □ , □ , □ , □ } -- sequence str = "●●●●" -- string
Subscript notation lets us identify the parts of a sequence: item s[2] , slice s[2..2] , and gap s[2..1] .
1 2 3 4 -- index values s = { □ , □ , □ , □ } {} {□} gap slice s[2..1] s[2..2] □ item s[2]
The parts of a sequence can be output:
s = { □ , ■ , □ , □ } ? s[2] --> ■ -- item ? s[2..2] --> { ■ } -- slice ? s[2..1] --> {} -- gap
You can change the value of an item with an assignment statement.
s = { □ , ■ , □ , □ } s[2] = ▧ ? s --> { □ , ▧ , □ , □ }
fill
You can fill a slice with an atom value:
s = { □ , ■ , ■ , □ } s[2..3] = ● ? s --> { □ , ● , ● , □ }
sequence to slice
You can assign items of a sequence to a slice of the same length:
s = { □ , ■ , ■ , □ } s[2..3] = { ▧ , ▧ } ? s --> { □ , ▧ , ▧ , □ }
remove)
The remove) function "gets rid of an item or slice."
s = { □ , ■ , □ , □ } s = remove(s, 2 ) ? s --> { □ , □ , □ } s = { □ , ■ , ■ , □ } s = remove(s, 2 , 3 ) ? s --> { □ , □ }
insert)
The insert) function "replaces a gap with one object." The length of the target sequence increases by one.
-- gap -- s[2..1] s = { □ , ■ , □ , □ } s = insert(s, {▧,▧}, 2 ) ? s --> { □ , {▧,▧}, ■ , □ , □ }
splice)
The splice) function "end to end joins the items of a sequence in a gap." The length of the target sequence increases by the length of the spliced sequence.
-- gap -- s[2..1] s = { □ , ■ , □ , □ } s = splice(s, {▧,▧}, 2 ) ? s --> { □ , ▧,▧ , ■ , □ , □ }
replace)
The replace) function "end to end joins the items of a sequence where a slice used to be."
-- slice -- s[2..3] s = { □ , ■ , ■ , □ } s = replace(s, {▧,▧,▧}, 2 , 3 ) ? s --> { □ , ▧, ▧, ▧ , □ }
The length of the target sequence can change.
-- slice -- s[2..3] s = { □ , ■ , ■ , □ } s = replace(s, {▧}, 2 , 3 ) ? s --> { □ , ▧, , □ }
2. Re: [tutorial] Basic Sequence Operations
- Posted by lgregg Jan 23, 2019
- 1503 views
This is a nice tutorial on sequences. I learned some new things. Thanks.
3. Re: [tutorial] Basic Sequence Operations
- Posted by petelomax Jan 23, 2019
- 1508 views
You can assign items of a sequence to a slice of the same length:
or of any length in phix, eg:
string s = "fed" s[2..2] = "easte" -- "e"->"easte", or --s[2..1] = "east" -- ie "f" & x & "ed" --s[3..2] = "aste" -- ie "fe" & x & "d" ?s -- s is now "feasted" -- the two alternatives insert/replace an empty slice s[2..6] = "e" -- "easte" -> "e" --s[2..5] = "" -- or "east" -> "" --s[3..6] = "" -- or "aste" -> "" ?s --s is now "fed"
4. Re: [tutorial] Basic Sequence Operations
- Posted by archeopteryx_08 Jan 24, 2019
- 1463 views
Trying some new definitions for properties and functions.
_tom
First, let me thank you for the marvelous work you've done writing these helpful tutorials. I keep learning a lot from them.
When i came to Eu v2 from BASIC as a self-taught hobbyist, i found sequences challenging from the start: that they're not the same as arrays; that an empty sequence really is empty; that a printable letter on its own is different from the same letter inside a sequence; that a string is really a sequence in disguise; that a character is really a small integer in disguise...
When learning about Eu objects, I found it helpful to think in terms of billiard balls on an infinitely large table. I imagined an atom as a lone billiard ball, with its decimal value painted on its surface and its binary value encased inside it. I imagined some billiard balls with their printable-character value overlaid on their decimal value, with their binary value deep inside them, so that if i needed to, i could peel back the character to reveal its decimal value. (This helped me make sense of trace() displays, print() output, and ex.err files, with their character/number combinations.) Keeping with the billiard simile, for sequences i imagined elastic, extensible frames that could stretch around any number of balls or other frames that had balls in them, or even nothing at all in them. Then i imagined those billiard balls - alone or within their own elastic frame - being inserted, removed, replaced, reversed, reassigned, or otherwise manipulated as needed. For some data structures, i imagined elastic frames that had been twisted to look (for example) like a table (eg a 3x3 table), but which were really one long sequence (eg of 9 top-level elements) that had been bent into a different shape; this helped me understand them as the same object, with a different look. An empty sequence looked like a collapsed elastic frame with nothing in it - not even a space (which had confused me at the start). One or more billiard balls spread around the table, were atoms. The same balls inside one or more elastic frames of their own, represented one or more sequences. A string became a sequence of balls inside a single elastic frame, with no other framed balls within it.
I thought i'd share this way of thinking about sequences, in case you find a use for it in your excellent tutorials.
PS: i couldn't play billiards if my life depended on it!
_Arch
5. Re: [tutorial] Basic Sequence Operations
- Posted by irv Jan 25, 2019
- 1420 views
Oops - double posted.
6. Re: [tutorial] Basic Sequence Operations
- Posted by irv Jan 25, 2019
- 1458 views
That's an excellent analogy!
It could be the basis for a neat demo, with graphics. I might just do that.
(Oh... I kinda majored in billiards in college :)
7. Re: [tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 25, 2019
- 1399 views
This is a nice tutorial on sequences. I learned some new things. Thanks.
I'll try to write a few more.
Thank you.
_tom
8. Re: [tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 25, 2019
- 1383 views
...
PS: i couldn't play billiards if my life depended on it!
_Arch
Thank for your input. These diagrams will now be officially called "Billiard ball diagrams."
_tom
9. Re: [tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 25, 2019
- 1393 views
You can assign items of a sequence to a slice of the same length:
or of any length in phix, eg:
I will make some Billiard Ball Diagrams for Phix as well.
_tom
10. Re: [tutorial] Basic Sequence Operations
- Posted by archeopteryx_08 Jan 26, 2019
- 1353 views
...
PS: i couldn't play billiards if my life depended on it!
_Arch
Thank for your input. These diagrams will now be officially called "Billiard ball diagrams."
_tom
I'm glad they're useful. I found them helpful in my case. Often, I drew diagrams and tediously moved the balls around. They helped me:
_ distinguish between atoms and sequences
_ visualise how a character was an integer in disguise
_ visualise the difference between (eg) 'b' and "b"
_ visualise a string as a sequence of characters
_ visualise an empty string
_ visualise printf ( 1, "%s", "euphoria" ) vs printf ( 1, "%s", { "euphoria" } )
_ understand concatenation vs append()
_ visualise append, prepend, slice, splice, insert
_Arch
11. Re: [tutorial] Basic Sequence Operations
- Posted by archeopteryx_08 Jan 27, 2019
- 1328 views
Speaking of basic sequence operations, on page 43 of the euphoria (4.1) pdf there is this code:
sequence s = {1,2,3} for i=0 to length(s)+1 do ? insert(si, 99, i ) end for -- the output is -- {99,1,2,3} -- {99,1,2,3} -- same as prepend(s,99) -- {1,99,2,3} -- {1,2,99,3} -- {1,2,3,99} -- same as append(s,99)
I understand what it's doing, but i can't help feel uncomfortable that insert() works when i=0, and that it gives the same value as when i=1. At (my) first sight, it appears visually to suggest that the inserted object (ie 99) should occupy s[0] (i know it doesn't!), then s[1] ... to s[4] - almost like a visual illusion. It isn't a big thing, but maybe insert() was designed to work this way; maybe it works well in real code ?
_Arch
12. Re: [tutorial] Basic Sequence Operations
- Posted by irv Jan 27, 2019
- 1306 views
? insert(s,99,8) -- {1,2,3,99}
Note that this, too is semantically puzzling. Any number larger than the length of the sequence just appends, resulting in a sequence of length 4. It doesn't expand the sequence to the required length, and then set s[8] = 99, as one might expect. But that would bring up another problem: what values are to be used to fill in s[4..7]?
The alternative would be to have errors - something like s[0] doesn't exist! and can't insert past end of sequence! Those would be easy to add.
The real question being "is this a problem worth fixing?"
13. Re: [tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 27, 2019
- 1286 views
? insert(s,99,8) -- {1,2,3,99}
Note that this, too is semantically puzzling. Any number larger than the length of the sequence just appends, resulting in a sequence of length 4. It doesn't expand the sequence to the required length, and then set s[8] = 99, as one might expect. But that would bring up another problem: what values are to be used to fill in s[4..7]?
The alternative would be to have errors - something like s[0] doesn't exist! and can't insert past end of sequence! Those would be easy to add.
The real question being "is this a problem worth fixing?"
The Phix version of insert) crashes "as expected."
This looks like a ?feature? of oE that no-one has noticed until now.
It looks like Python also has this strange kind of behavior.
I would describe the oE behaviour as a bug.
sequence s = {1,2,3} s = insert(s, 99, -11 ) ? s --> {99,1,2,3}
_tom