# Lists

l at: index → any | l is-a?: List | index is-a?: Integer

Returns the list's value at `index`

. If the index is out of bounds, `@(out-of-bounds: index for-list: l)`

is raised.

l at: index put: v → List | l is-a?: List | index is-a?: Integer

Yield a new list with the value at `index`

replaced with `v`

. If the index is out of bounds, `@(out-of-bounds: index for-list: l)`

is raised.

l head → any | l is-a?: List

Returns the first value of the list. If the list is empty, `@empty-list`

is raised.

*Example:*

> [1, 2, 3] head 1

l last → any | l is-a?: List

Returns the last value of the list. If the list is empty, `@empty-list`

is raised.

*Example:*

> [1, 2, 3] last 3

l init → List | l is-a?: List

Returns all but the last value of the list. If the list is empty, `@empty-list`

is raised.

*Example:*

> [1, 2, 3] init [1, 2]

l tail → List | l is-a?: List

Returns all but the first value of the list. If the list is empty, `@empty-list`

is raised.

*Example:*

> [1, 2, 3] tail [2, 3]

l from: start to: end → List | l is-a?: List | start is-a?: Integer | end is-a?: Integer

Returns the slice of elements from `start`

to `end`

of `l`

. If the range is invalid, `@(invalid-range: @(from: start to: end) for-list: l)`

is raised. A range is invalid if either `start`

or `end`

are negative, or if either are out of the list's boundaries.

l take: n → List | l is-a?: List | n is-a?: Integer

Returns a list of the first `n`

values of the list. If `n`

is greater than the list's length, it returns as many as it can (i.e., the entire list); the length of the returned list will not always be `n`

.

l drop: n → List | l is-a?: List | n is-a?: Integer

Returns a list of the values after `n`

values have been dropped from the start of the list. If `n`

is greater than the list's length, it returns an empty list.

v replicate: n → List | n is-a?: Integer

Returns a list containing `v`

replicated `n`

times.

*Example:*

> "Odelay!" replicate: 5 ["Odelay!", "Odelay!", "Odelay!", "Odelay!", "Odelay!"]

l reverse → List | l is-a?: List

Returns a new list, with all values of `l`

, in reversed order.

*Example:*

> [1, 2, 3] reverse [3, 2, 1]

l map: a → List | l is-a?: List | a responds-to?: @call:

Returns a new list, with `call:`

sent to `a`

with each value in `l`

.

x zip: (... ys) &zipper: @id → List | x is-a?: List | zipper responds-to?: @call:

"Zips" up the lists `x`

and `ys`

. If `zipper`

is not `@id`

, it is `call:`

ed with each zipped value as an argument.

The list it returns is as long as the shortest list.

l take-while: p → List | l is-a?: List | p responds-to?: @call:

Takes values from list `l`

while `p`

called on them yields `True`

.

*Example:*

> [1, 3, 4, 5, 6] take-while: @odd? [1, 3]

l drop-while: p → List | l is-a?: List | p responds-to?: @call:

Removes values from list `l`

while `p`

called on them yields `True`

, returning a list of the remaining values.

*Example:*

> [1, 3, 4, 5, 6] drop-while: @odd? [4, 5, 6]

l filter: p → List | l is-a?: List | p responds-to?: @call:

Returns a list of all values in `l`

that satisfy the predicate `p`

.

l reduce: b → any | l is-a?: List | b responds-to?: @call:

Alias for `l tail reduce: b with: l head`

. If the list is empty, `@empty-list`

is raised.

*Example:*

> [1, 2, 3, 4, 5] reduce: @* 120

l reduce: b with: v → any | l is-a?: List | b responds-to?: @call:

Reduces the list to a single value by "folding" over it, with `v`

as the initial value.

l reduce-right: b → any | l is-a?: List | b responds-to?: @call:

Similar to `reduce:`

, but right-associative. If the list is empty, `@empty-list`

is raised.

l reduce-right: b with: v → any | l is-a?: List | b responds-to?: @call:

l concat → List | l is-a?: List

Reduces a list by appending all of its values. The list's values must all be lists.

l maximum → any | l is-a?: List

Reduces a list with `max:`

. The list must not be empty.

*Example:*

> [1, 2, 3] maximum 3

l minimum → any | l is-a?: List

Reduces a list with `min:`

. The list must not be empty.

*Example:*

> [1, 2, 3] minimum 1

l all?: p → Boolean | l is-a?: List | p responds-to?: @call:

Checks if all values in `l`

satisfy the predicate `p`

.

l any?: p → Boolean | l is-a?: List | p responds-to?: @call:

Checks if any values in `l`

satisfy the predicate `p`

.

l contains?: v → Boolean | l is-a?: List

Check if `v`

is a member of the list `l`

.

*Example:*

> "abc" contains?: $a True > "abc" contains?: $d False

v in?: l → Boolean | l is-a?: List

Alias for `l contains?: v`

.

l push: v → List | l is-a?: List

Yields a new list with

added to the end of `v`

.
`l`

*Example:*

> [1, 2, 3] push: 4 [1, 2, 3, 4]

l split: delimiter → List | l is-a?: List | delimiter is-a?: List

Split a list into sublists, separated by `delimiter`

. Note that strict equality (i.e. `equals?:`

) is used for matching the delimiter.

l split-on: o → List | l is-a?: List

Split `l`

into sublists, breaking on every occurrence of `o`

. Also uses strict equality.

l sort → List | l is-a?: List

Returns a sorted list of `l`

's values, using the mergesort algorithm. It does this by sending `>`

to determine the larger between two values.