squiggle/packages/website/docs/Api/List.md

109 lines
1.9 KiB
Markdown
Raw Permalink Normal View History

2022-06-06 04:47:29 +00:00
---
2022-06-11 00:35:48 +00:00
sidebar_position: 7
2022-06-06 04:47:29 +00:00
title: List
---
2022-06-14 23:12:29 +00:00
Squiggle lists are a lot like Python lists or Ruby arrays. They accept all types.
```javascript
2022-06-14 23:47:31 +00:00
myList = [3, normal(5, 2), "random"];
2022-06-14 23:12:29 +00:00
```
2022-06-11 00:35:48 +00:00
### make
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
List.make: (number, 'a) => list<'a>
```
2022-06-11 15:57:02 +00:00
2022-06-14 23:12:29 +00:00
Returns an array of size `n` filled with the value.
2022-06-06 04:47:29 +00:00
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
List.make(4, 1); // creates the list [1, 1, 1, 1]
2022-06-11 00:35:48 +00:00
```
2022-06-06 04:47:29 +00:00
2022-06-11 00:35:48 +00:00
See [Rescript implementation](https://rescript-lang.org/docs/manual/latest/api/belt/array#make)
2022-06-06 04:47:29 +00:00
### toString
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
toString: (list<'a>) => string
2022-06-06 04:47:29 +00:00
```
### length
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
length: (list<'a>) => number
2022-06-06 04:47:29 +00:00
```
### upTo
2022-06-14 23:12:29 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
List.upTo: (low:number, high:number) => list<number>
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
2022-06-11 00:35:48 +00:00
```js
2022-06-11 15:57:02 +00:00
List.upTo(0, 5); // creates the list [0, 1, 2, 3, 4, 5]
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
Syntax taken from [Ruby](https://apidock.com/ruby/v2_5_5/Integer/upto).
2022-06-06 04:47:29 +00:00
2022-06-11 00:35:48 +00:00
### first
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
first: (list<'a>) => 'a
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
### last
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
last: (list<'a>) => 'a
2022-06-06 04:47:29 +00:00
```
### reverse
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
reverse: (list<'a>) => list<'a>
2022-06-06 04:47:29 +00:00
```
### map
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
map: (list<'a>, a => b) => list<'b>
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
See [Rescript implementation](https://rescript-lang.org/docs/manual/latest/api/belt/array#map).
2022-06-06 04:47:29 +00:00
2022-07-29 15:07:11 +00:00
### filter
```
filter: (list<'a>, 'a => bool) => list<'a>
```
2022-07-29 17:26:14 +00:00
2022-07-29 15:07:11 +00:00
See [Rescript implementation of keep](https://rescript-lang.org/docs/manual/latest/api/belt/array#keep), which is functionally equivalent.
2022-06-06 04:47:29 +00:00
### reduce
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
reduce: (list<'b>, 'a, ('a, 'b) => 'a) => 'a
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
`reduce(arr, init, f)`
Applies `f` to each element of `arr`. The function `f` has two paramaters, an accumulator and the next value from the array.
2022-06-06 04:47:29 +00:00
2022-06-11 00:35:48 +00:00
```js
reduce([2, 3, 4], 1, {|acc, value| acc + value}) == 10
2022-06-06 04:47:29 +00:00
```
2022-06-11 00:35:48 +00:00
See [Rescript implementation](https://rescript-lang.org/docs/manual/latest/api/belt/array#reduce).
2022-06-06 04:47:29 +00:00
2022-06-11 00:35:48 +00:00
### reduce reverse
2022-06-11 15:57:02 +00:00
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
reduceReverse: (list<'b>, 'a, ('a, 'b) => 'a) => 'a
2022-06-06 04:47:29 +00:00
```
2022-06-11 15:57:02 +00:00
Works like `reduce`, but the function is applied to each item from the last back to the first.
2022-06-06 04:47:29 +00:00
2022-07-29 17:26:14 +00:00
See [Rescript implementation](https://rescript-lang.org/docs/manual/latest/api/belt/array#reducereverse).