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

From: pablodherrero@...
Date: 2015-06-30 11:37:45 UTC
List: ruby-core #69816
Issue #6284 has been updated by Pablo Herrero.


Martin D=C3=BCrst wrote:
> I'm teaching Haskell in a graduate class, so I'm quite familiar with func=
tion composition and use it a lot, but the original example isn't convincin=
g at all. For me, in Ruby, something like val.strip.capitalize reads much, =
much better than some artificially forced function composition. If there we=
re a method String#prepend, it would be even more natural: val.strip.capita=
lize.prepend('Title: ').
>=20
> If there are better examples that feel more natural in Ruby, please post =
them here.

I don't believe you need a pure PF language to benefit from a feature like =
this. Many ETL projects like transproc (https://github.com/solnic/transproc=
) would probably find it useful too.

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

* 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