diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-01-04 14:32:19 +0900 |
|---|---|---|
| committer | GitHub <[email protected]> | 2019-01-04 14:32:19 +0900 |
| commit | 2a9ccf24496429547c1280d6d0f18525cb20d1a9 (patch) | |
| tree | dc798bb6c57c93a699b2b4aeea55835de7f1cd4c | |
| parent | acb1fdc79fb1a18f843a49e8c7dd58a5610678c7 (diff) | |
| parent | fc20d0186dfa3a6bd72b4cf1e09a1497c50b536b (diff) | |
| download | mruby-2a9ccf24496429547c1280d6d0f18525cb20d1a9.tar.gz mruby-2a9ccf24496429547c1280d6d0f18525cb20d1a9.zip | |
Merge pull request #4211 from dearblue/proc-composition
Add proc composition feature (CRuby-2.6 compatible)
| -rw-r--r-- | mrbgems/mruby-method/mrblib/method.rb | 8 | ||||
| -rw-r--r-- | mrbgems/mruby-method/test/method.rb | 19 | ||||
| -rw-r--r-- | mrbgems/mruby-proc-ext/mrblib/proc.rb | 8 | ||||
| -rw-r--r-- | mrbgems/mruby-proc-ext/test/proc.rb | 13 |
4 files changed, 48 insertions, 0 deletions
diff --git a/mrbgems/mruby-method/mrblib/method.rb b/mrbgems/mruby-method/mrblib/method.rb index 5de0afdf7..f7cefa2e5 100644 --- a/mrbgems/mruby-method/mrblib/method.rb +++ b/mrbgems/mruby-method/mrblib/method.rb @@ -17,4 +17,12 @@ class Method def name @name end + + def <<(other) + ->(*args, &block) { call(other.call(*args, &block)) } + end + + def >>(other) + ->(*args, &block) { other.call(call(*args, &block)) } + end end diff --git a/mrbgems/mruby-method/test/method.rb b/mrbgems/mruby-method/test/method.rb index 9fd6a558e..dfddde9cc 100644 --- a/mrbgems/mruby-method/test/method.rb +++ b/mrbgems/mruby-method/test/method.rb @@ -371,6 +371,25 @@ assert "Method#initialize_copy" do assert_equal(m1, m2) end +assert "Method#<< and Method#>>" do + obj = Object.new + class << obj + def mul2(n); n * 2; end + def add3(n); n + 3; end + end + + f = obj.method(:mul2) + g = obj.method(:add3) + + m1 = f << g + assert_kind_of Proc, m1 + assert_equal 16, m1.call(5) + + m2 = f >> g + assert_kind_of Proc, m2 + assert_equal 13, m2.call(5) +end + assert 'UnboundMethod#arity' do c = Class.new { def foo(a, b) diff --git a/mrbgems/mruby-proc-ext/mrblib/proc.rb b/mrbgems/mruby-proc-ext/mrblib/proc.rb index 789b7a3ac..abe9c7944 100644 --- a/mrbgems/mruby-proc-ext/mrblib/proc.rb +++ b/mrbgems/mruby-proc-ext/mrblib/proc.rb @@ -39,4 +39,12 @@ class Proc make_curry.call end + def <<(other) + ->(*args, &block) { call(other.call(*args, &block)) } + end + + def >>(other) + ->(*args, &block) { other.call(call(*args, &block)) } + end + end diff --git a/mrbgems/mruby-proc-ext/test/proc.rb b/mrbgems/mruby-proc-ext/test/proc.rb index 3e64bc103..1220841c8 100644 --- a/mrbgems/mruby-proc-ext/test/proc.rb +++ b/mrbgems/mruby-proc-ext/test/proc.rb @@ -77,6 +77,19 @@ assert('Kernel#proc') do end end +assert "Proc#<< and Proc#>>" do + add3 = ->(n) { n + 3 } + mul2 = ->(n) { n * 2 } + + f1 = mul2 << add3 + assert_kind_of Proc, f1 + assert_equal 16, f1.call(5) + + f2 = mul2 >> add3 + assert_kind_of Proc, f2 + assert_equal 13, f2.call(5) +end + assert('mrb_proc_new_cfunc_with_env') do ProcExtTest.mrb_proc_new_cfunc_with_env(:test) ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby) |
