diff options
| author | h2so5 <[email protected]> | 2013-04-17 07:01:32 +0900 |
|---|---|---|
| committer | h2so5 <[email protected]> | 2013-04-17 22:59:57 +0900 |
| commit | 1a375c6fd4a3b7d797754e19d0a0896d2bbd5e9a (patch) | |
| tree | bb97c35a42b0c3cedb0c43ff0c83051c5fe8eff1 /mrbgems/mruby-proc-ext/mrblib | |
| parent | 4b24ee189ef5b2190562ce4c67d48c3170bdc2a2 (diff) | |
| download | mruby-1a375c6fd4a3b7d797754e19d0a0896d2bbd5e9a.tar.gz mruby-1a375c6fd4a3b7d797754e19d0a0896d2bbd5e9a.zip | |
Add mruby-proc-ext
Diffstat (limited to 'mrbgems/mruby-proc-ext/mrblib')
| -rw-r--r-- | mrbgems/mruby-proc-ext/mrblib/proc.rb | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/mrbgems/mruby-proc-ext/mrblib/proc.rb b/mrbgems/mruby-proc-ext/mrblib/proc.rb new file mode 100644 index 000000000..5dd9981df --- /dev/null +++ b/mrbgems/mruby-proc-ext/mrblib/proc.rb @@ -0,0 +1,40 @@ +class Proc + + def ===(*args) + call(*args) + end + + def yield(*args) + call(*args) + end + + def to_proc + self + end + + def curry(arity=self.arity) + abs = lambda {|a| a < 0 ? -a - 1 : a} + arity = abs[arity] + if lambda? + self_arity = self.arity + if (self_arity >= 0 && arity != self_arity) || + (self_arity < 0 && abs[self_arity] > arity) + raise ArgumentError, "wrong number of arguments (#{arity} for #{abs[self_arity]})" + end + end + + pproc = self + make_curry = proc do |given_args=[]| + proc do |*args| + new_args = given_args + args + if new_args.size >= arity + pproc[*new_args] + else + make_curry[new_args] + end + end + end + make_curry.call + end + +end |
