Compare commits
6 Commits
Author | SHA1 | Date |
---|---|---|
Denis Knauf | ede467e934 | |
Denis Knauf | 145129641d | |
Denis Knauf | 99a268a89d | |
Denis Knauf | c8edcddcf6 | |
Denis Knauf | 53033b8cdd | |
Denis Knauf | 29e7a0e4e2 |
|
@ -1 +1,2 @@
|
|||
functional.gemspec
|
||||
pkg
|
||||
|
|
|
@ -47,6 +47,7 @@ class Counter
|
|||
end
|
||||
|
||||
class Functional
|
||||
def self.__version__() '0.1.5' end
|
||||
include Enumerable
|
||||
|
||||
class DEFAULT
|
||||
|
@ -161,6 +162,11 @@ class Functional
|
|||
end
|
||||
|
||||
class Each <Base
|
||||
def each_fun *a
|
||||
@exe.call *a
|
||||
end
|
||||
alias call each_fun
|
||||
|
||||
def end
|
||||
nil
|
||||
end
|
||||
|
@ -213,25 +219,42 @@ class Functional
|
|||
super *a, &exe
|
||||
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
|
||||
|
||||
def reduce_fun *a
|
||||
@buf[ a[0]] = @exe.call @buf[ a[0]], *a[1..-1]
|
||||
def reduce_fun k, *a
|
||||
@buf[ k] = @exe.call k, @buf[ k], *a
|
||||
end
|
||||
alias call reduce_fun
|
||||
|
||||
def end
|
||||
@buf.each {|i| @next.call *i}
|
||||
@buf.each &@next.method( :call)
|
||||
@next.end
|
||||
end
|
||||
end
|
||||
|
||||
class ToHash <Base
|
||||
def initialize
|
||||
super
|
||||
@buf = {}
|
||||
end
|
||||
|
||||
def to_hash_fun k, *a
|
||||
@buf[k] = a
|
||||
end
|
||||
alias call to_hash_fun
|
||||
|
||||
def end
|
||||
@buf
|
||||
end
|
||||
|
||||
def clean
|
||||
end
|
||||
end
|
||||
|
||||
class Slice <Base
|
||||
def initialize n
|
||||
@buf, @n = [], n
|
||||
|
@ -343,6 +366,7 @@ class Functional
|
|||
def filter &exe
|
||||
push Filter.new( &exe)
|
||||
end
|
||||
alias reject filter
|
||||
|
||||
def compact
|
||||
push Compact.new
|
||||
|
@ -362,8 +386,12 @@ class Functional
|
|||
|
||||
def each &exe
|
||||
return self unless exe
|
||||
push Each.new
|
||||
push exe
|
||||
push Each.new( &exe)
|
||||
run
|
||||
end
|
||||
|
||||
def to_hash
|
||||
push ToHash.new
|
||||
run
|
||||
end
|
||||
|
||||
|
@ -402,19 +430,21 @@ class Functional
|
|||
def with_index &exe
|
||||
i = 0
|
||||
exe ||= Array.method :[]
|
||||
push Collect.new {|*a| exe.call i, *a }
|
||||
push Collect.new {|*a| exe.call i, *a; i += 1 }
|
||||
end
|
||||
|
||||
def slice n, &e
|
||||
def slice n, &exe
|
||||
push Slice.new( n)
|
||||
push Collect.new( &e) if e
|
||||
self
|
||||
block_given? ? self.collect( &exe) : self
|
||||
end
|
||||
|
||||
def cons n, &e
|
||||
def cons n, &exe
|
||||
push Cons.new( n)
|
||||
push Collect.new( &e) if e
|
||||
self
|
||||
block_given? ? self.collect( &exe) : self
|
||||
end
|
||||
|
||||
def tap &exe
|
||||
push Tap.new( &exe)
|
||||
end
|
||||
|
||||
class Save < Base
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
require 'test/unit'
|
||||
require 'functional'
|
||||
|
||||
class FunTest < Test::Unit::TestCase
|
||||
M = 0..100
|
||||
|
||||
def doit_fun m, &exe
|
||||
f = m.to_fun
|
||||
yield f
|
||||
f.to_a
|
||||
end
|
||||
|
||||
def test_to_fun_exists
|
||||
assert_respond_to Object, :to_fun
|
||||
end
|
||||
|
||||
def test_to_a
|
||||
assert_equal M.to_a, doit_fun( M) {|x| x }
|
||||
end
|
||||
|
||||
def test_collect
|
||||
l = lambda {|x| x*2}
|
||||
assert_equal M.collect( &l), doit_fun( M) {|x| x.collect( &l) }
|
||||
end
|
||||
|
||||
def test_inject
|
||||
assert_equal M.inject( 0) {|i,j| i+j }, M.to_fun.inject( 0) {|i,j| i+j }
|
||||
end
|
||||
end
|
Reference in New Issue