#map/#reduce will raise an error. Functional can be used like Enumerator. Version 0.0.1

This commit is contained in:
Denis Knauf 2010-04-19 21:22:30 +02:00
parent b51bd564ef
commit 438fc48b3d
3 changed files with 16 additions and 12 deletions

View file

@ -9,4 +9,9 @@ Usage
require 'functional' require 'functional'
obj = 0 .. 10**12 obj = 0 .. 10**12
Functional.new( obj).select {|i| i.even? }.map {|i| i/3 }.select {|i| i.even? }.each &method( :puts) Functional.new( obj).select {|i| i.even? }.collect {|i| i/3 }.select {|i| i.even? }.each &method( :puts)
What's with _#map_?
=================
Do you know MapReduce? In future #map will be used for MapReduce. Use #collect.

View file

@ -1 +1 @@
0.0.0 0.0.1

View file

@ -21,12 +21,15 @@ class Functional
push_method "value=%s(value)", &exe push_method "value=%s(value)", &exe
end end
=begin
# map/reduce? # map/reduce?
def map &exe def map &exe
push_method "value=%s(value)", &exe raise "Reserved for MapReduce."
end
# map/reduce?
def reduce &exe
raise "Reserved for MapReduce."
end end
=end
def select &exe def select &exe
push_method "%s(value)||next", &exe push_method "%s(value)||next", &exe
@ -36,12 +39,8 @@ class Functional
push_method "%s(value)&&next", &exe push_method "%s(value)&&next", &exe
end end
def each obj, &exe def each &exe
return Enumerator.new self, :each, obj unless exe return self unless exe
obj.each &eval( "lambda{|value|#{@stack.join( ";")};exe.call(value)}") @obj.send @func || :each, *@args, &eval( "lambda{|value|#{@stack.join( ";")};exe.call(value)}")
end
def inject obj, start, &exe
each( obj).inject start, &exe
end end
end end