Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
ede467e934 | |||
|
145129641d | ||
|
99a268a89d | ||
|
c8edcddcf6 | ||
|
53033b8cdd | ||
|
29e7a0e4e2 |
5 changed files with 80 additions and 15 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1 +1,2 @@
|
||||||
functional.gemspec
|
functional.gemspec
|
||||||
|
pkg
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
|
Obsolete
|
||||||
|
========
|
||||||
|
|
||||||
|
Use `#lazy`.
|
||||||
|
|
||||||
Install
|
Install
|
||||||
=======
|
=======
|
||||||
|
|
||||||
|
|
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
0.1.3
|
0.1.5
|
||||||
|
|
|
@ -47,6 +47,7 @@ class Counter
|
||||||
end
|
end
|
||||||
|
|
||||||
class Functional
|
class Functional
|
||||||
|
def self.__version__() '0.1.5' end
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
class DEFAULT
|
class DEFAULT
|
||||||
|
@ -161,6 +162,11 @@ class Functional
|
||||||
end
|
end
|
||||||
|
|
||||||
class Each <Base
|
class Each <Base
|
||||||
|
def each_fun *a
|
||||||
|
@exe.call *a
|
||||||
|
end
|
||||||
|
alias call each_fun
|
||||||
|
|
||||||
def end
|
def end
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -213,25 +219,42 @@ class Functional
|
||||||
super *a, &exe
|
super *a, &exe
|
||||||
iv = Array.method :new if ::Functional::DEFAULT == iv
|
iv = Array.method :new if ::Functional::DEFAULT == iv
|
||||||
@buf = if iv.kind_of?( ::Proc) || iv.kind_of?( ::Method)
|
@buf = if iv.kind_of?( ::Proc) || iv.kind_of?( ::Method)
|
||||||
p default: :proc, iv: iv
|
|
||||||
Hash.new {|h,k| h[k] = iv.call }
|
Hash.new {|h,k| h[k] = iv.call }
|
||||||
else
|
else
|
||||||
p default: :value, iv: iv
|
|
||||||
{}.tap {|h| h.default = iv }
|
{}.tap {|h| h.default = iv }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def reduce_fun *a
|
def reduce_fun k, *a
|
||||||
@buf[ a[0]] = @exe.call @buf[ a[0]], *a[1..-1]
|
@buf[ k] = @exe.call k, @buf[ k], *a
|
||||||
end
|
end
|
||||||
alias call reduce_fun
|
alias call reduce_fun
|
||||||
|
|
||||||
def end
|
def end
|
||||||
@buf.each {|i| @next.call *i}
|
@buf.each &@next.method( :call)
|
||||||
@next.end
|
@next.end
|
||||||
end
|
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
|
class Slice <Base
|
||||||
def initialize n
|
def initialize n
|
||||||
@buf, @n = [], n
|
@buf, @n = [], n
|
||||||
|
@ -343,6 +366,7 @@ class Functional
|
||||||
def filter &exe
|
def filter &exe
|
||||||
push Filter.new( &exe)
|
push Filter.new( &exe)
|
||||||
end
|
end
|
||||||
|
alias reject filter
|
||||||
|
|
||||||
def compact
|
def compact
|
||||||
push Compact.new
|
push Compact.new
|
||||||
|
@ -362,8 +386,12 @@ class Functional
|
||||||
|
|
||||||
def each &exe
|
def each &exe
|
||||||
return self unless exe
|
return self unless exe
|
||||||
push Each.new
|
push Each.new( &exe)
|
||||||
push exe
|
run
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_hash
|
||||||
|
push ToHash.new
|
||||||
run
|
run
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -402,19 +430,21 @@ class Functional
|
||||||
def with_index &exe
|
def with_index &exe
|
||||||
i = 0
|
i = 0
|
||||||
exe ||= Array.method :[]
|
exe ||= Array.method :[]
|
||||||
push Collect.new {|*a| exe.call i, *a }
|
push Collect.new {|*a| exe.call i, *a; i += 1 }
|
||||||
end
|
end
|
||||||
|
|
||||||
def slice n, &e
|
def slice n, &exe
|
||||||
push Slice.new( n)
|
push Slice.new( n)
|
||||||
push Collect.new( &e) if e
|
block_given? ? self.collect( &exe) : self
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def cons n, &e
|
def cons n, &exe
|
||||||
push Cons.new( n)
|
push Cons.new( n)
|
||||||
push Collect.new( &e) if e
|
block_given? ? self.collect( &exe) : self
|
||||||
self
|
end
|
||||||
|
|
||||||
|
def tap &exe
|
||||||
|
push Tap.new( &exe)
|
||||||
end
|
end
|
||||||
|
|
||||||
class Save < Base
|
class Save < Base
|
||||||
|
|
29
test/functional_test.rb
Normal file
29
test/functional_test.rb
Normal file
|
@ -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 a new issue