-
Notifications
You must be signed in to change notification settings - Fork 4
vector
A vector is a contiguous array of values. A matrix is a vector composed of vectors.
(vector 1 2 3) -> (vector: 1 2 3)
(# 1 2 3) -> (vector: 1 2 3)
(# (1 2) (3 4))
-> (matrix (1 2) (3 4))
-> (vector: (vector: 1 2) (vector: 3 4))
The vector constructor procedure can take a size: option.
(vector 1 2 3 size: 10)
-> (vector: 1 2 3 null null null null null null null)
x.n, (x n) -> if n is a number, return the (n-1)th value of the vector.
(def v (vector 1 2 3))
v.0 -> 1
(v 0) -> 1
v.2 -> 3
x.type -> vector
x.empty? -> true if x is size 0, false otherwise
x.to-bool -> false if x is size 0, true otherwise
x.size -> number of cells in the vector
x.clone -> copy the vector
x.pairs -> return a list of pairs with each index and value of the vector
(x.has? item) -> true if item is a member of the vector, false otherwise
(x.apply args) -> this allows lists to be used at the head of a code list.
(def v (# 1 2 3))
(v 'size) -> (send v 'size) -> 3
x.to-list -> return a list of the vector's items
x.to-vector -> x
x.to-text -> return a text. Works only on vectors composed solely of runes.
(def my-runes (vector $h $e $l $l $o))
my-runes.to-text => "hello"
(x.fold init proc) -> apply the procedure proc to successive items of the vector, returning a single value
(x.reduce init proc) -> like fold, but breadth-first
(x.map proc) -> execute proc for each item of the vector and return a vector of results
(x.each proc) -> execute proc for each item of the vector, and return nothing. Used for side effects.
(x.filter predicate) -> return a vector of items for whom the predicate returned true
(x.sort predicate) -> sort the vector according to the given predicate
(def nums (vector 8 6 7 5 3 0 9))
(nums.fold 0 +) -> 38
(nums.reduce 0 +) -> 38
(nums.map (proc (x) (* 10 x))) -> (vector: 80 60 70 50 30 0 90)
(nums.filter (proc (x) (> x 5))) -> (vector: 8 6 7 9)
(nums.sort <) -> (vector: 0 3 5 6 7 8 9)
(nums.sort >) -> (vector: 9 8 7 6 5 3 0)
(nums.each (proc (x) (sys.say x)))
8
6
7
5
3
0
9
-> null