[tutorial] Basic Sequence Operations
- Posted by _tom (admin) Jan 22, 2019
- 1871 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 --> { â–¡ , â–§, , â–¡ }