Class: Apricot::List
Overview
A linked list implementation representing (a b c) syntax in Apricot
Direct Known Subclasses
Defined Under Namespace
Classes: EmptyList
Constant Summary collapse
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#head ⇒ Object
readonly
Returns the value of attribute head.
-
#tail ⇒ Object
readonly
Returns the value of attribute tail.
Class Method Summary collapse
Instance Method Summary collapse
- #cons(x) ⇒ Object
- #first ⇒ Object
-
#initialize(head, tail) ⇒ List
constructor
A new instance of List.
- #inspect ⇒ Object (also: #to_s)
- #next ⇒ Object
- #to_list ⇒ Object
- #to_seq ⇒ Object
Methods included from Seq
#<=>, #each, #empty?, #hash, #last, #rest
Constructor Details
#initialize(head, tail) ⇒ List
Returns a new instance of List.
16 17 18 19 20 |
# File 'lib/apricot/list.rb', line 16 def initialize(head, tail) @head = head @tail = tail || EMPTY_LIST @count = tail ? tail.count + 1 : 1 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
14 15 16 |
# File 'lib/apricot/list.rb', line 14 def count @count end |
#head ⇒ Object (readonly)
Returns the value of attribute head.
14 15 16 |
# File 'lib/apricot/list.rb', line 14 def head @head end |
#tail ⇒ Object (readonly)
Returns the value of attribute tail.
14 15 16 |
# File 'lib/apricot/list.rb', line 14 def tail @tail end |
Class Method Details
.[](*args) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/apricot/list.rb', line 6 def self.[](*args) list = EMPTY_LIST args.reverse_each do |arg| list = list.cons(arg) end list end |
Instance Method Details
#first ⇒ Object
37 38 39 |
# File 'lib/apricot/list.rb', line 37 def first @head end |
#inspect ⇒ Object Also known as: to_s
49 50 51 52 53 54 55 56 |
# File 'lib/apricot/list.rb', line 49 def inspect return '()' if empty? str = '(' each {|x| str << x.apricot_inspect << ' ' } str.chop! str << ')' end |
#next ⇒ Object
41 42 43 |
# File 'lib/apricot/list.rb', line 41 def next @tail.empty? ? nil : @tail end |
#to_list ⇒ Object
33 34 35 |
# File 'lib/apricot/list.rb', line 33 def to_list self end |
#to_seq ⇒ Object
45 46 47 |
# File 'lib/apricot/list.rb', line 45 def to_seq self end |