List
type List<X>
Implements Monoid, Blankable, Enumerable, Foldable, Truthyable.
Methods
first
first : X?
Returns the first element wrapped in Just, or None if the list is empty.
Examples
[1, 2, 3].first # => Just(1)
[].first # => None
second
second : X?
Returns the second element wrapped in Just, or None if the list has fewer than two elements.
Examples
[1, 2, 3].second # => Just(2)
[1].second # => None
third
third : X?
Returns the third element wrapped in Just, or None if the list has fewer than three elements.
Examples
[1, 2, 3].third # => Just(3)
[1, 2].third # => None
rest
rest : [X]
Returns all elements after the first. Returns [] for an empty or single-element list.
Examples
[1, 2, 3].rest # => [2, 3]
[1].rest # => []
[].rest # => []
last
last : X?
Returns the last element wrapped in Just, or None if the list is empty.
Examples
[1, 2, 3].last # => Just(3)
[].last # => None
count Foldable
count : Integer
Returns the number of elements. When given a predicate, returns the count of elements for which it holds.
Examples
[1, 2, 3].count # => 3
[1, 2, 3, 4].count(~even?) # => 2
count(pred: (X -> Bool)) -> Integer
count(pred) is provided by the Enumerable trait.
empty?
empty? : Bool
Returns true if the list contains no elements.
Examples
[].empty? # => true
[1, 2].empty? # => false
find Foldable
find(pred: (X -> Bool)) -> X?
Returns the first element satisfying the predicate wrapped in Just, or None if no element matches.
find/any?/all? are provided by the Enumerable trait.
Parameters
pred
Examples
[1, 2, 3].find { |x| x > 1 } # => Just(2)
[1, 2, 3].find { |x| x > 9 } # => None
any? Foldable
any?(pred: (X -> Bool)) -> Bool
Returns true if at least one element satisfies the predicate.
Parameters
pred
Examples
[1, 2, 3].any? { |x| x > 2 } # => true
[1, 2, 3].any? { |x| x > 9 } # => false
all? Foldable
all?(pred: (X -> Bool)) -> Bool
Returns true if every element satisfies the predicate.
Parameters
pred
Examples
[1, 2, 3].all? { |x| x > 0 } # => true
[1, 2, 3].all? { |x| x > 1 } # => false
map Enumerable
map(f: (X -> Y)) -> [Y]
Transforms each element by applying f.
map/filter/each are provided by the Enumerable trait (in terms of reduce).
Parameters
f
Examples
[1, 2, 3].map { |x| x * 2 } # => [2, 4, 6]
filter Enumerable
filter(pred: (X -> Bool)) -> [X]
Returns a new list containing only the elements for which pred is true.
Parameters
pred
Examples
[1, 2, 3, 4].filter { |x| x.even? } # => [2, 4]
reject
reject(pred: (X -> Bool)) -> [X]
Returns a new list with all elements for which pred is true removed. The inverse of filter.
Parameters
pred
Examples
[1, 2, 3, 4].reject { |x| x.even? } # => [1, 3]
each Foldable
each(f: (X -> Void)) -> Void
Calls f with each element for its side effects. Returns unit.
Parameters
f
Examples
[1, 2, 3].each { |x| IO.printLine(x) }
reduce Enumerable Foldable
reduce(acc: A, f: (A -> X -> A)) -> A
Folds the list from the left, starting with acc and combining each element via f.
Parameters
acc- initial accumulator value
f
Examples
[1, 2, 3].reduce(0) { |acc, x| acc + x } # => 6
[1, 2, 3].reduce(1) { |acc, x| acc * x } # => 6
flatMap Enumerable
flatMap(f: (X -> [Y])) -> [Y]
Maps each element to a list and concatenates the results.
Parameters
f
Examples
[1, 2, 3].flatMap { |n| [n, n * 10] } # => [1, 10, 2, 20, 3, 30]
at
at(i: Integer) -> X?
Returns the element at position i (0-based) wrapped in Just, or None if the index is out of range.
Parameters
i
Examples
[10, 20, 30].at(1) # => Just(20)
[10, 20, 30].at(9) # => None
contains?
contains?(elem: X) -> Bool
Returns true if elem is present in the list.
Parameters
elem
Examples
[1, 2, 3].contains?(2) # => true
[1, 2, 3].contains?(9) # => false
indexOf
indexOf(elem: X) -> Integer?
Returns true if the first index at which elem appears, wrapped in Just, or None if the element is not present.
Parameters
elem
Examples
[10, 20, 30].indexOf(20) # => Just(1)
[10, 20, 30].indexOf(99) # => None
findIndex
findIndex(pred: (X -> Bool)) -> Integer?
Returns the index of the first element satisfying pred, wrapped in Just, or None if none does. The predicate counterpart of indexOf, which searches by value.
Parameters
pred
Examples
[10, 25, 30].findIndex { |n| n > 20 } # => Just(1)
[10, 25, 30].findIndex { |n| n > 99 } # => None
takeWhile
takeWhile(pred: (X -> Bool)) -> [X]
Returns the longest leading run of elements satisfying pred. Stops at the first element that does not, so it is not filter — later matches are dropped with everything after the first failure.
Parameters
pred
Examples
[1, 2, 9, 1].takeWhile { |n| n < 5 } # => [1, 2]
dropWhile
dropWhile(pred: (X -> Bool)) -> [X]
Returns what is left after takeWhile — everything from the first element that does not satisfy pred onwards.
Parameters
pred
Examples
[1, 2, 9, 1].dropWhile { |n| n < 5 } # => [9, 1]
uniq
uniq : [X]
Returns a new list with duplicate elements removed, preserving the first occurrence of each element.
Examples
[1, 2, 3, 2, 1].uniq # => [1, 2, 3]
partition
partition(pred: (X -> Bool)) -> ([X], [X])
Splits the list into two lists: those for which pred is true (first) and those for which it is false (second).
Parameters
pred
Examples
[1, 2, 3, 4].partition { |n| n.even? } # => ([2, 4], [1, 3])
collect Enumerable
collect(f: (X -> Y?)) -> [Y]
Maps each element through f (which returns an Optional), keeping and unwrapping the Just(y) results and dropping None. Filter + map fused.
Parameters
f
Examples
[1, 2, 3, 4].collect { |n| n > 2 then Just(n * 10) else None } # => [30, 40]
take
take(n: Integer) -> [X]
Returns the first n elements.
Parameters
n
Examples
[1, 2, 3, 4, 5].take(3) # => [1, 2, 3]
drop
drop(n: Integer) -> [X]
Drops the first n elements.
Parameters
n
Examples
[1, 2, 3, 4, 5].drop(2) # => [3, 4, 5]
push
push(x: X) -> [X]
Returns a new list with x appended at the end.
Parameters
x
Examples
[1, 2].push(3) # => [1, 2, 3]
reverse
reverse : [X]
Returns the elements in reverse order.
Examples
[1, 2, 3].reverse # => [3, 2, 1]
zip
zip(other: [Y]) -> [(X, Y)]
Pairs each element of this list with the corresponding element of other. Stops at the end of the shorter list.
Parameters
other
Examples
[1, 2, 3].zip(["a", "b", "c"]) # => [(1, "a"), (2, "b"), (3, "c")]
flatten
flatten : [X]
Flattens exactly one level of nesting. Only valid on lists of lists.
Examples
[[1, 2], [3, 4]].flatten # => [1, 2, 3, 4]
sort
sort : [X]
Returns the elements sorted in ascending natural order.
Examples
[3, 1, 2].sort # => [1, 2, 3]
sort(comp: (X -> X -> Bool)) -> [X]
Returns the elements sorted using a custom comparator. comp should return true when its first argument should come before its second.
Parameters
comp
Examples
[3, 1, 2].sort { |a, b| a > b } # => [3, 2, 1]
min
min(f: (X -> Y)) -> X?
Returns the element with the smallest f key wrapped in Just, or None for an empty list.
Parameters
f
Examples
["hey", "hi"].min { |s| s.count } # => Just("hi")
max
max(f: (X -> Y)) -> X?
Returns the element with the largest f key wrapped in Just, or None for an empty list.
Parameters
f
Examples
["hey", "hi"].max { |s| s.count } # => Just("hey")
sum
sum(f: (X -> Number)) -> Number
Maps each element through f and sums the results.
Parameters
f
Examples
["hello", "hi"].sum { |s| s.count } # => 7
join
join(sep: String) -> String
join : String
Receiver is [X], not [String | Char]: the intrinsic renders whatever it is given, so [1, 2].join(", ") is "1, 2" on both backends. The narrower receiver only ever fired once element types became precise enough to catch it — until then map/filter handed join an unknown element type and the restriction checked nothing. Concatenates all elements, placing sep between each adjacent pair. With no separator the elements are joined directly.
Parameters
sep
Examples
["hello", "world", "kex"].join(", ") # => "hello, world, kex"
["a", "b", "c"].join # => "abc"
On [Number]
sum
sum : Number
Sums all elements. Returns 0 for an empty list.
Examples
[1, 2, 3].sum # => 6
[].sum # => 0
product
product : Number
Multiplies all elements. Returns 1 for an empty list.
Examples
[1, 2, 3].product # => 6
[].product # => 1
product(f: (X -> Number)) -> Number
Maps each element through f and multiplies the results.
Parameters
f
Examples
[[1,2],[3,4]].product { |pair| pair.first.or(1) } # => 3
min
min : X?
Returns the smallest element wrapped in Just, or None for an empty list.
Examples
[3, 1, 2].min # => Just(1)
[].min # => None
max
max : X?
Returns the largest element wrapped in Just, or None for an empty list.
Examples
[3, 1, 2].max # => Just(3)
[].max # => None
From Monoid
repeat- Combines this value with itself
ntimes.
From Enumerable
mapIndexed- Transforms each item together with its 0-based position, collecting the results into a list.
From Foldable
eachIndexed- Applies
fto each item together with its 0-based position, for side effects.