From: keystonelemur@... Date: 2018-04-20T18:34:28+00:00 Subject: [ruby-core:86632] [Ruby trunk Feature#6284] Add composition for procs Issue #6284 has been updated by baweaver (Brandon Weaver). yuroyoro (TOMOHITO Ozaki) wrote: > 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 I do like the idea of using shovel as it's already fairly common in the language, and present in others. I'd be interested in potentially making a container variant though: ~~~ ruby arr.map(&:to_s >> :upcase) # becomes arr.map(&[:to_s, :upcase]) ~~~ Though that would involve introducing the idea of Array#to_proc. I've used a similar technique in a few gems with `#[]()`, so a vague implementation may look like: ```ruby class Array def to_proc self[1..].reduce(self[0].to_proc, :compose) end end ``` Wherein it'd be nice if `#compose` tried to coerce its argument: ```ruby def compose(sym) fn = sym.is_a?(Proc) ? sym : sym.to_proc ... end ``` Currently planning on releasing a gem this weekend that will do a similar thing, it could serve as inspiration for an implementation, but my C is no where near good enough to try at the Ruby core level. Aside: Have we ever considered implicitly `to_proc`ing anything passed to a method expecting a block? e.g. `arr.map(&:to_s)` becomes `arr.map(:to_s)` ---------------------------------------- Feature #6284: Add composition for procs https://bugs.ruby-lang.org/issues/6284#change-71589 * 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: