Class: Lisp::DottedPair

Inherits:
Object show all
Includes:
Enumerable, Lisp
Defined in:
lib/mega/lisp.rb

Overview

DottedPair

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lisp

#accumulate, #all?, #append, #assoc, #atom?, #car, #cdr, #cons, #consonto, #drop, #equal?, #exists?, #explode, #filter, #implode, #length, #link, #list, #map, #member?, #null?, #pair!, #pair?, #pairlis, #prod, #reduce, #reverse, #set, #set_car!, #set_cdr!, #sum, #take, #zip

Constructor Details

#initialize(first = nil, second = nil) ⇒ DottedPair

Returns a new instance of DottedPair.



62
63
64
# File 'lib/mega/lisp.rb', line 62

def initialize(first = nil, second = nil)
  @first, @second = first, second
end

Instance Attribute Details

#firstObject

Returns the value of attribute first.



66
67
68
# File 'lib/mega/lisp.rb', line 66

def first
  @first
end

#secondObject

Returns the value of attribute second.



66
67
68
# File 'lib/mega/lisp.rb', line 66

def second
  @second
end

Class Method Details

.[](*array) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/mega/lisp.rb', line 68

def DottedPair.[](*array)
  if array.empty?
    nil
  else
    DottedPair.new(array.shift, DottedPair[*array])
  end
end

Instance Method Details

#==(other) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/mega/lisp.rb', line 107

def ==(other)
  if pair?(self) and pair?(other)
    car(self) == car(other) and cdr(self) == cdr(other)
  else
    if pair?(self) then false else self == other end
  end
end

#each(&block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mega/lisp.rb', line 76

def each(&block)
  if @first
    if @first.is_a? DottedPair
      @first.each(&block)
    else
      block.call(@first)
    end
  end
  if @second
    if @second.is_a? DottedPair
      @second.each(&block)
    else
      block.call(@second)
    end
  end
end

#inspectObject



97
98
99
# File 'lib/mega/lisp.rb', line 97

def inspect
  '(' + repr + ')'
end

#to_aObject



101
102
103
104
105
# File 'lib/mega/lisp.rb', line 101

def to_a
  a = []
  each { |e| a << e }
  a
end

#to_sObject



93
94
95
# File 'lib/mega/lisp.rb', line 93

def to_s
  '(' + repr + ')'
end