ludy 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +26 -0
- data/lib/ludy.rb +1 -1
- data/lib/ludy/callstack.rb +1 -1
- data/lib/ludy/curry.rb +44 -0
- data/lib/ludy/dice.rb +1 -1
- data/lib/ludy/lazy.rb +1 -1
- data/lib/ludy/ludy_ext.rb +57 -4
- data/lib/ludy/rambda.rb +1 -1
- data/lib/ludy/this.rb +1 -1
- data/lib/ludy/variable.rb +1 -1
- data/lib/ludy/y_combinator.rb +1 -1
- data/lib/ludy/z_combinator.rb +1 -1
- data/ludy.gemspec +2 -2
- data/test/tc_callstack.rb +1 -1
- data/test/tc_curry.rb +49 -0
- data/test/tc_dice.rb +1 -1
- data/test/tc_lazy.rb +1 -1
- data/test/tc_ludy_ext.rb +62 -7
- data/test/tc_rambda.rb +2 -4
- data/test/tc_this.rb +1 -1
- data/test/tc_variable.rb +1 -1
- data/test/tc_y_combinator.rb +1 -1
- data/test/tc_z_combinator.rb +1 -1
- data/test/ts_ludy.rb +1 -1
- metadata +5 -2
data/CHANGES
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
==============================
|
3
|
+
ludy 0.0.3, 2007.08
|
4
|
+
|
5
|
+
ludy_ext:
|
6
|
+
added:
|
7
|
+
1. Proc#curry
|
8
|
+
2. Proc#compose
|
9
|
+
3. Proc#chain
|
10
|
+
4. Symbol#to_proc
|
11
|
+
5. Array#foldl
|
12
|
+
6. Array#foldr
|
13
|
+
7. Array#filter
|
14
|
+
|
15
|
+
removed:
|
16
|
+
1. Fixnum#collect # see tc_ludy_ext.rb#test_fixnum_collect for reason
|
17
|
+
|
18
|
+
info:
|
19
|
+
1. ruby2ruby has NilClass#method_missing return nil,
|
20
|
+
so i can't just make it return blackhole
|
21
|
+
|
22
|
+
4. module Curry
|
23
|
+
see test/tc_curry.rb for usage
|
24
|
+
|
25
|
+
see unit test for usage
|
26
|
+
==============================
|
data/lib/ludy.rb
CHANGED
data/lib/ludy/callstack.rb
CHANGED
data/lib/ludy/curry.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
begin
|
18
|
+
require_ludy 'ludy_ext'
|
19
|
+
rescue NameError
|
20
|
+
raise LoadError.new('please require "ludy" first')
|
21
|
+
end
|
22
|
+
|
23
|
+
module Ludy
|
24
|
+
|
25
|
+
module Curry
|
26
|
+
def self.included target
|
27
|
+
target.module_eval{
|
28
|
+
instance_methods.each{ |m|
|
29
|
+
next unless m =~ /^\w/
|
30
|
+
module_eval <<-END
|
31
|
+
def c#{m} *args, &block
|
32
|
+
if args.size == method(:#{m}).arity
|
33
|
+
self.__send__ :#{m}, *args, &block
|
34
|
+
else
|
35
|
+
method(:c#{m}).to_proc.curry *args
|
36
|
+
end
|
37
|
+
end
|
38
|
+
END
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/ludy/dice.rb
CHANGED
data/lib/ludy/lazy.rb
CHANGED
data/lib/ludy/ludy_ext.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
# Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
|
4
4
|
#
|
@@ -14,6 +14,8 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
|
+
require 'singleton'
|
18
|
+
|
17
19
|
class Object
|
18
20
|
def tap
|
19
21
|
yield self
|
@@ -34,12 +36,23 @@ class Object
|
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
=begin
|
40
|
+
class Blackhole # < NilClass
|
41
|
+
include Singleton
|
42
|
+
def method_missing msg, *arg, █ self; end
|
43
|
+
def nil?; true; end
|
44
|
+
def null?; true; end
|
45
|
+
def blackhole?; true; end
|
46
|
+
def to_bool; false; end
|
47
|
+
end
|
48
|
+
=end
|
49
|
+
|
50
|
+
# module Kernel; def blackhole; Blackhole.instance; end; end
|
37
51
|
class NilClass
|
38
|
-
def method_missing msg, *arg, &block
|
39
|
-
self
|
40
|
-
end
|
52
|
+
def method_missing msg, *arg, █ self; end
|
41
53
|
end
|
42
54
|
|
55
|
+
=begin
|
43
56
|
class Fixnum
|
44
57
|
def collect
|
45
58
|
result = []
|
@@ -47,3 +60,43 @@ class Fixnum
|
|
47
60
|
result
|
48
61
|
end
|
49
62
|
end
|
63
|
+
=end
|
64
|
+
|
65
|
+
class Symbol
|
66
|
+
def to_proc; lambda{|*args| args.shift.__send__ self, *args }; end
|
67
|
+
end
|
68
|
+
|
69
|
+
class Array
|
70
|
+
alias_method :filter, :select
|
71
|
+
def foldl func, init; self.inject init, &func; end
|
72
|
+
def foldr func, init
|
73
|
+
self.reverse_each{ |i|
|
74
|
+
init = func[i, init]
|
75
|
+
}
|
76
|
+
init
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
class Proc
|
81
|
+
def curry *pre
|
82
|
+
lambda{ |*post| self[*(pre + post)] }
|
83
|
+
end
|
84
|
+
def chain *procs, &block
|
85
|
+
procs << block if block
|
86
|
+
lambda{ |*args|
|
87
|
+
result = []
|
88
|
+
([self] + procs).each{ |i|
|
89
|
+
result += [i[*args]].flatten
|
90
|
+
}
|
91
|
+
result
|
92
|
+
}
|
93
|
+
end
|
94
|
+
def compose *procs, &block
|
95
|
+
procs << block if block
|
96
|
+
lambda{ |*args|
|
97
|
+
([self] + procs).reverse.inject(args){ |val, fun|
|
98
|
+
fun[*val]
|
99
|
+
}
|
100
|
+
}
|
101
|
+
end
|
102
|
+
end
|
data/lib/ludy/rambda.rb
CHANGED
data/lib/ludy/this.rb
CHANGED
data/lib/ludy/variable.rb
CHANGED
data/lib/ludy/y_combinator.rb
CHANGED
data/lib/ludy/z_combinator.rb
CHANGED
data/ludy.gemspec
CHANGED
@@ -18,7 +18,7 @@ require 'rubygems'
|
|
18
18
|
|
19
19
|
spec = Gem::Specification.new{|s|
|
20
20
|
s.name = 'ludy'
|
21
|
-
s.version = '0.0.
|
21
|
+
s.version = '0.0.3'
|
22
22
|
s.author = 'Lin Jen-Shin(a.k.a. godfat)'
|
23
23
|
s.email = 'strip number: 135godfat7911@246gmail.890com'
|
24
24
|
s.homepage = 'http://ludy.rubyforge.org/'
|
@@ -27,7 +27,7 @@ spec = Gem::Specification.new{|s|
|
|
27
27
|
candidates = Dir.glob '{bin,doc,lib,test}/**/*'
|
28
28
|
candidates+= Dir.glob '*'
|
29
29
|
s.files = candidates.delete_if{|item|
|
30
|
-
item.include?('
|
30
|
+
item.include?('rdoc') || File.extname(item) == '.gem'
|
31
31
|
}
|
32
32
|
|
33
33
|
s.require_path = 'lib'
|
data/test/tc_callstack.rb
CHANGED
data/test/tc_curry.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'test/unit'
|
18
|
+
require(File.join(File.dirname(__FILE__), '..', 'lib', 'ludy'))
|
19
|
+
require_ludy 'curry'
|
20
|
+
require_ludy 'ludy_ext'
|
21
|
+
include Ludy
|
22
|
+
class Array
|
23
|
+
def test_curry a, b, c, d, e
|
24
|
+
[a, b, c, d, e]
|
25
|
+
end
|
26
|
+
include Curry
|
27
|
+
end
|
28
|
+
class TestCurry < Test::Unit::TestCase
|
29
|
+
def test_curry
|
30
|
+
func1 = [1,2,3].cfoldr[:-.to_proc]
|
31
|
+
assert_equal 2, func1[0]
|
32
|
+
|
33
|
+
assert_equal [8,10,12], [4,5,6].cmap(&:*.to_proc.curry(2))
|
34
|
+
|
35
|
+
result = [2,3,4,5,6]
|
36
|
+
func2 = result.ctest_curry[2]
|
37
|
+
func3 = func2[3]
|
38
|
+
func4 = func3[4]
|
39
|
+
func5 = func4[5]
|
40
|
+
func6 = func5[6]
|
41
|
+
|
42
|
+
assert_equal result, result.ctest_curry[2,3,4,5,6]
|
43
|
+
assert_equal result, func2[3,4,5,6]
|
44
|
+
assert_equal result, func3[4,5,6]
|
45
|
+
assert_equal result, func4[5,6]
|
46
|
+
assert_equal result, func5[6]
|
47
|
+
assert_equal result, func6
|
48
|
+
end
|
49
|
+
end
|
data/test/tc_dice.rb
CHANGED
data/test/tc_lazy.rb
CHANGED
data/test/tc_ludy_ext.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
# Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
|
4
4
|
#
|
@@ -21,16 +21,18 @@ class TestLudyExt < Test::Unit::TestCase
|
|
21
21
|
def test_object_tap
|
22
22
|
assert_equal '11', 10.tap{|i| assert_equal '10', i.to_s}.succ.to_s
|
23
23
|
end
|
24
|
-
def
|
25
|
-
|
24
|
+
def test_blackhole
|
25
|
+
assert_equal nil, nil.XD.Orz.zzz
|
26
26
|
end
|
27
27
|
def test_fixnum_collect
|
28
|
-
a, b, c = 3.collect{|i| i}
|
28
|
+
# a, b, c = 3.collect{|i| i}
|
29
|
+
a, b, c = (0..2).to_a
|
29
30
|
assert_equal 0, a
|
30
31
|
assert_equal 1, b
|
31
32
|
assert_equal 2, c
|
32
33
|
|
33
|
-
array = 5.collect{Array.new}
|
34
|
+
# array = 5.collect{Array.new}
|
35
|
+
array = Array.new(5).map{[]}
|
34
36
|
assert_equal 5, array.size
|
35
37
|
5.times{|y|
|
36
38
|
5.times{|x|
|
@@ -42,12 +44,65 @@ class TestLudyExt < Test::Unit::TestCase
|
|
42
44
|
end
|
43
45
|
def test_if
|
44
46
|
assert_equal "XD", (true ).if{"XD"}
|
45
|
-
assert
|
47
|
+
assert (false).if{"XD"}.nil?
|
46
48
|
assert_equal "Orz", (false).if{"XD"}.else{"Orz"}
|
47
49
|
assert_equal "XD", (true ).if{"XD"}.else{"Orz"}
|
48
50
|
assert_equal "XD", (true ).if{"xd"}.upcase
|
49
|
-
assert
|
51
|
+
assert (false).if{"xd"}.upcase.nil?
|
50
52
|
assert_equal "OTL", (false).if{"xd"}.else{"otl"}.upcase
|
51
53
|
assert_equal "XD", (true ).if{"xd"}.else{"otl"}.upcase
|
52
54
|
end
|
55
|
+
def test_filter
|
56
|
+
assert_equal [1,2,3], [1,18,29,9,4,3,2,1,3,7].filter{|i| i<=3}.sort.uniq
|
57
|
+
end
|
58
|
+
def test_folds
|
59
|
+
assert_equal 6, [1,2,3].foldl(:+.to_proc, 0)
|
60
|
+
assert_equal -6, [1,2,3].foldl(:-.to_proc, 0)
|
61
|
+
assert_equal 6, [1,2,3].foldr(:+.to_proc, 0)
|
62
|
+
assert_equal 2, [1,2,3].foldr(:-.to_proc, 0)
|
63
|
+
end
|
64
|
+
def test_proc_curry
|
65
|
+
multiply = lambda{|l,r| l*r}
|
66
|
+
|
67
|
+
double = multiply.curry 2
|
68
|
+
assert_equal 8, double[4]
|
69
|
+
assert_equal 6, double[3]
|
70
|
+
|
71
|
+
xd = multiply.curry 'XD', 5
|
72
|
+
assert_equal 'XDXDXDXDXD', xd.call
|
73
|
+
|
74
|
+
assert_equal 29, :+.to_proc.curry(18)[11]
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_proc_chain
|
78
|
+
f1 = lambda{|v| v+1}
|
79
|
+
assert_equal 5, f1[4]
|
80
|
+
|
81
|
+
f2 = lambda{|v| v+2}
|
82
|
+
assert_equal 6, f2[4]
|
83
|
+
|
84
|
+
f3 = f1.chain f2
|
85
|
+
assert_equal [6,7], f3[5]
|
86
|
+
|
87
|
+
f4 = f3.chain f1
|
88
|
+
assert_equal [2,3,2], f4[1]
|
89
|
+
|
90
|
+
f5 = f4.chain{|v|[10,11,v]}
|
91
|
+
assert_equal [1,2,1,10,11,0], f5[0]
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_proc_compose
|
95
|
+
f1 = lambda{|v| v+1}
|
96
|
+
f2 = lambda{|v| v*2}
|
97
|
+
f3 = f1.compose f2
|
98
|
+
assert_equal 21, f3[10]
|
99
|
+
|
100
|
+
f4 = lambda{|a,b| a*b}
|
101
|
+
f5 = lambda{|a,b| [a*b, a-b]}
|
102
|
+
f6 = f4.compose f5
|
103
|
+
assert_equal -30, f6[3,5]
|
104
|
+
|
105
|
+
f7 = lambda{|a| a*2}.compose f6.compose{|a,b| [b,a]}
|
106
|
+
assert_equal 60, f7[3,5]
|
107
|
+
end
|
53
108
|
end
|
data/test/tc_rambda.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/ruby
|
1
|
+
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
# Copyright (c) 2007, Lin Jen-Shin(a.k.a. godfat 真常)
|
4
4
|
#
|
@@ -25,8 +25,6 @@ class TestRambda < Test::Unit::TestCase
|
|
25
25
|
(0...10).map(&rambda{|n| n<=1 ? 1 : this[n-2]+this[n-1]}))
|
26
26
|
|
27
27
|
v = "can't refer v"
|
28
|
-
|
29
|
-
rambda{v}.call
|
30
|
-
}
|
28
|
+
assert_equal nil, rambda{v}.call
|
31
29
|
end
|
32
30
|
end
|
data/test/tc_this.rb
CHANGED
data/test/tc_variable.rb
CHANGED
data/test/tc_y_combinator.rb
CHANGED
data/test/tc_z_combinator.rb
CHANGED
data/test/ts_ludy.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ludy
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2007-07
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2007-08-07 00:00:00 +08:00
|
8
8
|
summary: Aims to extend Ruby standard library, providing some useful tools that's not existed in the standard library.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/lib/smulti.rb
|
36
36
|
- lib/ludy
|
37
37
|
- lib/ludy/callstack.rb
|
38
|
+
- lib/ludy/curry.rb
|
38
39
|
- lib/ludy/dice.rb
|
39
40
|
- lib/ludy/lazy.rb
|
40
41
|
- lib/ludy/ludy_ext.rb
|
@@ -45,6 +46,7 @@ files:
|
|
45
46
|
- lib/ludy/z_combinator.rb
|
46
47
|
- lib/ludy.rb
|
47
48
|
- test/tc_callstack.rb
|
49
|
+
- test/tc_curry.rb
|
48
50
|
- test/tc_dice.rb
|
49
51
|
- test/tc_lazy.rb
|
50
52
|
- test/tc_ludy_ext.rb
|
@@ -54,6 +56,7 @@ files:
|
|
54
56
|
- test/tc_y_combinator.rb
|
55
57
|
- test/tc_z_combinator.rb
|
56
58
|
- test/ts_ludy.rb
|
59
|
+
- CHANGES
|
57
60
|
- lib
|
58
61
|
- LICENSE
|
59
62
|
- ludy.gemspec
|