struct

Monads::List(T)

Inherits Iterator / Enumerable / Indexable / Enumerable / Iterable / Comparable / Monads::Monad / Monads::Functor / Struct / Value / Object

Constructors

new(value : Array(T))
Source

Class methods

return(value)
Source

Instance methods

+(other : List) : List
Source
<=>(other : List)

The comparison operator. Returns 0 if the two objects are equal, a negative number if this object is considered less than other, a positive number if this object is considered greater than other, or nil if the two objects are not comparable.

Subclasses define this method to provide class-specific ordering.

The comparison operator is usually used to sort values:

# Sort in a descending way:
[3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]

# Sort in an ascending way:
[3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
Source
bind(lambda : T -> List(U)) forall U
Source
empty?

Returns true if self does not contain any element.

([] of Int32).empty? # => true
([1]).empty?         # => false
[nil, false].empty?  # => false
  • #present? returns the inverse.
Source
fmap(lambda : T -> U) forall U
Source
head
Source
inspect(io)
Source
join(sep = "")

Returns a String created by concatenating the elements in the collection, separated by separator (defaults to none).

[1, 2, 3, 4, 5].join(", ") # => "1, 2, 3, 4, 5"
Source
last

Returns the last element of self if it's not empty, or raises IndexError.

([1, 2, 3]).last   # => 3
([] of Int32).last # raises IndexError
Source
next

Returns the next element in this iterator, or Iterator::Stop::INSTANCE if there are no more elements.

Source
permutations

Returns an Array with all possible permutations of size of self.

a = [1, 2, 3]
a.permutations    # => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutations(1) # => [[1],[2],[3]]
a.permutations(2) # => [[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
a.permutations(3) # => [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
a.permutations(0) # => [[]]
a.permutations(4) # => []
Source
reverse
Source
size

Returns the number of elements in the collection.

[1, 2, 3, 4].size # => 4
Source
sort
Source
sort
Source
sort_by
Source
subsequences
Source
tail
Source
to_s

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.

Source
unsafe_fetch(index : Int)

Returns the element at the given index, without doing any bounds check.

Indexable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.

Clients never invoke this method directly. Instead, they access elements with #[](index) and #[]?(index).

This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.

Source

Macros

[](*args)

create new List

Monads::List[1, 2, 3] == Monads::List.new([1, 2, 3])
Source