Documentation
API
List

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.

Signatures
List.make(Number, lambda(0,1)) => any
List.make(count: Number, fn: (index?: Number) => 'A) => List('A)
List.make(count: Number, value: 'A) => List('A)
List.make(SampleSetDist) => List(Number)
Examples
List.make(2, 3)
List.make(2, {|| 3})
List.make(2, {|index| index+1})

upTo

Signatures
List.upTo(low: Number, high: Number) => List(Number)
Examples
List.upTo(1,4)

Modifications

reverse

Namespace optional
Signatures
List.reverse(List('A)) => List('A)
Examples
List.reverse([1,4,5]) // [5,4,1]

concat

Namespace optional
Signatures
List.concat(List('A), List('A)) => List('A)
Examples
List.concat([1,2,3], [4, 5, 6])

sortBy

Signatures
List.sortBy(List('A), fn: ('A) => Number) => List('A)
Examples
List.sortBy([{a:3}, {a:1}], {|f| f.a})

append

Signatures
List.append(List('A), 'A) => List('A)
Examples
List.append([1,4],5)

join

Signatures
List.join(List(String), separator?: String?) => String
List.join(List(String)) => String
Examples
List.join(["a", "b", "c"], ",") // "a,b,c"

flatten

Signatures
List.flatten(List(any)) => List(any)
Examples
List.flatten([[1,2], [3,4]])

shuffle

Signatures
List.shuffle(List('A)) => List('A)
Examples
List.shuffle([1,3,4,20])

zip

Signatures
List.zip(List('A), List('B)) => List(['A, 'B])
Examples
List.zip([1,3,4,20], [2,4,5,6])

unzip

Signatures
List.unzip(List(['A, 'B])) => [List('A), List('B)]
Examples
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.

Signatures
List.slice(List('A), startIndex: Number, endIndex?: Number?) => List('A)
Examples
List.slice([1,2,5,10],1,3)

uniq

Filters the list for unique elements. Works on select Squiggle types.

Signatures
List.uniq(List('A)) => List('A)
Examples
List.uniq([1,2,3,"hi",false,"hi"])

uniqBy

Filters the list for unique elements. Works on select Squiggle types.

Signatures
List.uniqBy(List('A), ('A) => 'B) => List('A)
Examples
List.uniqBy([[1,5], [3,5], [5,7]], {|x| x[1]})

filter

Namespace optional
Signatures
List.filter(List('A), fn: ('A) => Bool) => List('A)
Examples
List.filter([1,4,5], {|x| x>3})

Queries

length

Signatures
List.length(List(any)) => Number
Examples
List.length([1,4,5])

first

Signatures
List.first(List('A)) => 'A
Examples
List.first([1,4,5])

last

Signatures
List.last(List('A)) => 'A
Examples
List.last([1,4,5])

minBy

Signatures
List.minBy(List('A), fn: ('A) => Number) => 'A
Examples
List.minBy([{a:3}, {a:1}], {|f| f.a})

maxBy

Signatures
List.maxBy(List('A), fn: ('A) => Number) => 'A
Examples
List.maxBy([{a:3}, {a:1}], {|f| f.a})

every

Signatures
List.every(List('A), fn: ('A) => Bool) => Bool
Examples
List.every([1,4,5], {|el| el>3 })

some

Signatures
List.some(List('A), fn: ('A) => Bool) => Bool
Examples
List.some([1,4,5], {|el| el>3 })

find

Returns an error if there is no value found

Signatures
List.find(List('A), fn: ('A) => Bool) => 'A
Examples
List.find([1,4,5], {|el| el>3 })

findIndex

Returns -1 if there is no value found

Signatures
List.findIndex(List('A), fn: ('A) => Bool) => Number
Examples
List.findIndex([1,4,5], {|el| el>3 })

Functional Transformations

map

Namespace optional
Signatures
List.map(Number, lambda(1,2)) => any
List.map(List('A), ('A, index?: Number) => 'B) => List('B)
Examples
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.

Namespace optional
Signatures
List.reduce(Number, fn: lambda(2,3)) => any
List.reduce(List('B), initialValue: 'A, callbackFn: (accumulator: 'A, currentValue: 'B, currentIndex?: Number) => 'A) => 'A
Examples
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.

Namespace optional
Signatures
List.reduceReverse(List('B), initialValue: 'A, callbackFn: (accumulator: 'A, currentValue: 'B) => 'A) => 'A
Examples
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.

Signatures
List.reduceWhile(List('B), initialValue: 'A, callbackFn: (accumulator: 'A, currentValue: 'B) => 'A, conditionFn: ('A) => Bool) => 'A
Examples
// 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}
)