Version 0.1.0: Vollstaendig neugeschrieben. Klassen statt Lambdas. Erweiterbar
This commit is contained in:
parent
8e883065ca
commit
63b6ac53b1
|
@ -23,26 +23,138 @@ end
|
|||
class Functional
|
||||
include Enumerable
|
||||
|
||||
def self.method_missing meth, *args, &exe
|
||||
self.new.send meth, *args, &exe
|
||||
class Base
|
||||
attr_reader :exe
|
||||
attr_accessor :next
|
||||
def initialize &e
|
||||
@exe = e
|
||||
end
|
||||
|
||||
def call *a
|
||||
@next.call *a
|
||||
end
|
||||
|
||||
def end
|
||||
@next.end
|
||||
end
|
||||
end
|
||||
|
||||
def push_method meth, *args, &exe
|
||||
@stack.push [meth, exe]+args
|
||||
class Collect <Base
|
||||
def call *a
|
||||
@next.call *@exe.call( *a)
|
||||
end
|
||||
end
|
||||
|
||||
class Select <Base
|
||||
def call *a
|
||||
@next.call *a if @exe.call *a
|
||||
end
|
||||
end
|
||||
|
||||
class DeleteIf <Base
|
||||
def call *a
|
||||
@next.call *a unless @exe.call *a
|
||||
end
|
||||
end
|
||||
|
||||
class Compact <Base
|
||||
def call *a
|
||||
@next.call *a unless a.empty? || [nil] == a
|
||||
end
|
||||
end
|
||||
|
||||
class BottomUp <Base
|
||||
def initialize start, *a, &e
|
||||
@next.call *a, &e
|
||||
@buffer, @start = nil, start
|
||||
end
|
||||
|
||||
def call a
|
||||
if @exe.call a
|
||||
@next.call @buffer+a
|
||||
@buffer = @start.dup
|
||||
else
|
||||
@buffer += a
|
||||
end
|
||||
end
|
||||
|
||||
def end
|
||||
@next.call @buffer
|
||||
@next.end
|
||||
end
|
||||
end
|
||||
|
||||
class TopDown <Base
|
||||
def initialize start, *a, &e
|
||||
@next.call *a, &e
|
||||
@buffer, @start = nil, start
|
||||
end
|
||||
|
||||
def call a
|
||||
if @exe.call a
|
||||
@next.call @buffer
|
||||
@buffer = @start.dup+a
|
||||
else
|
||||
@buffer += a
|
||||
end
|
||||
end
|
||||
|
||||
def end
|
||||
@next.call @buffer
|
||||
@next.end
|
||||
end
|
||||
end
|
||||
|
||||
class Each <Base
|
||||
def end
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
class P <Each
|
||||
def initialize *a
|
||||
super *a, &Kernel.method( :p)
|
||||
end
|
||||
end
|
||||
|
||||
class Inject <Base
|
||||
attr_reader :it
|
||||
def initialize start, *a, &e
|
||||
super *a, &e
|
||||
@it = start
|
||||
end
|
||||
def call *a
|
||||
@it = @exe.call @it, *a
|
||||
end
|
||||
def end
|
||||
@it
|
||||
end
|
||||
end
|
||||
|
||||
class To_a <Inject
|
||||
def initialize *a, &e
|
||||
super [], *a, &e
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :next, :stack, :obj, :func, :args
|
||||
|
||||
def initialize obj = nil, func = nil, *args
|
||||
@next, @stack, @obj, @func, @args = self, self, obj, func, args
|
||||
end
|
||||
|
||||
def push a
|
||||
@stack = @stack.next = a
|
||||
self
|
||||
end
|
||||
|
||||
def initialize obj = nil, func = nil, *args
|
||||
@stack, @obj, @func, @args = [], obj, func, args
|
||||
end
|
||||
|
||||
def collect &exe
|
||||
push_method :collect, &exe
|
||||
push Collect.new( &exe)
|
||||
end
|
||||
|
||||
# map/reduce?
|
||||
def map &exe
|
||||
push_method :map, &exe
|
||||
push Map.new( &exe)
|
||||
end
|
||||
|
||||
# map/reduce?
|
||||
|
@ -51,49 +163,38 @@ class Functional
|
|||
end
|
||||
|
||||
def select &exe
|
||||
push_method :select, &exe
|
||||
push Select.new( &exe)
|
||||
end
|
||||
|
||||
def grep r
|
||||
push_method :select, &r.method( :match)
|
||||
def grep re
|
||||
push Select.new( &re.method( :match))
|
||||
end
|
||||
|
||||
def delete_if &exe
|
||||
push_method :delete_if, &exe
|
||||
push DeleteIf.new( &exe)
|
||||
end
|
||||
|
||||
def compact
|
||||
push_method :compact
|
||||
push Compact.new
|
||||
end
|
||||
|
||||
def together init, &exe
|
||||
push_method :together, init, &exe
|
||||
def updown init, &exe
|
||||
push UpDown.new( init, &exe)
|
||||
end
|
||||
|
||||
def topdown init, &exe
|
||||
push TopDown.new( init, &exe)
|
||||
end
|
||||
|
||||
def each &exe
|
||||
return self unless exe
|
||||
callstack = exe
|
||||
@stack.reverse.each do |a|
|
||||
m, e = *a[0..1]
|
||||
pre = callstack
|
||||
callstack = case m
|
||||
when :collect then lambda {|val| pre.call e.call( val) }
|
||||
when :select then lambda {|val| pre.call val if e.call val }
|
||||
when :delete_if then lambda {|val| pre.call val unless e.call val }
|
||||
when :compact then lambda {|val| pre.call val if val }
|
||||
when :map then lambda {|val| e.call( val).each &pre }
|
||||
when :reduce
|
||||
buf = {}
|
||||
lambda {|val| buf[ val.first] = e.call( *val) }
|
||||
when :together
|
||||
buf = a[2].dup
|
||||
lambda {|val| if e.call val then pre.call buf; buf = a[2].dup+val else buf += val end }
|
||||
else
|
||||
$stderr.puts "Whats that? #{m.inspect}"
|
||||
callstack
|
||||
end
|
||||
end
|
||||
@obj.send @func||:each, *@args, &callstack
|
||||
push Each.new
|
||||
push exe
|
||||
run
|
||||
end
|
||||
|
||||
def run
|
||||
@obj.send @func||:each, *@args, &@next.method(:call)
|
||||
@next.end
|
||||
end
|
||||
|
||||
def p
|
||||
|
|
Reference in a new issue