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)
-
+ (list) empty
Returns an empty list.
Instance Method Summary (collapse)
-
- (List) cons(x)
Adds a new element at the head of self.
-
- (true, false) empty?
Returns whether self is empty.
-
- (Object) head
Returns the first element of self.
-
- (List) init
Returns all the elements of self except the last one.
-
- (Object) last
Returns the last element of self.
-
- (List) pop
Same as #init.
-
- (List) tail
Returns the elements after the head of self.
-
- (List) to_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
+ (list) empty
Returns an empty list.
26 27 28 |
# File 'lib/immutable/list.rb', line 26 def self.empty Nil end |
Instance Method Details
- (List) cons(x)
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 |
- (true, false) empty?
Returns whether self is empty.
81 82 83 |
# File 'lib/immutable/list.rb', line 81 def empty? # this method should be overriden end |
- (Object) head
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 |
- (List) init
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 |
- (Object) last
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 |
- (List) tail
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 |
- (List) to_list
Returns self.
88 89 90 |
# File 'lib/immutable/list.rb', line 88 def to_list self end |