Class: Immutable::List

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

Overview

Immutable::List represents an immutable list.

Immutable::List is an abstract class and [] should be used instead of new. For example:

include Immutable
p List[]      #=> List[]
p List[1, 2]  #=> List[1, 2]

Immutable::Nil represents an empty list, and Immutable::Cons represents a cons cell, which is a node in a list. For example:

p Nil                    #=> List[]
p Cons[1, Cons[2, Nil]]  #=> List[1, 2]

Direct Known Subclasses

Cons

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Consable

#+, #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

Methods included from Foldable

#foldl, #length, #product, #sum

Class Method Details

.emptylist

Returns an empty list.

Returns:

  • (list)

    the empty list.



26
27
28
# File 'lib/immutable/list.rb', line 26

def self.empty
  Nil
end

Instance Method Details

#cons(x) ⇒ List

Adds a new element at the head of self.

Parameters:

  • x (Object)

    the element to add.

Returns:

  • (List)

    a new list.



34
35
36
# File 'lib/immutable/list.rb', line 34

def cons(x)
  Cons[x, self]
end

#empty?true, false

Returns whether self is empty.

Returns:

  • (true, false)

    true if self is empty; otherwise, false.



81
82
83
# File 'lib/immutable/list.rb', line 81

def empty?
  # this method should be overriden
end

#headObject

Returns the first element of self. If self is empty, Immutable::EmptyError is raised.

Returns:

  • (Object)

    the first element of self.



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

def head
  # this method should be overriden
end

#initList

Returns all the elements of self except the last one. If self is empty, Immutable::EmptyError is raised.

Returns:

  • (List)

    the elements of self except the last one.



67
68
69
# File 'lib/immutable/list.rb', line 67

def init
  # this method should be overriden
end

#lastObject

Returns the last element of self. If self is empty, Immutable::EmptyError is raised.

Returns:

  • (Object)

    the last element of self.



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

def last
  # this method should be overriden
end

#popList

Same as #init.

Returns:

  • (List)

    the elements of self except the last one.



74
75
76
# File 'lib/immutable/list.rb', line 74

def pop
  init
end

#tailList

Returns the elements after the head of self. If self is empty, Immutable::EmptyError is raised.

Returns:

  • (List)

    the elements after the head of self.



58
59
60
# File 'lib/immutable/list.rb', line 58

def tail
  # this method should be overriden
end

#to_listList

Returns self.

Returns:



88
89
90
# File 'lib/immutable/list.rb', line 88

def to_list
  self
end