struct Monads::List(T)
- Monads::List(T)
- Monads::Monad(T)
- Monads::Functor(T)
- Struct
- Value
- Object
Included Modules
- Comparable(Monads::List(T))
- Indexable(T)
- Iterator(T)
Defined in:
monads/list.crConstructors
Class Method Summary
Instance Method Summary
- #+(rhs : List) : List
-
#<=>(rhs : List)
The comparison operator.
- #bind(lambda : T -> List(U)) forall U
-
#empty?
Returns
true
ifself
is empty,false
otherwise. - #fmap(lambda : T -> U) forall U
- #head : Maybe(T)
- #inspect(io)
-
#join(sep = "")
Returns a
String
created by concatenating the elements in the collection, separated by separator (defaults to none). -
#last
Returns the last element of
self
if it's not empty, or raisesIndexError
. -
#next
Returns the next element in this iterator, or
Iterator::Stop::INSTANCE
if there are no more elements. -
#permutations
Returns an
Array
with all possible permutations of size ofself
. - #reverse : List(T)
-
#size
Returns the number of elements in the collection.
- #sort : List(T)
- #sort(&block : T, T -> U) : List(T) forall U
- #sort_by(&block : T -> _) : List(T)
- #subsequences
- #tail : List(T)
-
#to_s
Returns a nicely readable and concise string representation of this object, typically intended for users.
-
#unsafe_fetch(index : Int)
Returns the element at the given index, without doing any bounds check.
Macro Summary
-
[](*args)
create new List
Instance methods inherited from struct Monads::Monad(T)
>>(other : Monad(U)) forall U
>>,
|(other : _ -> Monad(U)) forall U
|,
bind(lambda : T -> Monad(U)) forall Ubind(&block : T -> Monad(U)) forall U bind
Constructor methods inherited from struct Monads::Monad(T)
new
new,
return(v : T) : self
return
Instance methods inherited from struct Monads::Functor(T)
fmap(lambda : T -> U)fmap(&block : T -> U) forall U fmap, initialize initialize
Constructor methods inherited from struct Monads::Functor(T)
new
new
Constructor Detail
Class Method Detail
Instance Method Detail
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]
Returns true
if self
is empty, false
otherwise.
([] of Int32).empty? # => true
([1]).empty? # => false
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"
Returns the last element of self
if it's not empty, or raises IndexError
.
([1, 2, 3]).last # => 3
([] of Int32).last # raises IndexError
Returns the next element in this iterator, or Iterator::Stop::INSTANCE
if there
are no more elements.
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) # => []
Returns the number of elements in the collection.
[1, 2, 3, 4].size # => 4
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
.
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.