Class: Immutable::Cons
Overview
Immutable::Cons
represents a cons cell.
Instance Attribute Summary collapse
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Class Method Summary collapse
-
.[](head, tail = Nil) ⇒ Cons
Creates a new list obtained by prepending
head
to the listtail
.
Instance Method Summary collapse
- #empty? ⇒ Boolean
- #init ⇒ Object
-
#initialize(head, tail = Nil) ⇒ Cons
constructor
Creates a list obtained by prepending
head
to the listtail
. - #last ⇒ Object
Methods inherited from List
Methods included from Consable
#+, #cons, #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, #to_list
Methods included from Foldable
#foldl, #length, #product, #sum
Constructor Details
#initialize(head, tail = Nil) ⇒ Cons
Creates a list obtained by prepending head
to the list tail
.
112 113 114 115 |
# File 'lib/immutable/list.rb', line 112 def initialize(head, tail = Nil) @head = head @tail = tail end |
Instance Attribute Details
#head ⇒ Object (readonly)
Returns the value of attribute head.
121 122 123 |
# File 'lib/immutable/list.rb', line 121 def head @head end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
139 140 141 |
# File 'lib/immutable/list.rb', line 139 def tail @tail end |
Class Method Details
.[](head, tail = Nil) ⇒ Cons
Creates a new list obtained by prepending head
to the list tail
.
107 108 109 |
# File 'lib/immutable/list.rb', line 107 def self.[](head, tail = Nil) self.new(head, tail) end |
Instance Method Details
#empty? ⇒ Boolean
157 158 159 |
# File 'lib/immutable/list.rb', line 157 def empty? false end |
#init ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/immutable/list.rb', line 145 def init if @tail.empty? Nil else Cons[@head, @tail.init] end end |
#last ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/immutable/list.rb', line 127 def last if @tail.empty? @head else @tail.last end end |