diff --git a/lib/functional.rb b/lib/functional.rb index 7faf71d..37edbb9 100644 --- a/lib/functional.rb +++ b/lib/functional.rb @@ -21,10 +21,30 @@ class ::Regexp end class ::Object - def functional meth = nil + def to_fun meth = nil Functional.new self, meth end - alias to_fun functional +end + +class Counter + include Enumerable + attr_reader :c + + def initialize first = nil, step = nil + @c, @step = first || 0, step || 1 + end + + def next; @c += @step end + def to_i; c.to_i end + def to_f; c.to_f end + + def + i + @c += @step*i + end + + def each &e + loop { e.call self; self.next } + end end class Functional @@ -45,6 +65,10 @@ class Functional @next.end end + def clean + @next.clean + end + def to_proc method( :call).to_proc end @@ -120,6 +144,7 @@ class Functional def end nil end + alias :clean :end end class P @buf.size + @next.call @buf + @buf.clear + end + end + + def end + @next.call @buf + @next.end + end + end + + class Cons @buf.size + class < @buf.size + @next.end + end + end + + class Pager [ [0, _A], [1, _B], ..., [_I, _C], ..., [_N, _D]] + # [ [_A|_As], [_B|_Bs], ..., [_C|_Cs], ..., [_D|_Ds] ] ==> [ [0,_A|_As], [1,_B|_Bs], ..., [_I,_C|_Cs], ..., [_N,_D|_Ds] ] + def with_index &exe + i = 0 + exe ||= Array.method :[] + push Collect.new {|*a| exe.call i, *a } + end + + def slice n, &e + push Slice.new( n) + push Collect.new( &e) if e + self + end + + def cons n, &e + push Cons.new( n) + push Collect.new( &e) if e + self + end + + class Save < Base + attr_reader :db + + def initialize db + @db = db + end + + def call k, *v + @db[ k] = v.length == 1 ? v.first : v + end + end + + def save db + push Save.new( db) end end - -begin - require 'tokyocabinet' - - class Functional - class Map