Class: Immutable::List
- Inherits:
-
Object
- Object
- Immutable::List
- Includes:
- Consable
- Defined in:
- lib/immutable/list.rb
Overview
Immutable::List
represents an immutable list.
Immutable::List
is an abstract class and [] should be used instead of new. For example:
include Immutable
p List[] #=> List[]
p List[1, 2] #=> List[1, 2]
Immutable::Nil
represents an empty list, and Immutable::Cons
represents a cons cell, which is a node in a list. For example:
p Nil #=> List[]
p Cons[1, Cons[2, Nil]] #=> List[1, 2]
Direct Known Subclasses
Class Method Summary collapse
-
.empty ⇒ list
Returns an empty list.
Instance Method Summary collapse
-
#cons(x) ⇒ List
Adds a new element at the head of
self
. -
#empty? ⇒ true, false
Returns whether
self
is empty. -
#head ⇒ Object
Returns the first element of
self
. -
#init ⇒ List
Returns all the elements of
self
except the last one. -
#last ⇒ Object
Returns the last element of
self
. -
#pop ⇒ List
Same as #init.
-
#tail ⇒ List
Returns the elements after the head of
self
. -
#to_list ⇒ List
Returns
self
.
Methods included from Consable
#+, #drop, #drop_while, #filter, #flat_map, #flatten, included, #intercalate, #intersperse, #map, #prepend, #rev_map, #reverse, #subsequences, #take, #take_while, #transpose, #unshift, #zip, #zip_with
Methods included from Headable
#==, #[], #each, #each_index, #eql?, #fetch, #find, #first, #foldl, #foldl1, #foldr, #foldr1, #hash, #index, #inspect, #null?, #rindex, #shift
Methods included from Foldable
#foldl, #length, #product, #sum
Class Method Details
.empty ⇒ list
Returns an empty list.
26 27 28 |
# File 'lib/immutable/list.rb', line 26 def self.empty Nil end |
Instance Method Details
#cons(x) ⇒ List
Adds a new element at the head of self
.
34 35 36 |
# File 'lib/immutable/list.rb', line 34 def cons(x) Cons[x, self] end |
#empty? ⇒ true, false
Returns whether self
is empty.
81 82 83 |
# File 'lib/immutable/list.rb', line 81 def empty? # this method should be overriden end |
#head ⇒ Object
Returns the first element of self
. If self
is empty, Immutable::EmptyError
is raised.
42 43 44 |
# File 'lib/immutable/list.rb', line 42 def head # this method should be overriden end |
#init ⇒ List
Returns all the elements of self
except the last one. If self
is empty, Immutable::EmptyError
is raised.
67 68 69 |
# File 'lib/immutable/list.rb', line 67 def init # this method should be overriden end |
#last ⇒ Object
Returns the last element of self
. If self
is empty, Immutable::EmptyError
is raised.
50 51 52 |
# File 'lib/immutable/list.rb', line 50 def last # this method should be overriden end |
#tail ⇒ List
Returns the elements after the head of self
. If self
is empty, Immutable::EmptyError
is raised.
58 59 60 |
# File 'lib/immutable/list.rb', line 58 def tail # this method should be overriden end |