Permalink
Browse files

Some fixes per sniffer.io [1].

  • Loading branch information...
1 parent 0961753 commit cfb36d36064b6e7120e92214299912cf14e3cc1c Arlen Christian Mart Cuss committed Dec 5, 2012
Showing with 68 additions and 38 deletions.
  1. +11 −9 lib/rouge.rb
  2. +57 −29 lib/rouge/seq.rb
View
@@ -9,7 +9,7 @@ module Rouge; end
start = Time.now
Rouge.define_singleton_method :start, lambda {start}
-class << Rouge
+module Rouge
require 'rouge/version'
require 'rouge/wrappers'
require 'rouge/symbol'
@@ -19,36 +19,38 @@ class << Rouge
require 'rouge/context'
require 'rouge/repl'
- def print(form, out)
+ def self.print(form, out)
Rouge::Printer.print form, out
end
- def [](ns)
+ def self.[](ns)
Rouge::Namespace[ns]
end
- def boot!
+ def self.boot!
return if @booted
@booted = true
+ builtin = Rouge[:"rouge.builtin"]
+
core = Rouge[:"rouge.core"]
- core.refer Rouge[:"rouge.builtin"]
+ core.refer builtin
user = Rouge[:user]
- user.refer Rouge[:"rouge.builtin"]
- user.refer Rouge[:"rouge.core"]
+ user.refer builtin
+ user.refer core
user.refer Rouge[:ruby]
Rouge::Context.new(user).readeval(
File.read(Rouge.relative_to_lib('boot.rg')))
end
- def repl(argv)
+ def self.repl(argv)
boot!
Rouge::REPL.repl(argv)
end
- def relative_to_lib name
+ def self.relative_to_lib name
File.join(File.dirname(File.absolute_path(__FILE__)), name)
end
end
View
@@ -1,12 +1,30 @@
# encoding: utf-8
+# Functions, clases and modules concerning the `seq' sequence abstraction.
module Rouge::Seq
+ # An empty seq.
Empty = Object.new
+ # A sequence consisting of a head (element) and tail (another sequence).
+ # Filled out below after ASeq's definition.
class Cons; end
+ # A module purely to indicate that this is a seqable type. Any class
+ # including ISeq should define a #seq method.
module ISeq; end
+ # A partial implementation of ISeq. You supply #first and #next, it gives:
+ #
+ # - #cons
+ # - #inspect
+ # - #to_s
+ # - #seq
+ # - #length (#count)
+ # - #[]
+ # - #==
+ # - #each
+ # - #map
+ # - #to_a
module ASeq
include ISeq
@@ -22,41 +40,41 @@ def first; raise NotImplementedError; end
def next; raise NotImplementedError; end
def more
- s = self.next
- if s.nil?
+ nseq = self.next
+ if nseq.nil?
Empty
else
- s
+ nseq
end
end
- def cons(o)
- Cons.new(o, self)
+ def cons(head)
+ Cons.new(head, self)
end
def length
- l = 0
+ len = 0
cursor = self
while cursor != Empty
- l += 1
+ len += 1
cursor = cursor.more
end
- l
+ len
end
alias count length
- def [](i)
- return to_a[i] if i.is_a? Range
+ def [](idx)
+ return to_a[idx] if idx.is_a? Range
cursor = self
- i += self.length if i < 0
+ idx += self.length if idx < 0
- while i > 0
- i -= 1
+ while idx > 0
+ idx -= 1
cursor = cursor.more
return nil if cursor == Empty
end
@@ -65,8 +83,15 @@ def [](i)
end
def ==(seq)
- (seq.is_a?(ISeq) and self.to_a == seq.to_a) or
- (seq.is_a?(::Array) and self.to_a == seq)
+ if seq.is_a?(ISeq)
+ return self.to_a == seq.to_a
+ end
+
+ if seq.is_a?(::Array)
+ return self.to_a == seq
+ end
+
+ false
end
def each(&block)
@@ -86,20 +111,16 @@ def each(&block)
def map(&block)
return self.enum_for(:map) if block.nil?
- r = []
- self.each {|e| r << block.call(e)}
- r
+ result = []
+ self.each {|elem| result << block.call(elem)}
+ result
end
def to_a
r = []
self.each {|e| r << e}
r
end
-
- def map(&block)
- Cons[*to_a.map(&block)]
- end
end
class << Empty
@@ -135,10 +156,11 @@ def first; @head; end
def next; Rouge::Seq.seq @tail; end
def self.[](*elements)
- return Empty if elements.length.zero?
+ length = elements.length
+ return Empty if length.zero?
head = nil
- (elements.length - 1).downto(0).each do |i|
+ (length - 1).downto(0).each do |i|
head = new(elements[i], head.freeze)
end
@@ -148,24 +170,27 @@ def self.[](*elements)
attr_reader :head, :tail
end
+ # A seq over a Ruby Array.
class Array
include ASeq
- def initialize(array, i)
- @array, @i = array, i
+ def initialize(array, idx)
+ @array, @idx = array, idx
end
def first
- @array[@i]
+ @array[@idx]
end
def next
- if @i + 1 < @array.length
- Array.new(@array, @i + 1)
+ if @idx + 1 < @array.length
+ Array.new(@array, @idx + 1)
end
end
end
+ # A lazy seq; contains the body (thunk) which is a lambda to get the "real"
+ # seq. Once evaluated (realised), the result is cached.
class Lazy
include ISeq
@@ -179,6 +204,7 @@ def seq
@result
else
@result = Rouge::Seq.seq(@body.call) || Empty
+ @body = nil
@realized = true
@result
end
@@ -196,6 +222,8 @@ 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)
def self.seq(form)

0 comments on commit cfb36d3

Please sign in to comment.