Class: Klam::Cons

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/klam/cons.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hd, tl) ⇒ Cons

Returns a new instance of Cons.



6
7
8
9
# File 'lib/klam/cons.rb', line 6

def initialize(hd, tl)
  @hd = hd
  @tl = tl
end

Instance Attribute Details

#hdObject (readonly)

Returns the value of attribute hd.



4
5
6
# File 'lib/klam/cons.rb', line 4

def hd
  @hd
end

#tlObject (readonly)

Returns the value of attribute tl.



4
5
6
# File 'lib/klam/cons.rb', line 4

def tl
  @tl
end

Instance Method Details

#==(other) ⇒ Object



11
12
13
# File 'lib/klam/cons.rb', line 11

def ==(other)
  other.instance_of?(Cons) && other.hd == @hd && other.tl == @tl
end

#eachObject



19
20
21
22
23
24
25
# File 'lib/klam/cons.rb', line 19

def each
  x = self
  until x == Klam::Primitives::Lists::EMPTY_LIST
    yield x.hd
    x = x.tl
  end
end

#hashObject



15
16
17
# File 'lib/klam/cons.rb', line 15

def hash
  [@hd, @tl].hash
end