MapReduce: first code - needs tokyocabinet
This commit is contained in:
parent
63b6ac53b1
commit
aaa7f106c8
|
@ -152,14 +152,12 @@ class Functional
|
||||||
push Collect.new( &exe)
|
push Collect.new( &exe)
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/reduce?
|
def map *a, &exe
|
||||||
def map &exe
|
raise "Reserved for MapReduce. Install TokyoCabinet, if you want to use it."
|
||||||
push Map.new( &exe)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/reduce?
|
def reduce *a, &exe
|
||||||
def reduce &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
|
||||||
|
@ -187,11 +185,17 @@ class Functional
|
||||||
end
|
end
|
||||||
|
|
||||||
def each &exe
|
def each &exe
|
||||||
|
return self unless exe
|
||||||
push Each.new
|
push Each.new
|
||||||
push exe
|
push exe
|
||||||
run
|
run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def join deli
|
||||||
|
push Inject.new('') {|i,j|i+deli+j}
|
||||||
|
run
|
||||||
|
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
|
||||||
|
@ -201,3 +205,31 @@ class Functional
|
||||||
each &Kernel.method( :p)
|
each &Kernel.method( :p)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'tokyocabinet'
|
||||||
|
|
||||||
|
class Functional
|
||||||
|
class Map <Base
|
||||||
|
class Emit < TokyoCabinet::BDB
|
||||||
|
alias emit putdup
|
||||||
|
alias call emit
|
||||||
|
end
|
||||||
|
|
||||||
|
def call *a
|
||||||
|
@exe.call( *a).each &@next.method(:call)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def map name, &e
|
||||||
|
push Map.new( name, &e)
|
||||||
|
end
|
||||||
|
|
||||||
|
def Reduce name, &e
|
||||||
|
push Reduce.new( name, &e)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
rescue MissingSourceFile
|
||||||
|
# TokyoCabinet not installed?
|
||||||
|
end
|
||||||
|
|
Reference in a new issue