List
Squiggle lists are a lot like Python lists or Ruby arrays. They accept all types.
myList = [3, normal(5, 2), "random"]
List Creation
make
List.make: (number, 'a) => list<'a>
List.make: (number, () => 'a) => list<'a>
List.make: (number, (index: number) => 'a) => list<'a>
Returns an array of size n
filled with the value.
List.make(4, 1) // [1, 1, 1, 1]
List.make(3, {|index| index * 2}) // [2,4,6]
See Rescript implementation (opens in a new tab)
upTo
List.upTo: (low:number, high:number) => list<number>
List.upTo(0, 5) // [0, 1, 2, 3, 4, 5]
Syntax taken from Ruby (opens in a new tab).
List Querying and Reading
first
List.first: (list<'a>) => 'a
last
List.last: (list<'a>) => 'a
length
List.length: (list<'a>) => number
find
Returns an error if there is no value found.
List.find: (list<'a>, ('a) => bool) => 'a
findIndex
Returns -1
if there is no value found.
List.findIndex: (list<'a>, 'a => bool) => number
every
List.every: (list<'a>, 'a => bool) => list<'a>
some
List.some: (list<'a>, 'a => bool) => list<'a>
List Modification
Please note that methods in the standard library are immutable. They do not alter the input data; instead, they return a modified version.
concat
List.concat: (list<'a>, list<'a>) => list<'a>
append
List.append: (list<'a>, <'a>) => list<'a>
reverse
List.reverse: (list<'a>) => list<'a>
shuffle
List.shuffle: (list<'a>) => list<'a>
zip
List.zip: (list<'a>, list<'b>) => list<['a,'b]>
unzip
List.unzip: (list<['a,'b]>) => list<'a>, list<'b>
join
List.join: (list<string>, string) => string
List.join(["foo", "bar", "char"], "--") // "foo--bar--char"
flatten
flatten: (list<list>) => list
List.flatten([
[1, 2],
[3, 4],
]) // [1,2,3,4]
List Sorting
sortBy
List.sortBy: (list<'a>, ('a => number)) => list<'a>
List.sortBy(
[
{a:2, b:10}
{a:10, b:3}
],
{|obj| obj.b}
) // [{a:10, b:3}, {a:2, b:10}]
minBy
List.minBy: (list<'a>, ('a => number)) => 'a
maxBy
List.maxBy: (list<'a>, ('a => number)) => 'a
List Filtering
slice
List.slice: (list<'a>, minIndex: number) => list<'a>
List.slice: (list<'a>, minIndex: number, maxIndex: number) => list<'a>
Returns a copy of the list, between the selected start
and end
, end not included. Directly uses the Javascript implementation (opens in a new tab) underneath.
List.slice([1,2,3,4,5,6], 2) // [3,4,5,6]
List.slice([1,2,3,4,5,6], 2, 4) // [3,4]
filter
List.filter: (list<'a>, 'a => bool) => list<'a>
See Rescript implementation of keep (opens in a new tab), which is functionally equivalent.
uniq
Filters the list for unique elements. Now only works on some Squiggle types.
List.uniq: (list<'a>) => list<'a>
List.uniq(["foobar", "foobar", 1, 1, 2]) // ["foobar", 1, 2]
uniqBy
Filters the list for unique elements. Now only works on some Squiggle types.
List.uniqBy: (list<'a>, 'a => 'b) => list<'a>
List.uniqBy([{a: 3, b: 10}, {a:3, b:40}, {a:5, b:20}], {|e| e.a}) // [{a: 3, b: 10}, {a:5, b:20}]
Functional Transformations
map
map: (list<'a>, 'a => 'b) => list<'b>
map: (list<'a>, ('a, index: number) => 'b) => list<'b>
map(["foo", "bar"], {|s| s + "!"})
map(["foo", "bar"], {|s, i| {word: s, index: i}})
See Rescript implementation (opens in a new tab).
reduce
reduce: (list<'b>, 'a, ('a, 'b) => 'a) => 'a
reduce: (list<'b>, 'a, ('a, 'b, index: number) => 'a) => 'a
reduce(arr, init, f)
Applies f
to each element of arr
. The function f
has two main paramaters, an accumulator and the next value from the array. It can also accept an optional third index
parameter.
reduce([2, 3, 4], 1, {|acc, value| acc + value}) == 10
See Rescript implementation (opens in a new tab).
reduce reverse
reduceReverse: (list<'b>, 'a, ('a, 'b) => 'a) => 'a
Works like reduce
, but the function is applied to each item from the last back to the first.
See Rescript implementation (opens in a new tab).
reduceWhile
List.reduceWhile: (list<'b>, 'a, ('a, 'b) => 'a, ('a) => bool) => 'a
Works like reduce
, but stops when the accumulator doesn't satisfy the condition, and returns the last accumulator that satisfies the condition (or the initial value if even the initial value doesn't satisfy the condition).
This is useful for simulating processes that need to stop based on the process state.
Example:
/** Adds first two elements, returns `11`. */
List.reduceWhile([5, 6, 7], 0, {|acc, curr| acc + curr}, {|acc| acc < 15})
/** Adds first two elements, returns `{ x: 11 }`. */
List.reduceWhile([5, 6, 7], { x: 0 }, {|acc, curr| { x: acc.x + curr }}, {|acc| acc.x < 15})