Compare commits

...

6 Commits

Author SHA1 Message Date
Denis Knauf ede467e934 „README.md“ ändern 2021-12-03 18:40:08 +01:00
Denis Knauf 145129641d v0.1.5 2011-12-16 12:45:52 +01:00
Denis Knauf 99a268a89d more tests 2011-12-16 12:45:22 +01:00
Denis Knauf c8edcddcf6 tests added 2011-12-16 11:42:45 +01:00
Denis Knauf 53033b8cdd bugfixes; tap. v0.1.4 2011-12-16 11:22:48 +01:00
Denis Knauf 29e7a0e4e2 ignore pkg-dir 2011-11-24 11:50:08 +01:00
5 changed files with 80 additions and 15 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
functional.gemspec
pkg

View File

@ -1,3 +1,8 @@
Obsolete
========
Use `#lazy`.
Install
=======

View File

@ -1 +1 @@
0.1.3
0.1.5

View File

@ -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

29
test/functional_test.rb Normal file
View 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