| 1 |
class Proc |
| 2 |
|
| 3 |
def ===(*args) |
| 4 |
call(*args) |
| 5 |
end |
| 6 |
|
| 7 |
def yield(*args) |
| 8 |
call(*args) |
| 9 |
end |
| 10 |
|
| 11 |
def to_proc |
| 12 |
self |
| 13 |
end |
| 14 |
|
| 15 |
def curry(arity=self.arity) |
| 16 |
type = :proc |
| 17 |
abs = lambda {|a| a < 0 ? -a - 1 : a} |
| 18 |
arity = abs[arity] |
| 19 |
if lambda? |
| 20 |
type = :lambda |
| 21 |
self_arity = self.arity |
| 22 |
if (self_arity >= 0 && arity != self_arity) || |
| 23 |
(self_arity < 0 && abs[self_arity] > arity) |
| 24 |
raise ArgumentError, "wrong number of arguments (given #{arity}, expected #{abs[self_arity]})" |
| 25 |
end |
| 26 |
end |
| 27 |
|
| 28 |
pproc = self |
| 29 |
make_curry = proc do |given_args=[]| |
| 30 |
__send__(type) do |*args| |
| 31 |
new_args = given_args + args |
| 32 |
if new_args.size >= arity |
| 33 |
pproc[*new_args] |
| 34 |
else |
| 35 |
make_curry[new_args] |
| 36 |
end |
| 37 |
end |
| 38 |
end |
| 39 |
make_curry.call |
| 40 |
end |
| 41 |
|
| 42 |
def <<(other) |
| 43 |
->(*args, **opts, &block) { call(other.call(*args, **opts, &block)) } |
| 44 |
end |
| 45 |
|
| 46 |
def >>(other) |
| 47 |
->(*args, **opts, &block) { other.call(call(*args, **opts, &block)) } |
| 48 |
end |
| 49 |
|
| 50 |
end |