-
Effekt Library
- list
- List
- Nil
- Cons
- empty
- singleton
- fill
- build
- isEmpty
- nonEmpty
- head
- tail
- headOption
- last
- get
- foreach
- foreach
- foreachIndex
- foreachIndex
- map
- collect
- flatMap
- all
- any
- count
- foldLeft
- foldRight
- sum
- size
- reverse
- reverseOnto
- append
- join
- join
- take
- drop
- slice
- splitAt
- updateAt
- modifyAt
- deleteAt
- insert
- replace
- zip
- zipWith
- unzip
- partition
- sequences
- ascending
- descending
- mergeAll
- mergePairs
- merge
- sortBy
- sort
- sort
- isSortedBy
- show
- show
- show
- show
- show
- println
- println
- println
- println
- list Jump to source: libraries/common/list.effekt
- List
[A]
- Nil
- Cons
(head: A, tail: List[A])
- empty
[A]: List[A] / {}
- singleton
[A] (x: A): List[A] / {}
- fill
[A] (size: Int, default: A): List[A] / {}
- build
[A] (size: Int) { index: (Int) => A }: List[A] / {}
- isEmpty
[A] (l: List[A]): Bool / {}
- nonEmpty
[A] (l: List[A]): Bool / {}
- head
[A] (l: List[A]): A / {Exception[MissingValue]}
- tail
[A] (l: List[A]): List[A] / {Exception[MissingValue]}
- headOption
[A] (l: List[A]): Option[A] / {}
- last
[A] (l: List[A]): A / {Exception[MissingValue]}
- get
[A] (list: List[A], index: Int): A / {Exception[OutOfBounds]}
- foreach
[A] (l: List[A]) { f: (A) => Unit }: Unit / {}
- foreach
[A] (l: List[A]) { f: (A){Control} => Unit }: Unit / {}
- foreachIndex
[A] (list: List[A]) { f: (Int, A) => Unit }: Unit / {}
- foreachIndex
[A] (list: List[A]) { f: (Int, A){Control} => Unit }: Unit / {}
- map
[A, B] (l: List[A]) { f: (A) => B }: List[B] / {}
- collect
[A, B] (l: List[A]) { f: (A) => Option[B] }: List[B] / {}
- flatMap
[A, B] (l: List[A]) { f: (A) => List[B] }: List[B] / {}
- all
[A] (list: List[A]) { predicate: (A) => Bool }: Bool / {}
- any
[A] (list: List[A]) { predicate: (A) => Bool }: Bool / {}
- count
[A] (list: List[A]) { predicate: (A) => Bool }: Int / {}
- foldLeft
[A, B] (l: List[A], init: B) { f: (B, A) => B }: B / {}
- foldRight
[A, B] (l: List[A], init: B) { f: (A, B) => B }: B / {}
- sum
(list: List[Int]): Int / {}
- size
[A] (l: List[A]): Int / {}
- reverse
[A] (l: List[A]): List[A] / {}
- reverseOnto
[A] (l: List[A], other: List[A]): List[A] / {}
- append
[A] (l: List[A], other: List[A]): List[A] / {}
- join
[A] (lists: List[List[A]]): List[A] / {}
- join
[A] (lists: List[List[A]], between: List[A]): List[A] / {}
- take
[A] (l: List[A], n: Int): List[A] / {}
- drop
[A] (l: List[A], n: Int): List[A] / {}
- slice
[A] (list: List[A], start: Int, stopExclusive: Int): List[A] / {}
- splitAt
[A] (list: List[A], index: Int): Tuple2[List[A], List[A]] / {}
- updateAt
[A] (list: List[A], index: Int) { update: (A) => A }: List[A] / {}
- modifyAt
[A] (list: List[A], index: Int) { update: (A) => A }: List[A] / {Exception[OutOfBounds]}
- deleteAt
[A] (list: List[A], index: Int): List[A] / {}
- insert
[A] (list: List[A], index: Int, x: A): List[A] / {}
- replace
[A] (list: List[A], index: Int, x: A): List[A] / {}
- zip
[A, B] (left: List[A], right: List[B]): List[Tuple2[A, B]] / {}
- zipWith
[A, B, C] (left: List[A], right: List[B]) { combine: (A, B) => C }: List[C] / {}
- unzip
[A, B] (pairs: List[Tuple2[A, B]]): Tuple2[List[A], List[B]] / {}
- partition
[A] (l: List[A]) { pred: (A) => Bool }: Tuple2[List[A], List[A]] / {}
- sequences
[A] (list: List[A]) { compare: (A, A) => Bool }: List[List[A]] / {}
- ascending
[A] (current: A, rest: List[A]) { runDiff: (List[A]) => List[A] } { compare: (A, A) => Bool }: List[List[A]] / {}
- descending
[A] (current: A, run: List[A], rest: List[A]) { compare: (A, A) => Bool }: List[List[A]] / {}
- mergeAll
[A] (runs: List[List[A]]) { compare: (A, A) => Bool }: List[A] / {}
- mergePairs
[A] (runs: List[List[A]]) { compare: (A, A) => Bool }: List[List[A]] / {}
- merge
[A] (l1: List[A], l2: List[A]) { compare: (A, A) => Bool }: List[A] / {}
- sortBy
[A] (list: List[A]) { lessOrEqual: (A, A) => Bool }: List[A] / {}
- sort
(l: List[Int]): List[Int] / {}
- sort
(l: List[Double]): List[Double] / {}
- isSortedBy
[A] (list: List[A]) { lessOrEqual: (A, A) => Bool }: Bool / {}
- show
[A] (l: List[A]) { showA: (A) => String }: String / {}
- show
(l: List[Int]): String / {}
- show
(l: List[Double]): String / {}
- show
(l: List[Bool]): String / {}
- show
(l: List[String]): String / {}
- println
(l: List[Int]): Unit / {}
- println
(l: List[Double]): Unit / {}
- println
(l: List[Bool]): Unit / {}
- println
(l: List[String]): Unit / {}
Example usage: examples/stdlib/list
Immutable linked list for finite sequences of elements.
Create an empty list. O(1)
Create a list with one element. O(1)
Create a list of length `size` where all elements are `default`. O(size)
Create a list from a function `index` of given `size`. O(size)
Check if list is empty. O(1)
Check if list is nonempty. O(1)
Return the first element of a given list. Throws a `MissingValue` exception if it's empty. O(1)
Return all elements of a given list except the first element. Throws a `MissingValue` exception if it's empty. O(1)
Return the first element of a given list. Returns `None()` if it's empty. O(1)
Returns the last element of a given list. O(N)
Get the value at given index. O(N)
Traverse a list, applying the given action on every element. O(N)
Traverse a list, applying the given action on every element. O(N)
Traverse a list, applying the given action on every element and its (zero-based) index. O(N)
Traverse a list, applying the given action on every element and its (zero-based) index. O(N)
Map a function `f` over elements in a given list. O(N)
Map a function `f` over elements in a given list, keeping only the elements for which the function returned `Some(...)`, discarding the elements for which the function returned `None()`. O(N)
Map a function `f` over elements in a given list and concatenate the results. O(N)
Check if predicate is true for all elements of the given list. O(N)
Check if predicate is true for at least one element of the given list. O(N)
Count how many elements of a list satisfy the given predicate. Examples: ``` > ["AB", "BC", "A", "GG", "AAA"].count { x => x.length == 2 } 3 > [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].count { x => (x * 2) > 10 } 5 ``` O(N)
Fold a list using `f`, starting from the left given a starting value. O(N)
Fold a list using `f`, starting from the right given a starting value. O(N)
Sum the elements of the list. O(N)
Calculate the size of the list. O(N)
Reverse the list. O(N)
Reverse a list `l` and append `other` to it. Example: ``` > [1,2,3].reverseOnto([4,5,6]) [3,2,1,4,5,6] ``` O(|l|)
Concatenate list `l` with list `other`: Example: ``` > [1,2,3].append([4,5,6]) [1,2,3,4,5,6] ``` O(N)
Flatten a list of lists into a single list. Examples: ``` > [[1, 2, 3], [4, 5], [6]].join() [1, 2, 3, 4, 5, 6] > [[]].join() [] ``` O(N)
Flatten a list of lists into a single list, putting the `between` list in between each list in the input. Examples: ``` > [[100], [200, 300], [400]].join([1, 2, 3]) [100, 1, 2, 3, 200, 300, 1, 2, 3, 400] > [[]].join([1, 2, 3]) [] ``` O(N)
Take the first `n` elements of a given list. Examples: ``` > [1, 2, 3].take(2) [1, 2] > [1, 2, 3].take(0) [] > [1, 2, 3].take(3) [1, 2, 3] > [1, 2, 3].take(5) [1, 2, 3] > [1, 2, 3].take(-1) [] ``` O(n)
Drop the first `n` elements of a given list. Examples: ``` > [1, 2, 3].drop(2) [3] > [1, 2, 3].drop(0) [1, 2, 3] > [1, 2, 3].drop(3) [] > [1, 2, 3].drop(5) [] > [1, 2, 3].drop(-1) [1, 2, 3] ``` O(n)
Return a slice of a given list from the starting index (inclusive) to the given end index (exclusive). Examples: ``` > [1, 2, 3, 4, 5, 6].slice(1, 4) [2, 3, 4] > [1, 2, 3, 4, 5, 6].slice(1, 2) [2] > [1, 2, 3, 4, 5, 6].slice(1, 1) [] > [1, 2, 3, 4, 5, 6].slice(4, 1) [] > [1, 2, 3, 4, 5, 6].slice(-100, 100) [1, 2, 3, 4, 5, 6] ``` O(N)
Split the list at given index. Law: `val (l, r) = list.splitAt(i); l.append(r) === list` O(N)
Update the element at given index in the list using the `update` function. Returns the original list if the index is out of bounds. See: `modifyAt` Examples: ``` > [1, 2, 3].updateAt(1) { n => n + 100 } [1, 102, 3] > [1, 2, 3].updateAt(10) { n => n + 100 } [1, 2, 3] ``` O(N)
Modify the element at given index in the list using the `update` function. Throws `OutOfBounds` if the index is out of bounds. See: `updateAt` Examples: ``` > [1, 2, 3].modifyAt(1) { n => n + 100 } Some([1, 102, 3]) > [1, 2, 3].modifyAt(10) { n => n + 100 } None() ``` O(N)
Delete the element at given index in the list. Example: ``` > [1, 2, 3, 4].deleteAt(1) [1, 3, 4] > [1, 2, 3, 4].deleteAt(-1) [1, 2, 3, 4] > [1, 2, 3, 4].deleteAt(10) [1, 2, 3, 4] ``` O(N)
Add an element at given index in the list. Examples: ``` > [1, 2, 3].insert(-1, 0) [0, 1, 2, 3] > [1, 2, 3].insert(0, 0) [0, 1, 2, 3] > [1, 2, 3].insert(1, 0) [1, 0, 2, 3] > [1, 2, 3].insert(3, 0) [1, 2, 3, 0] > [1, 2, 3].insert(10, 0) [1, 2, 3, 0] ``` O(N)
Replace an element at given index in the list. Returns the original list when the index is out of bounds. Examples: ``` > [1, 2, 3].replace(0, 42) [42, 2, 3] > [1, 2, 3].replace(-1, 42) [1, 2, 3] > [1, 2, 3].replace(10, 42) [1, 2, 3] ``` O(N)
Produce a list of pairs from a pair of lists. The length of the result is the minimum of lengths of the two lists. Examples: ``` > zip([1, 2, 3], [100, 200, 300]) [(1, 100), (2, 200), (3, 300)] > zip([1, 2, 3], Nil[Int]()) [] > zip(Nil[Int](), [1, 2, 3]) [] > zip([1, 2, 3], [42]) [(1, 42)] ``` O(N)
Combine two lists with the given function. The length of the result is the minimum of lengths of the two lists. Examples: ``` > zipWith([1, 2, 3], [100, 200, 300]) { (a, b) => a + b } [101, 202, 303] > zipWith([1, 2, 3], Nil[Int]()) { (a, b) => a + b } [] > zipWith(Nil[Int](), [1, 2, 3]) { (a, b) => a + b } [] > zipWith([1, 2, 3], [42]) { (a, b) => a + b } [43] ``` O(N)
Produce a pair of lists from a list of pairs. Examples: ``` > [(1, 100), (2, 200), (3, 300)].unzip() ([1, 2, 3], [100, 200, 300]) ``` O(N)
Partition a given list into two lists. The left list contains the elements that satsify the predicate, the right list contains the elements that do not. O(N)
Splits the given list into monotonic segments (so a list of lists). Internally used in the mergesort 'sortBy' to prepare the to-be-merged partitions.
When in an ascending sequence, try to add `current` to `run` (if possible)
When in an descending sequence, try to add `current` to `run` (if possible)
Sort a list given a comparison operator (like less-or-equal!) The sorting algorithm is stable and should act reasonably well on partially sorted data. Examples: ``` > [1, 3, -1, 5].sortBy { (a, b) => a <= b } [-1, 1, 3, 5] > [1, 3, -1, 5].sortBy { (a, b) => a >= b } [5, 3, 1, -1] > [(1, 0), (0, 1), (-1, 1), (0, 0)].sortBy { (a, b) => a.first + a.second <= b.first + b.second } [(1, -1), (0, 0), (1, 0), (0, 1)] > Nil[Int]().sortBy { (a, b) => a <= b } [] ``` Note: this implementation is not stacksafe! (works for ~5M random elements just fine, but OOMs on ~10M random elements) O(N log N) worstcase
Sort a list of integers in an ascending order. See 'sortBy' for more details. O(N log N) worstcase
Sort a list of doubles in an ascending order. See 'sortBy' for more details. O(N log N) worstcase
Check if a list is sorted according to the given comparison function (less-or-equal). O(N)