Class: Nockr::Noun

Inherits:
Object
  • Object
show all
Defined in:
lib/nockr/noun.rb

Direct Known Subclasses

Atom, Cell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_ary:) ⇒ Noun

Returns a new instance of Noun.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/nockr/noun.rb', line 7

def initialize(from_ary:)
  raise ArgumentError.new("a Noun must be initialized with an Array") unless from_ary.is_a? Array
  @ary = self.to_tuples from_ary
  @n = self.nonify @ary
end

Instance Attribute Details

#aryObject (readonly)

Returns the value of attribute ary.



5
6
7
# File 'lib/nockr/noun.rb', line 5

def ary
  @ary
end

#nObject (readonly)

Returns the value of attribute n.



5
6
7
# File 'lib/nockr/noun.rb', line 5

def n
  @n
end

Instance Method Details

#==(another_noun) ⇒ Object



13
14
15
# File 'lib/nockr/noun.rb', line 13

def ==(another_noun)
  another_noun.ary == self.ary
end

#at(index:) ⇒ Object



17
18
19
20
# File 'lib/nockr/noun.rb', line 17

def at(index:)
  return self if 1 == index
  return self.n.at(index: index)
end

#nonify(ary) ⇒ Object

private



24
25
26
27
28
29
# File 'lib/nockr/noun.rb', line 24

def nonify(ary)
  return Atom.new(ary) unless ary.is_a? Array
  return Atom.new(ary[0]) if 1 == ary.size
  return Cell.new(head: self.nonify(ary[0]), tail: self.nonify(ary[1])) if 2 == ary.size
  return [ary[0], self.nonify(ary[1..])]
end

#to_tuples(ary) ⇒ Object

a b c
a [b c]

q.v. github.com/belisarius222/pynock



36
37
38
39
40
41
# File 'lib/nockr/noun.rb', line 36

def to_tuples(ary)
  return ary unless ary.is_a? Array
  return ary if 1 == ary.size
  return [self.to_tuples(ary[0]), self.to_tuples(ary[1])] if 2 == ary.size
  return [ary[0], self.to_tuples(ary[1..])]
end