indexing - pannous/warp GitHub Wiki
indexing
Angle offers
- zero based indexing (a b c)[1] == b
- one based indexing {a b c}#2 == b
An index (from 0) and a position (from 1) are both ints, and warp tracks which one a value is. xs.index_of(x)
gives a position. argmax(xs) and argmin(xs) give an index. xs.insert(v, at: i) takes an index. A variable
can be declared either one: p: position, i: index. When one meets the other's brackets it is converted, so
xs = [10 20 30]; xs[xs.index_of(20)] is 20. Arithmetic keeps the kind: xs[xs.index_of(10) + 1] is 20, the
next item. The difference of two positions is a plain count.
A range takes a slice: xs#(2 to 3) and xs#(2…3) are positions 2 through 3, xs[1:3] indexes 1 up to 3 as in
Python; both give [20 30] for [10 20 30 40]. It works on a list of objects too: people#(11…20) is a page of
ten.
-
map field object accessors:
-
{a:1 b:2}[a] == 1
-
{a:1 b:2}.a == 1
-
a in {a:1 b:2} == 1
-
a of {a:1 b:2} == 1
Direct access
Bracket indexing is as a general rule close to metal: "abc"[1]='B' manipultates the byte sequence blindly, whereas "abc"#2="β" sets the character unicode safely.
The bracket version allows complex expressions, whereas '#' 'in' and 'of' bind tightly, so
1 + 1 in {1:2} == 1 + {1:2}['1'] == 1+2
TODO: REALLY ?
1 + 1 in {1:3} == (1+1) in {1:3} == false
Solution: compiler error: mixing indexing and expressions needs grouping
{a:1 b:2}[a] is related to pattern matching and evaluation
{a:1 b:2}(a)