MapReduce implementiert

master
Denis Knauf 2010-05-25 19:02:49 +02:00
parent 76d141c101
commit 67a2d116fe
1 changed files with 14 additions and 8 deletions

View File

@ -24,6 +24,7 @@ class ::Object
def functional meth = nil def functional meth = nil
Functional.new self, meth Functional.new self, meth
end end
alias to_fun functional
end end
class Functional class Functional
@ -149,19 +150,24 @@ class Functional
class Map <Collect class Map <Collect
def call *a def call *a
@next.call *@exe.call(*a, &@next) @exe.call *a, &@next
end end
end end
class Reduce <Base class Reduce <Base
def initialize *a, &e def initialize iv, *a, &e
super iv, *a, &e super *a, &e
@buf = {} @buf = {}
@buf.default = iv @buf.default = iv
end end
def call *a def call *a
@buf[] @buf[ a[0]] = @exe.call @buf[ a[0]], *a[1..-1]
end
def end
@buf.each {|i| @next.call *i}
@next.end
end end
end end
@ -180,12 +186,12 @@ class Functional
push Collect.new( &exe) push Collect.new( &exe)
end end
def map *a, &exe def map &exe
push Map.new( &exe) push Map.new( &exe)
end end
def reduce *a, &exe def reduce iv, &exe
raise "Reserved for MapReduce." push Reduce.new( iv, &exe)
end end
def select &exe def select &exe
@ -230,7 +236,7 @@ class Functional
end end
def p def p
each &Kernel.method( :p) each {|*a|Kernel.p a}
end end
end end