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

From: tom@...
Date: 2015-07-01 15:52:21 UTC
List: ruby-core #69833
Issue #6284 has been updated by Tom Stuart.


I support the proposed `Proc#*`/`Method#*` syntax and semantics.

The feature being added is **function composition**; not relation compositi=
on, not method chaining. Its target audience is most likely to read `f * g`=
 as =E2=80=9C`f` after `g`=E2=80=9D, so that=E2=80=99s how it should work. =
Perhaps some Ruby programmers will not use this feature directly (as with `=
Proc#curry`) because they neither program nor think in a functional style, =
but it should be designed to be useful and familiar to those who do. The pr=
oposed implementation achieves that.

The asterisk isn=E2=80=99t ideal, but it=E2=80=99s the best choice availabl=
e.

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

* 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