List
Lists are a simple data structure that can hold any type of value. They are similar to arrays in Javascript or lists in Python.
myList = [1, 2, 3, normal(5,2), "hello"]
Lists are immutable, meaning that they cannot be modified. Instead, all list functions return a new list.
Constructors
make
Creates an array of length count
, with each element being value
. If value
is a function, it will be called count
times, with the index as the argument.
List.make(2, 3)
List.make(2, {|| 3})
List.make(2, {|index| index+1})
upTo
List.upTo(1,4)
Modifications
reverse
List.reverse([1,4,5]) // [5,4,1]
concat
List.concat([1,2,3], [4, 5, 6])
sortBy
List.sortBy([{a:3}, {a:1}], {|f| f.a})
append
List.append([1,4],5)
join
List.join(["a", "b", "c"], ",") // "a,b,c"
flatten
List.flatten([[1,2], [3,4]])
shuffle
List.shuffle([1,3,4,20])
zip
List.zip([1,3,4,20], [2,4,5,6])
unzip
List.unzip([[1,2], [2,3], [4,5]])
Filtering
slice
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,5,10],1,3)
uniq
Filters the list for unique elements. Works on select Squiggle types.
List.uniq([1,2,3,"hi",false,"hi"])
uniqBy
Filters the list for unique elements. Works on select Squiggle types.
List.uniqBy([[1,5], [3,5], [5,7]], {|x| x[1]})
filter
List.filter([1,4,5], {|x| x>3})
Queries
length
List.length([1,4,5])
first
List.first([1,4,5])
last
List.last([1,4,5])
minBy
List.minBy([{a:3}, {a:1}], {|f| f.a})
maxBy
List.maxBy([{a:3}, {a:1}], {|f| f.a})
every
List.every([1,4,5], {|el| el>3 })
some
List.some([1,4,5], {|el| el>3 })
find
Returns an error if there is no value found
List.find([1,4,5], {|el| el>3 })
findIndex
Returns -1
if there is no value found
List.findIndex([1,4,5], {|el| el>3 })
Functional Transformations
map
List.map([1,4,5], {|x| x+1})
List.map([1,4,5], {|x,i| x+i+1})
reduce
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.
List.reduce([1,4,5], 2, {|acc, el| acc+el})
reduceReverse
Works like reduce
, but the function is applied to each item from the last back to the first.
List.reduceReverse([1,4,5], 2, {|acc, el| acc-el})
reduceWhile
Works like reduce
, but stops when the condition is no longer met. This is useful, in part, for simulating processes that need to stop based on the process state.
// 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} )