[tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 22, 2019
- 1580 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 --> { □ , ▧, , □ }