Issue #6284 has been updated by printercu (Max Melentiev).


matz (Yukihiro Matsumoto) wrote:
> Considering the combination of OOP and FP, it seems a good idea to adding both forward and reverse combination of procs. So we pick Groovy way (adding `<<` and `>>` methods to `Proc`).

What do you think about selecting only single operation? Here is Groovy example from provided link:

```
final times2 = { it * 2 }
final plus1 = { it + 1 }
 
final plus1_then_times2 = times2 << plus1
final times2_then_plus1 = times2 >> plus1
```

It looks like `<<` is less intuitive and readable. I'm also really afraid of having chance to read something like `a >> b << c >> d`.


----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-73207

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  end
end
```

---Files--------------------------------
0001-proc.c-Implement-Proc-for-Proc-composition.patch (3.65 KB)
0002-proc.c-Implement-Method-for-Method-composition.patch (2.67 KB)
0003-proc.c-Support-any-callable-when-composing-Procs.patch (3.97 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>