Module: Immutable::Headable
Instance Method Summary collapse
-
#==(x) ⇒ true, false
Returns whether
selfequals tox. -
#[](n) ⇒ Object
Returns the nth element of
self. -
#each {|element| ... } ⇒ Enumerator
Calls
blockonce for each element inself. -
#empty? ⇒ true, false
Returns whether
selfis empty. - #eql?(x) ⇒ Boolean
-
#find(ifnone = nil) {|element| ... } ⇒ Enumerator
(also: #detect)
Returns the first element in
selffor which the given block evaluates to true. -
#first ⇒ Object
Same as #head.
-
#foldl(e, &block) ⇒ Object
Reduces
selfusingblockfrom left to right. -
#foldl1(&block) ⇒ Object
Reduces
selfusingblockfrom left to right. -
#foldr(e, &block) ⇒ Object
Reduces
selfusingblockfrom right to left. -
#foldr1(&block) ⇒ Object
Reduces
selfusingblockfrom right to left. - #hash ⇒ Integer
-
#head ⇒ Object
Returns the first element of
self. -
#inspect ⇒ String
Returns a string containing a human-readable representation of
self. -
#null? ⇒ true, false
Same as #empty?.
-
#shift ⇒ Headable
Same as #tail.
-
#tail ⇒ Headable
Returns the elements after the head of
self. -
#to_list ⇒ List
Converts
selfto a list.
Methods included from Foldable
Instance Method Details
#==(x) ⇒ true, false
Returns whether self equals to x.
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/immutable/headable.rb', line 78 def ==(x) if !x.is_a?(self.class) false else if empty? x.empty? else !x.empty? && head == x.head && tail == x.tail end end end |
#[](n) ⇒ Object
Returns the nth element of self. If n is out of range, nil is returned.
212 213 214 215 216 217 218 219 220 |
# File 'lib/immutable/headable.rb', line 212 def [](n) if n < 0 || empty? nil elsif n == 0 head else tail[n - 1] end end |
#each {|element| ... } ⇒ Enumerator
Calls block once for each element in self.
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/immutable/headable.rb', line 111 def each(&block) return to_enum unless block_given? unless empty? yield(head) tail.each(&block) end self end |
#empty? ⇒ true, false
Returns whether self is empty. A class including Immutable::Headable must implement this method.
50 51 52 |
# File 'lib/immutable/headable.rb', line 50 def empty? raise NotImplementedError end |
#eql?(x) ⇒ Boolean
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/immutable/headable.rb', line 90 def eql?(x) if !x.is_a?(self.class) false else if empty? x.empty? else !x.empty? && head.eql?(x.head) && tail.eql?(x.tail) end end end |
#find(ifnone = nil) {|element| ... } ⇒ Enumerator Also known as: detect
Returns the first element in self for which the given block evaluates to true. If such an element is not found, it returns nil.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/immutable/headable.rb', line 188 def find(ifnone=nil, &block) return to_enum(__callee__, ifnone) unless block_given? if empty? if ifnone.nil? nil else ifnone.call end else if yield(head) head else tail.find(ifnone, &block) end end end |
#first ⇒ Object
26 27 28 |
# File 'lib/immutable/headable.rb', line 26 def first head end |
#foldl(e, &block) ⇒ Object
Reduces self using block from left to right. e is used as the starting value. For example:
List[1, 2, 3].foldl(9) { |x, y| x + y } #=> ((9 - 1) - 2) - 3 = 3
160 161 162 163 164 165 166 |
# File 'lib/immutable/headable.rb', line 160 def foldl(e, &block) if empty? e else tail.foldl(yield(e, head), &block) end end |
#foldl1(&block) ⇒ Object
Reduces self using block from left to right. If self is empty, Immutable::EmptyError is raised.
172 173 174 175 176 177 178 |
# File 'lib/immutable/headable.rb', line 172 def foldl1(&block) if empty? raise EmptyError else tail.foldl(head, &block) end end |
#foldr(e, &block) ⇒ Object
Reduces self using block from right to left. e is used as the starting value. For example:
List[1, 2, 3].foldr(9) { |x, y| x + y } #=> 1 - (2 - (3 - 9)) = -7
129 130 131 132 133 134 135 |
# File 'lib/immutable/headable.rb', line 129 def foldr(e, &block) if empty? e else yield(head, tail.foldr(e, &block)) end end |
#foldr1(&block) ⇒ Object
Reduces self using block from right to left. If self is empty, Immutable::EmptyError is raised.
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/immutable/headable.rb', line 141 def foldr1(&block) if empty? raise EmptyError else if tail.empty? head else yield(head, tail.foldr1(&block)) end end end |
#hash ⇒ Integer
103 104 105 |
# File 'lib/immutable/headable.rb', line 103 def hash to_a.hash end |
#head ⇒ Object
Returns the first element of self. If self is empty, Immutable::EmptyError is raised. A class including Immutable::Headable must implement this method.
19 20 21 |
# File 'lib/immutable/headable.rb', line 19 def head raise NotImplementedError end |
#inspect ⇒ String
Returns a string containing a human-readable representation of self.
64 65 66 67 68 69 70 71 |
# File 'lib/immutable/headable.rb', line 64 def inspect if empty? class_name + "[]" else class_name + "[" + head.inspect + tail.foldl("") {|x, y| x + ", " + y.inspect } + "]" end end |
#null? ⇒ true, false
57 58 59 |
# File 'lib/immutable/headable.rb', line 57 def null? empty? end |
#shift ⇒ Headable
42 43 44 |
# File 'lib/immutable/headable.rb', line 42 def shift tail end |
#tail ⇒ Headable
Returns the elements after the head of self. If self is empty, Immutable::EmptyError is raised. A class including Immutable::Headable must implement this method.
35 36 37 |
# File 'lib/immutable/headable.rb', line 35 def tail raise NotImplementedError end |
#to_list ⇒ List
Converts self to a list.
225 226 227 |
# File 'lib/immutable/headable.rb', line 225 def to_list foldr(List[]) { |x, xs| Cons[x, xs] } end |