Class: Immutable::Cons

Inherits:
List
  • Object
show all
Defined in:
lib/immutable/list.rb

Overview

Immutable::Cons represents a cons cell.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from List

#cons, empty, #pop, #to_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

#headObject (readonly)

Returns the value of attribute head.



121
122
123
# File 'lib/immutable/list.rb', line 121

def head
  @head
end

#tailObject (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.

Returns:

  • (Cons)

    the new list.



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

Returns:

  • (Boolean)


157
158
159
# File 'lib/immutable/list.rb', line 157

def empty?
  false
end

#initObject



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

#lastObject



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