renamed call-functions. every call is an alias for a meaningful function-name. useful for debugging.

master
Denis Knauf 2011-11-24 11:35:08 +01:00
parent 70e8c7ea29
commit ed649d3801
1 changed files with 48 additions and 20 deletions

View File

@ -49,16 +49,23 @@ end
class Functional class Functional
include Enumerable include Enumerable
class DEFAULT
end
class Base class Base
attr_reader :exe attr_reader :exe
attr_accessor :next attr_accessor :next
attr_reader :caller
def initialize &e def initialize &e
@caller = Kernel.caller.first
@exe = e @exe = e
end end
def call *a def base_fun *a
@next.call *a @next.call *a
end end
alias call base_fun
def end def end
@next.end @next.end
@ -74,34 +81,39 @@ class Functional
end end
class Collect <Base class Collect <Base
def call *a def collect_fun *a
@next.call *@exe.call( *a) @next.call *@exe.call( *a)
end end
alias call collect_fun
end end
class Tap <Base class Tap <Base
def call *a def tap_fun *a
@exe.call *a @exe.call *a
@next.call *a @next.call *a
end end
alias call tap_fun
end end
class Select <Base class Select <Base
def call *a def select_fun *a
@next.call *a if @exe.call *a @next.call *a if @exe.call *a
end end
alias call select_fun
end end
class Filter <Base class Filter <Base
def call *a def filter_fun *a
@next.call *a unless @exe.call *a @next.call *a unless @exe.call *a
end end
alias call filter_fun
end end
class Compact <Base class Compact <Base
def call *a def compact_fun *a
@next.call *a unless a.empty? || [nil] == a @next.call *a unless a.empty? || [nil] == a
end end
alias call compact_fun
end end
class BottomUp <Base class BottomUp <Base
@ -110,7 +122,7 @@ class Functional
@buffer, @start = nil, start @buffer, @start = nil, start
end end
def call a def bottom_up_fun a
if @exe.call a if @exe.call a
@next.call @buffer+a @next.call @buffer+a
@buffer = @start.dup @buffer = @start.dup
@ -118,6 +130,7 @@ class Functional
@buffer += a @buffer += a
end end
end end
alias call bottom_up_fun
def end def end
@next.call @buffer @next.call @buffer
@ -131,7 +144,7 @@ class Functional
@buffer, @start = nil, start @buffer, @start = nil, start
end end
def call a def top_down_fun a
if @exe.call a if @exe.call a
@next.call @buffer @next.call @buffer
@buffer = @start.dup+a @buffer = @start.dup+a
@ -139,6 +152,7 @@ class Functional
@buffer += a @buffer += a
end end
end end
alias call top_down_fun
def end def end
@next.call @buffer @next.call @buffer
@ -168,9 +182,10 @@ class Functional
@it = start @it = start
end end
def call *a def inject_fun *a
@it = @exe.call @it, *a @it = @exe.call @it, *a
end end
alias call inject_fun
end end
class To_a <Inject class To_a <Inject
@ -180,26 +195,36 @@ class Functional
end end
class Map <Collect class Map <Collect
def call *a def map_fun *a
@exe.call *a, &@next @exe.call *a, &@next
end end
alias call map_fun
end end
class Flatten <Base class Flatten <Base
def call *a def flatten_fun *a
a.each &@next.method( :call) a.each &@next.method( :call)
end end
alias call flatten_fun
end end
class Reduce <Base class Reduce <Base
def initialize *a, &e def initialize iv = ::Functional::DEFAULT, *a, &exe
super *a, &e super *a, &exe
@buf = Hash.new {|h,k| h[k] = []} iv = Array.method :new if ::Functional::DEFAULT == iv
@buf = if iv.kind_of?( ::Proc) || iv.kind_of?( ::Method)
p default: :proc, iv: iv
Hash.new {|h,k| h[k] = iv.call }
else
p default: :value, iv: iv
{}.tap {|h| h.default = iv }
end
end end
def call *a def reduce_fun *a
@buf[ a[0]] = @exe.call @buf[ a[0]], *a[1..-1] @buf[ a[0]] = @exe.call @buf[ a[0]], *a[1..-1]
end end
alias call reduce_fun
def end def end
@buf.each {|i| @next.call *i} @buf.each {|i| @next.call *i}
@ -212,13 +237,14 @@ class Functional
@buf, @n = [], n @buf, @n = [], n
end end
def call *a def slice_fun *a
@buf.push a @buf.push a
unless @n > @buf.size unless @n > @buf.size
@next.call @buf @next.call @buf
@buf.clear @buf.clear
end end
end end
alias call slice_fun
def end def end
@next.call @buf @next.call @buf
@ -231,7 +257,7 @@ class Functional
@buf, @n = [], n @buf, @n = [], n
end end
def call *a def cons_fun *a
@buf.push a @buf.push a
unless @n > @buf.size unless @n > @buf.size
class <<self class <<self
@ -245,6 +271,7 @@ class Functional
@buf.shift @buf.shift
end end
end end
alias call cons_fun
def end def end
@next.call @buf unless @n > @buf.size @next.call @buf unless @n > @buf.size
@ -267,9 +294,10 @@ class Functional
@pager.puts a.inspect @pager.puts a.inspect
end end
def call *a def pager_fun *a
@pager.puts a @pager.puts a
end end
alias call pager_fun
def clean def clean
@pager.close @pager.close
@ -300,8 +328,8 @@ class Functional
push Map.new( &exe) push Map.new( &exe)
end end
def reduce &exe def reduce iv = ::Functional::DEFAULT, &exe
push Reduce.new( &exe) push Reduce.new( iv, &exe)
end end
def select &exe def select &exe