lib/rouge/seq.rb in rouge-lang-0.0.9 vs lib/rouge/seq.rb in rouge-lang-0.0.10
- old
+ new
@@ -14,11 +14,10 @@
module ISeq; end
# A partial implementation of ISeq. You supply #first and #next, it gives:
#
# - #cons
- # - #inspect
# - #to_s
# - #seq
# - #length (#count)
# - #[]
# - #==
@@ -26,18 +25,18 @@
# - #map
# - #to_a
module ASeq
include ISeq
- def inspect
- "(#{to_a.map(&:inspect).join " "})"
+ def to_s
+ "(#{to_a.map(&:to_s).join " "})"
end
- def to_s; inspect; end
+ def seq
+ self
+ end
- def seq; self; end
-
def first; raise NotImplementedError; end
def next; raise NotImplementedError; end
def more
nseq = self.next
@@ -82,17 +81,17 @@
end
cursor.first
end
- def ==(seq)
- if seq.is_a?(ISeq)
- return self.to_a == seq.to_a
+ def ==(other)
+ if other.is_a?(ISeq)
+ return self.to_a == other.to_a
end
- if seq.is_a?(::Array)
- return self.to_a == seq
+ if other.is_a?(::Array)
+ return self.to_a == other
end
false
end
@@ -126,12 +125,13 @@
end
class << Empty
include ASeq
- def inspect; "()"; end
- def to_s; inspect; end
+ def to_s
+ '()'
+ end
def seq; nil; end
def first; nil; end
def next; nil; end
@@ -143,35 +143,51 @@
Empty.freeze
class Cons
include ASeq
+ attr_reader :head, :tail
+
def initialize(head, tail)
- if tail and !tail.is_a?(ISeq)
+ if tail && !tail.is_a?(ISeq)
raise ArgumentError,
"tail should be an ISeq, not #{tail.inspect} (#{tail.class})"
end
@head, @tail = head, tail
end
- def first; @head; end
- def next; Rouge::Seq.seq @tail; end
+ def first
+ @head
+ end
+ def next
+ Rouge::Seq.seq(@tail)
+ end
+
+ def to_s
+ if self.length == 2 && self[0] == Rouge::Symbol[:quote]
+ "'#{self[1]}"
+ elsif self.length == 2 && self[0] == Rouge::Symbol[:var]
+ "#'#{self[1]}"
+ else
+ "(#{Rouge::Printer.print_collection(self)})"
+ end
+ end
+
def self.[](*elements)
length = elements.length
+
return Empty if length.zero?
head = nil
(length - 1).downto(0).each do |i|
head = new(elements[i], head.freeze)
end
head.freeze
end
-
- attr_reader :head, :tail
end
# A seq over a Ruby Array.
class Array
include ASeq
@@ -238,12 +254,17 @@
def method_missing(sym, *args, &block)
seq.send(sym, *args, &block)
end
- def inspect; seq.inspect; end
- def to_s; seq.to_s; end
+ def inspect
+ seq.inspect
+ end
+
+ def to_s
+ seq.to_s
+ end
end
# An error thrown when we try to do a seq operation on something that's not
# seqable.
UnknownSeqError = Class.new(StandardError)
@@ -258,17 +279,13 @@
if form.empty?
nil
else
Rouge::Seq::Array.new(form, 0)
end
- when Enumerator
+ when Hash, Set, Enumerator
seq(form.to_a)
when String
seq(form.chars)
- when Set
- seq(form.to_a)
- when Hash
- seq(form.to_a)
else
raise UnknownSeqError, form.inspect
end
end
end