NotRegexp -> NegRegexp. #map implemented. very simple. #reduce: not yet.

This commit is contained in:
Denis Knauf 2010-05-23 22:20:53 +02:00
parent aaa7f106c8
commit 76d141c101

View file

@ -1,5 +1,6 @@
class NotRegexp class ::Regexp
class NegRegexp
def initialize r def initialize r
@rx = r @rx = r
end end
@ -14,9 +15,14 @@ class NotRegexp
end end
end end
class Regexp
def -@ def -@
NotRegexp.new self NegRegexp.new self
end
end
class ::Object
def functional meth = nil
Functional.new self, meth
end end
end end
@ -37,6 +43,10 @@ class Functional
def end def end
@next.end @next.end
end end
def to_proc
method( :call).to_proc
end
end end
class Collect <Base class Collect <Base
@ -137,6 +147,24 @@ class Functional
end end
end end
class Map <Collect
def call *a
@next.call *@exe.call(*a, &@next)
end
end
class Reduce <Base
def initialize *a, &e
super iv, *a, &e
@buf = {}
@buf.default = iv
end
def call *a
@buf[]
end
end
attr_accessor :next, :stack, :obj, :func, :args attr_accessor :next, :stack, :obj, :func, :args
def initialize obj = nil, func = nil, *args def initialize obj = nil, func = nil, *args
@ -153,11 +181,11 @@ class Functional
end end
def map *a, &exe def map *a, &exe
raise "Reserved for MapReduce. Install TokyoCabinet, if you want to use it." push Map.new( &exe)
end end
def reduce *a, &exe def reduce *a, &exe
raise "Reserved for MapReduce. Install TokyoCabinet, if you want to use it." raise "Reserved for MapReduce."
end end
def select &exe def select &exe
@ -197,7 +225,7 @@ class Functional
end end
def run def run
@obj.send @func||:each, *@args, &@next.method(:call) @obj.send @func||:each, *@args, &@next #.method(:call)
@next.end @next.end
end end
@ -217,7 +245,7 @@ begin
end end
def call *a def call *a
@exe.call( *a).each &@next.method(:call) @exe.call( *a).each &@next
end end
end end
@ -232,4 +260,4 @@ begin
rescue MissingSourceFile rescue MissingSourceFile
# TokyoCabinet not installed? # TokyoCabinet not installed?
end end if false