From: ozaki@yuroyoro.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:82555] [Ruby trunk Feature#6284] Add composition for procs
Date: Thu, 31 Aug 2017 06:26:32 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-66365.20170831062631.ab1db2d068f35d02@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-6284.20120412152125@ruby-lang.org
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/
next prev parent reply other threads:[~2017-08-31 6:26 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-12 6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
2012-04-12 14:26 ` [ruby-core:44308] [ruby-trunk - Feature #6284] " trans (Thomas Sawyer)
2012-04-12 14:28 ` [ruby-core:44309] " trans (Thomas Sawyer)
2012-04-12 17:02 ` [ruby-core:44313] " pabloh (Pablo Herrero)
2012-04-12 21:26 ` [ruby-core:44319] " alexeymuranov (Alexey Muranov)
2012-04-12 21:39 ` [ruby-core:44320] " Adam Prescott
2012-04-12 22:53 ` [ruby-core:44321] [ruby-trunk - Feature #6284][Assigned] " mame (Yusuke Endoh)
2012-04-12 23:59 ` [ruby-core:44323] [ruby-trunk - Feature #6284] " pabloh (Pablo Herrero)
2012-04-13 5:56 ` [ruby-core:44327] " alexeymuranov (Alexey Muranov)
2012-04-15 8:10 ` [ruby-core:44365] " trans (Thomas Sawyer)
2012-04-16 2:59 ` [ruby-core:44373] " pabloh (Pablo Herrero)
2012-10-27 1:44 ` [ruby-core:48422] [ruby-trunk - Feature #6284][Feedback] " matz (Yukihiro Matsumoto)
2012-11-09 9:56 ` [ruby-core:49156] [ruby-trunk - Feature #6284] " jballanc (Joshua Ballanco)
2012-11-09 11:10 ` [ruby-core:49159] " rosenfeld (Rodrigo Rosenfeld Rosas)
2012-11-09 11:23 ` [ruby-core:49161] " rohitarondekar (Rohit Arondekar)
2012-11-09 16:25 ` [ruby-core:49169] " alexeymuranov (Alexey Muranov)
2012-11-09 17:42 ` [ruby-core:49171] " marcandre (Marc-Andre Lafortune)
2012-11-10 3:06 ` [ruby-core:49182] " duerst (Martin Dürst)
2012-11-10 4:00 ` [ruby-core:49184] " Matthew Kerwin
2012-11-10 9:23 ` [ruby-core:49190] " alexeymuranov (Alexey Muranov)
2012-11-10 12:33 ` [ruby-core:49197] " rosenfeld (Rodrigo Rosenfeld Rosas)
2012-11-24 1:31 ` [ruby-core:49947] " mame (Yusuke Endoh)
2012-12-01 19:54 ` [ruby-core:50461] " rits (First Last)
2012-12-05 3:13 ` [ruby-core:50563] " boris_stitnicky (Boris Stitnicky)
2012-12-05 10:47 ` [ruby-core:50568] " rosenfeld (Rodrigo Rosenfeld Rosas)
2012-12-05 11:23 ` [ruby-core:50569] " alexeymuranov (Alexey Muranov)
2015-06-14 16:55 ` [ruby-core:69586] [Ruby trunk " mudge
2015-06-23 14:47 ` [ruby-core:69712] " mudge
2015-06-29 8:39 ` [ruby-core:69768] " mudge
2015-06-29 21:32 ` [ruby-core:69777] " pablodherrero
2015-06-30 8:21 ` [ruby-core:69812] " duerst
2015-06-30 8:35 ` [ruby-core:69813] " eregontp
2015-06-30 11:37 ` [ruby-core:69816] " pablodherrero
2015-06-30 12:39 ` [ruby-core:69817] " mudge
2015-07-01 15:52 ` [ruby-core:69833] " tom
2015-08-07 7:21 ` [ruby-core:70264] " systho
2015-12-30 11:18 ` [ruby-core:72616] " mudge
2015-12-30 12:28 ` [ruby-core:72618] " mudge
2016-10-09 14:30 ` [ruby-core:77534] [Ruby trunk Feature#6284] " bigbadmath
2016-10-11 20:18 ` [ruby-core:77595] " mudge
2016-10-12 8:28 ` [ruby-core:77597] " mudge
2016-10-12 9:46 ` [ruby-core:77599] " duerst
2017-01-22 18:50 ` [ruby-core:79219] " t.hirsch
2017-03-13 8:13 ` [ruby-core:80110] " matz
2017-04-28 19:47 ` [ruby-core:80931] " ritchie
2017-08-31 6:26 ` ozaki [this message]
2018-01-29 7:49 ` [ruby-core:85213] " zverok.offline
2018-04-20 18:34 ` [ruby-core:86632] " keystonelemur
2018-04-30 18:51 ` [ruby-core:86780] " keystonelemur
2018-05-17 5:56 ` [ruby-core:87100] [Ruby trunk Feature#6284][Open] " matz
2018-07-30 8:13 ` [ruby-core:88188] [Ruby trunk Feature#6284] " melentievm
2018-08-08 5:41 ` [ruby-core:88339] " ko1
2018-08-08 18:37 ` [ruby-core:88356] " shannonskipper
2018-08-10 19:58 ` [ruby-core:88442] " shannonskipper
2018-11-12 12:43 ` [ruby-core:89775] " nobu
2018-11-14 17:33 ` [ruby-core:89796] " pablodherrero
2018-11-15 2:22 ` [ruby-core:89801] " nobu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-66365.20170831062631.ab1db2d068f35d02@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).