Issue #6284 has been updated by yuroyoro (TOMOHITO Ozaki).


Most languages do not define function composition in built-in operators, but provide them as function or method such as `compose`.

F.Y.I) https://rosettacode.org/wiki/Function_composition

In some few languages defined function composition operator as following.

  haskell : `.`
  F# : `<<` and  `>>`
  Groovy : `<<` and  `>>`

I think Ruby should proivide `compose` method (and `and_then` as forward composition), and alias some operator (like `#<<` or `#*) to them.

In my opinion, +1 for `<<` and `>>` instead of `*`. 
Because there is no language define function composition as `*` and `+`, but F# and groovy defined as `<<` and `>>`. It is intutive.

By the way, It is useful if `Symbol#<<` (or `Symbol#+`) is shortcut method to `sym.to_proc.compose(other_proc)`.

```
arr.map(&:upcase.to_proc.compose(:to_s.to_proc))
arr.map(&:to_s >> :upcase)
```
It is more visually and intuitive.

My reference implemenation of composition is following.

https://github.com/yuroyoro/ruby/pull/7




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

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

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

    format_as_title = add_header << to_camel << :strip

instead of:

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


It's pretty easy to implement in pure ruby:

  class Proc
    def << block
      proc { |*args| self.call( block.to_proc.call(*args) ) }
    end
  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>