#map/#reduce will raise an error. Functional can be used like Enumerator. Version 0.0.1
This commit is contained in:
parent
b51bd564ef
commit
438fc48b3d
|
@ -9,4 +9,9 @@ Usage
|
|||
require 'functional'
|
||||
|
||||
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.
|
||||
|
|
|
@ -21,12 +21,15 @@ class Functional
|
|||
push_method "value=%s(value)", &exe
|
||||
end
|
||||
|
||||
=begin
|
||||
# map/reduce?
|
||||
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
|
||||
|
||||
def select &exe
|
||||
push_method "%s(value)||next", &exe
|
||||
|
@ -36,12 +39,8 @@ class Functional
|
|||
push_method "%s(value)&&next", &exe
|
||||
end
|
||||
|
||||
def each obj, &exe
|
||||
return Enumerator.new self, :each, obj unless exe
|
||||
obj.each &eval( "lambda{|value|#{@stack.join( ";")};exe.call(value)}")
|
||||
end
|
||||
|
||||
def inject obj, start, &exe
|
||||
each( obj).inject start, &exe
|
||||
def each &exe
|
||||
return self unless exe
|
||||
@obj.send @func || :each, *@args, &eval( "lambda{|value|#{@stack.join( ";")};exe.call(value)}")
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue