[ruby-core:69768] [Ruby trunk - Feature #6284] Add composition for procs

From: mudge@...
Date: 2015-06-29 08:39:22 UTC
List: ruby-core #69768
Issue #6284 has been updated by Paul Mucur.


Regarding the syntax: I also support `*` as the operator where `f * g =3D f=
(g(x))` (as it seems close enough to the mathematical syntax already used b=
y other languages such as Haskell and Idris) but if that is too divisive, w=
e could choose a method name from the mathematical definition (https://en.w=
ikipedia.org/wiki/Function_composition) instead:

> The notation g=E2=80=89=E2=88=98=E2=80=89f is read as "g circle f ", or "=
g round f ", or "g composed with f ", "g after f ", "g following f ", or "g=
 of f", or "g on f ".

This opens up the following options:

* `Proc#compose`: `f.compose(g) #=3D> f(g(x))`
* `Proc#after`: `f.after(g) #=3D> f(g(x))`
* `Proc#following`: `f.following(g) #=3D> f(g(x))`
* `Proc#of`: `f.of(g) #=3D> f(g(x))`
* `Proc#on`: `f.on(g) #=3D> f(g(x))`

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=3Dbegin
It would be nice to be able to compose procs like functions in functional p=
rogramming languages:

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

    format_as_title =3D add_header << to_camel << :strip

instead of:

    format_as_title =3D 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
=3Dend

---Files--------------------------------
0002-proc.c-Implement-Method-for-Method-composition.patch (2.71 KB)
0001-proc.c-Implement-Proc-for-Proc-composition.patch (3.73 KB)
0003-proc.c-Support-any-callable-when-composing-Procs.patch (4.01 KB)


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

In This Thread