Module: Rouge::Seq::ASeq
Overview
A partial implementation of ISeq. You supply #first and #next, it gives:
- #cons
- #to_s
- #seq
- #length (#count)
- #[]
- #==
- #each
- #map
- #to_a
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](idx) ⇒ Object
- #cons(head) ⇒ Object
- #count ⇒ Object
- #each {|self.first| ... } ⇒ Object
- #first ⇒ Object
- #inspect ⇒ Object
- #length ⇒ Object
- #map(&block) ⇒ Object
- #more ⇒ Object
- #next ⇒ Object
- #seq ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Instance Method Details
#==(other) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rouge/seq.rb', line 90 def ==(other) if other.is_a?(ISeq) return self.to_a == other.to_a end if other.is_a?(::Array) return self.to_a == other end false end |
#[](idx) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rouge/seq.rb', line 74 def [](idx) return to_a[idx] if idx.is_a? Range cursor = self idx += self.length if idx < 0 while idx > 0 idx -= 1 cursor = cursor.more return nil if cursor == Empty end cursor.first end |
#cons(head) ⇒ Object
54 55 56 |
# File 'lib/rouge/seq.rb', line 54 def cons(head) Cons.new(head, self) end |
#count ⇒ Object
70 71 72 |
# File 'lib/rouge/seq.rb', line 70 def count length end |
#each {|self.first| ... } ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/rouge/seq.rb', line 102 def each(&block) return self.enum_for(:each) if block.nil? yield self.first cursor = self.more while cursor != Empty yield cursor.first cursor = cursor.more end self end |
#first ⇒ Object
42 |
# File 'lib/rouge/seq.rb', line 42 def first; raise NotImplementedError; end |
#inspect ⇒ Object
34 35 36 |
# File 'lib/rouge/seq.rb', line 34 def inspect to_s end |
#length ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/rouge/seq.rb', line 58 def length len = 0 cursor = self while cursor != Empty len += 1 cursor = cursor.more end len end |
#map(&block) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/rouge/seq.rb', line 116 def map(&block) return self.enum_for(:map) if block.nil? result = [] self.each {|elem| result << block.call(elem)} result end |
#more ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/rouge/seq.rb', line 45 def more nseq = self.next if nseq.nil? Empty else nseq end end |
#next ⇒ Object
43 |
# File 'lib/rouge/seq.rb', line 43 def next; raise NotImplementedError; end |
#seq ⇒ Object
38 39 40 |
# File 'lib/rouge/seq.rb', line 38 def seq self end |
#to_a ⇒ Object
124 125 126 127 128 |
# File 'lib/rouge/seq.rb', line 124 def to_a r = [] self.each {|e| r << e} r end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/rouge/seq.rb', line 30 def to_s "(#{to_a.map(&:to_s).join " "})" end |