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

master
Denis Knauf 2010-05-23 22:20:53 +02:00
parent aaa7f106c8
commit 76d141c101
1 changed files with 46 additions and 18 deletions

View File

@ -1,22 +1,28 @@
class NotRegexp class ::Regexp
def initialize r class NegRegexp
@rx = r def initialize r
end @rx = r
def match l end
! @rx.match( l) def match l
end ! @rx.match( l)
def =~ l end
! @rx =~ l def =~ l
! @rx =~ l
end
def -@
@rx
end
end end
def -@ def -@
@rx NegRegexp.new self
end end
end end
class Regexp class ::Object
def -@ def functional meth = nil
NotRegexp.new self 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