File tree 2 files changed +31
-14
lines changed
2 files changed +31
-14
lines changed Original file line number Diff line number Diff line change @@ -14,6 +14,23 @@ def self.__from_block__(env)
14
14
end
15
15
end
16
16
17
+ def self . __make_curry_proc__ ( proc , passed , arity )
18
+ is_lambda = proc . lambda?
19
+ passed . freeze
20
+
21
+ __send__ ( ( is_lambda ? :lambda : :proc ) ) do |*argv , &passed_proc |
22
+ my_passed = passed + argv
23
+ if my_passed . length < arity
24
+ if !passed_proc . nil?
25
+ warn "#{ caller [ 0 ] } : given block not used"
26
+ end
27
+ __make_curry_proc__ ( proc , my_passed , arity )
28
+ else
29
+ proc . call ( *my_passed )
30
+ end
31
+ end
32
+ end
33
+
17
34
def self . new ( *args )
18
35
env = nil
19
36
@@ -94,17 +111,7 @@ def curry(curried_arity = nil)
94
111
95
112
args = [ ]
96
113
97
- my_self = self
98
- m = lambda? ? :lambda : :proc
99
- f = __send__ ( m ) { |*x |
100
- call_args = args + x
101
- if call_args . length >= my_self . arity
102
- my_self [ *call_args ]
103
- else
104
- args = call_args
105
- f
106
- end
107
- }
114
+ f = Proc . __make_curry_proc__ ( self , [ ] , arity )
108
115
109
116
f . singleton_class . send ( :define_method , :binding ) {
110
117
raise ArgumentError , "cannot create binding from f proc"
Original file line number Diff line number Diff line change 59
59
l = lambda { |*a | }
60
60
l . curry . arity . should == -1
61
61
end
62
-
62
+
63
63
it "produces Procs that raise ArgumentError for #binding" do
64
64
lambda do
65
65
@proc_add . curry . binding
66
66
end . should raise_error ( ArgumentError )
67
67
end
68
-
68
+
69
69
it "produces Procs that return [[:rest]] for #parameters" do
70
70
@proc_add . curry . parameters . should == [ [ :rest ] ]
71
71
end
72
-
72
+
73
73
it "produces Procs that return nil for #source_location" do
74
74
@proc_add . curry . source_location . should == nil
75
75
end
79
79
80
80
instance_exec ( 3 , &curried ) . should == 6
81
81
end
82
+
83
+ it "combines arguments and calculates incoming arity accurately for successively currying" do
84
+ l = lambda { |a , b , c | a +b +c }
85
+ l1 = l . curry . call ( 1 )
86
+ # the l1 currying seems unnecessary, but it triggered the original issue
87
+ l2 = l1 . curry . call ( 2 )
88
+
89
+ l2 . curry . call ( 3 ) . should == 6
90
+ l1 . curry . call ( 2 , 3 ) . should == 6
91
+ end
82
92
end
83
93
84
94
describe "Proc#curry with arity argument" do
You can’t perform that action at this time.
0 commit comments