- Creating Lists
- Reading Lists
- Transforming Lists
- adding sources
- transforms
- into destinations
List.empty(): List<any>
returns a new empty List
fantasy-land compatibility for Monoid
Example:
var none = List.empty();
var nada = none.empty();
List.of<T>(value: T): List<T>
creates a list from values provided
example:
var one = List.of(1)
List.from(list: Array<T>|List<T>|Iterable<T>): List<T>
creates a list from another collection
example:
var one = List.of(1)
List.range(start: number, end: number): List<number>
example:
var nums = List.range(0, 5);
//nums == [0,1,2,3,4]
List.times<T>(n: number, function(i: number): T): List<T>
invokes a callback n times and builds into a List
example:
var nums = List.range(0, 5)
//nums == [0,1,2,3,4]
creates a builder from the current list see also: List.Builder to create an empty Builder
nth<T>(n: number, notFound: any): T|notFound
example
var nums = List.range(0, 5)
var resulta = nums.nth(2);
var resultb = nums.nth(7);
var resultc = nums.nth(7, Maybe.Nothing);
//nums == [0,1,2,3,4]
//resulta = 2
//resultb = undefined
//resultc = Nothing
nth<T>(index: number): Maybe<T>
returns the value at the provided index, wrapped in a Maybe.Just if in bounds or a Maybe.Nothing if out of bound
example
var nums = List.range(0, 5)
var maybe_a = nums.nth(2);
var maybe_b = nums.nth(7);
//nums == [0,1,2,3,4]
//maybe_a == Just(2)
//maybe_b == Nothing
indexOf<T>(value: T): number
includes(value: any): boolean
find<T>(function(value: T): boolean): number
iterator(start: number, end: number): RangedIterator
iterator(start: number, end: number): RangedIterator
returns a iterator for the current collection
append<T>(value: T): List<T>
alias for List.append
appendAll<T>(iterable: Array<T>|List<T>|Iterable<T>): List<T>
fantasy-land compatibility for Semigroup alias for List.appendAll
prepend<T>(value: T): List<T>
add an element to the beginning of the list, returning a new list
alias for List.prepend
drop(n: number): List
take(n: number): List
drop(index: number): List
alias for List.update
slice(from: number, to: number): List
removeAt(index: number): List
removeAt<T>(value: T): List<T>
insertAt<T>(index: number, value: T): List<T>
reduce<T>(callback: function(accumulator: W, next: T): W, seed: W): List<T>
reduceRight<T>(callback: function(accumulator: W, next: T): W, seed: W): List<T>
foldl<T>(callback: function(next: T, accumulator: W): W, seed: W): List<T>
foldr<T>(callback: function(next: T, accumulator: W): W, seed: W): List<T>
filter<T>(callback: function(value: T):Boolean): List<T>
map<T>(callback: function(value: T): W): List<W>
every(callback: function(value: T):Boolean): Boolean
some(callback: function(value: T):Boolean): Boolean
sortWith<T>(function(a: T, b: T): number): List<T>
sorts the list using a provided comparison function
the callback should return one of : 0 : if items are equal 1 : if a is greater than b -1: if b is greater than a
sort(): List
sort and return a new list using a Natural sort order
intersperse<T>(separator: T): List<T>
join(separator: string): string
flatten(): List
flatMap<T|Array>(callback: function(value: T): W): List<W>
fantasy-land compatibility for Chain alias for List.flatMap
fantasy-land compatibility for Applicative
fantasy-land compatibility for Traversible
fantasy-land compatibility for Traversible
create a new empty Builder
Builders offer faster batched iteration and transformations over multiple collections (like an iterator of iterators) with a fluent API.
appendAll<T>(list: Array<T>): Builder<T>
add a collection to the source list. Sources are lazily iterated over when builder exectutes a "terminating" statement. Any collection type can be used including:
- native Arrays
- Lists
- LinkedLists (an internal type)
- Iterators
Example:
var result = List.Builder()
.appendAll(myArray)
.appendAll(myList)
.appendAll(myIterable)
.toList()
alias for appendAll()
append<T>(value: T): Builder<T>
add a single item to the sources list.
Example:
var result = List.Builder()
.append(1)
.append(2)
.append(3)
.toList()
// result == [1,2,3]
alias for append()
prepend<T>(value: T): Builder<T>
add a single item to the beginning of sources list.
Example:
var result = List.Builder()
.prepend(3)
.prepend(2)
.prepend(1)
.toList()
// result == [1,2,3]
alias for prepend()
map<T>(callback: function(value:T): W): Builder<W>
calls a provided callback function once for each element in an sources, in order, and returns a new replacement element.
Example:
const plusOne = (x) => x + 1
var result = List.Builder()
.appendAll([1,2,3])
.map(plusOne)
.toList()
// result == [2,3,4]
filter<T>(callback: function(value:T): Boolean): Builder<T>
calls the provided callback function once for each element in the sources, in order, and keeps the element if callback returns true
drop(n: number): Builder
accepts a provided number, removing the first n elements from the sources, leaving the remaining
dropWhile(callback: function(value:T): Boolean): Builder
accepts a provided callback, removing elements until the callback returns true, leaving the remaining elements untouched
take(n: number): Builder
accepts a provided number, removing the first n elements from the sources, leaving the remaining
takeWhile(callback: function(value:T): Boolean): Builder
accepts a provided callback, keeping elements until the callback returns false, leaving the remaining elements untouched
flatten(): Builder
merges all elements recursively, if they are an Array, List or Iterable, into a single list
var result = List.Builder()
.appendAll([[1,2,3], [4,5,6], [7,8,9]])
.flatten()
.toList()
// result == [1,2,3,4,5,6,7,8,9]
map<T|Array>(callback: function(value:T): W): Builder<W>
transform each element or sub-element(if iterable), then merges into resulting list
const plusOne = (x) => x + 1
var result = List.Builder()
.appendAll([[1,2,3], [4,5,6], [7,8,9]])
.flatMap(plusOne)
.toList()
// result == [2,3,4,5,6,7,8,9,10]
intersperse(value: any): Builder
Accepts a single argument an inserts it between every element in the array. Will not insert as first or last item
var result = List.Builder()
.appendAll([1,2,3])
.intersperse("-")
.toList()
// result == [1, "-", 2, "-", 3]
unique(): Builder
ensures all elements only occur once in the result
var result = List.Builder()
.appendAll([1,2,3,1,2,3,3,3])
.unique()
.toList()
// result == [1, 2, 3]
scan<T>(callback: function(accumulator: W, next: T): W, seed: W): Builder<W>
Accepts an callback function and an optionally seed value. incrementally reduces each element in order. non-terminal (i.e. does not produce the final builder result)
reduce<T>(callback: function(accumulator: W, next: T): W, seed: W): W
Accepts a reducer callback and an option seed value. Terminal. Returns the result of the builder run through the callback.
join(separator: string): string
Returns the result of the builder as a native Array. Accepts an optional separator. Terminal.
toArray<T>(): Array<T>
Returns the result of the builder as an native Array. Accepts no input arguments. Terminal
toList<T>(): List<T>
Returns the result of the builder as an immutable List. Accepts no input arguments. Terminal
every<T>(callback: function(value:T): Boolean): Boolean
Accepts a callback function, returning a boolean if all elements pass Terminal.
some<T>(callback: function(value:T): Boolean): Boolean
Accepts a callback function, returning a boolean if any elements pass. Terminal