Module: Immutable::Headable

Includes:
Enumerable, Foldable
Included in:
Consable, Queue
Defined in:
lib/immutable/headable.rb

Instance Method Summary collapse

Methods included from Foldable

#length, #product, #sum

Instance Method Details

#==(x) ⇒ true, false

Returns whether self equals to x.

Parameters:

  • x (Object)

    the object to compare.

Returns:

  • (true, false)

    true if self equals to x; otherwise, false.



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.

Returns:

  • (Object)

    the nth element.



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.

Yields:

  • (element)

Yield Returns:

  • (self)

Returns:

  • (Enumerator)


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.

Returns:

  • (true, false)

    true if self is empty; otherwise, false.

Raises:

  • (NotImplementedError)


50
51
52
# File 'lib/immutable/headable.rb', line 50

def empty?
  raise NotImplementedError
end

#eql?(x) ⇒ Boolean

Returns:

  • (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.

Parameters:

  • ifnone (#call, nil) (defaults to: nil)

Yields:

  • (element)

Yield Returns:

  • (Object)

    the found element.

Returns:

  • (Enumerator)


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

#firstObject

Same as #head. This method just calls #head.

Returns:

  • (Object)

    the first element of self.



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

Parameters:

  • e (Object)

    the start value.

Returns:

  • (Object)

    the reduced value.



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.

Returns:

  • (Object)

    the reduced value.



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

Parameters:

  • e (Object)

    the start value.

Returns:

  • (Object)

    the reduced value.



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.

Returns:

  • (Object)

    the reduced value.



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

#hashInteger

Returns:

  • (Integer)


103
104
105
# File 'lib/immutable/headable.rb', line 103

def hash
  to_a.hash
end

#headObject

Returns the first element of self. If self is empty, Immutable::EmptyError is raised. A class including Immutable::Headable must implement this method.

Returns:

  • (Object)

    the first element of self.

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/immutable/headable.rb', line 19

def head
  raise NotImplementedError
end

#inspectString

Returns a string containing a human-readable representation of self.

Returns:

  • (String)

    a string 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

Same as #empty?. This method just calls #empty?.

Returns:

  • (true, false)

    true if self is empty; otherwise, false.



57
58
59
# File 'lib/immutable/headable.rb', line 57

def null?
  empty?
end

#shiftHeadable

Same as #tail. This method just calls #tail.

Returns:

  • (Headable)

    the elements after the head of self.



42
43
44
# File 'lib/immutable/headable.rb', line 42

def shift
  tail
end

#tailHeadable

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.

Returns:

  • (Headable)

    the elements after the head of self.

Raises:

  • (NotImplementedError)


35
36
37
# File 'lib/immutable/headable.rb', line 35

def tail
  raise NotImplementedError
end

#to_listList

Converts self to a list.

Returns:

  • (List)

    a list.



225
226
227
# File 'lib/immutable/headable.rb', line 225

def to_list
  foldr(List[]) { |x, xs| Cons[x, xs] }
end