added: tap. delete_if -> filter (delete_if manipulates self, but Functional never, so filter is better). reduce |-> inject (it is like Array#inject)
This commit is contained in:
parent
2b7dd9fb58
commit
abb9954a33
|
@ -80,13 +80,20 @@ class Functional
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Tap <Base
|
||||||
|
def call *a
|
||||||
|
@exe.call *a
|
||||||
|
@next.call *a
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class Select <Base
|
class Select <Base
|
||||||
def call *a
|
def call *a
|
||||||
@next.call *a if @exe.call *a
|
@next.call *a if @exe.call *a
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DeleteIf <Base
|
class Filter <Base
|
||||||
def call *a
|
def call *a
|
||||||
@next.call *a unless @exe.call *a
|
@next.call *a unless @exe.call *a
|
||||||
end
|
end
|
||||||
|
@ -180,12 +187,12 @@ class Functional
|
||||||
end
|
end
|
||||||
|
|
||||||
class Flatten <Base
|
class Flatten <Base
|
||||||
def call a
|
def call *a
|
||||||
Array === a ? a.each( &method( :call)) : @next.call( a)
|
a.each &@next.method( :call)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Reduce <Base
|
class Inject <Base
|
||||||
def initialize iv, *a, &e
|
def initialize iv, *a, &e
|
||||||
super *a, &e
|
super *a, &e
|
||||||
@buf = {}
|
@buf = {}
|
||||||
|
@ -295,9 +302,10 @@ class Functional
|
||||||
push Map.new( &exe)
|
push Map.new( &exe)
|
||||||
end
|
end
|
||||||
|
|
||||||
def reduce iv, &exe
|
def inject iv, &exe
|
||||||
push Reduce.new( iv, &exe)
|
push Inject.new( iv, &exe)
|
||||||
end
|
end
|
||||||
|
alias reduce inject
|
||||||
|
|
||||||
def select &exe
|
def select &exe
|
||||||
push Select.new( &exe)
|
push Select.new( &exe)
|
||||||
|
@ -307,8 +315,8 @@ class Functional
|
||||||
push Select.new( &re.method( :match))
|
push Select.new( &re.method( :match))
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_if &exe
|
def filter &exe
|
||||||
push DeleteIf.new( &exe)
|
push Filter.new( &exe)
|
||||||
end
|
end
|
||||||
|
|
||||||
def compact
|
def compact
|
||||||
|
@ -325,7 +333,6 @@ class Functional
|
||||||
|
|
||||||
def flatten &exe
|
def flatten &exe
|
||||||
push Flatten.new
|
push Flatten.new
|
||||||
push Collect.new( &exe) if exe
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def each &exe
|
def each &exe
|
||||||
|
|
Reference in a new issue