ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs
@ 2012-04-12  6:21 pabloh (Pablo Herrero)
  2012-04-12 14:26 ` [ruby-core:44308] [ruby-trunk - Feature #6284] " trans (Thomas Sawyer)
                   ` (53 more replies)
  0 siblings, 54 replies; 57+ messages in thread
From: pabloh (Pablo Herrero) @ 2012-04-12  6:21 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been reported by pabloh (Pablo Herrero).

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

Author: pabloh (Pablo Herrero)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44308] [ruby-trunk - Feature #6284] Add composition for procs
  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 ` trans (Thomas Sawyer)
  2012-04-12 14:28 ` [ruby-core:44309] " trans (Thomas Sawyer)
                   ` (52 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: trans (Thomas Sawyer) @ 2012-04-12 14:26 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by trans (Thomas Sawyer).


=begin
This makes assumptions about arity. Try:

    format_as_title = ->{ |val| add_header[to_camel[val.strip]] }
=end

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

Author: pabloh (Pablo Herrero)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44309] [ruby-trunk - Feature #6284] Add composition for procs
  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 ` trans (Thomas Sawyer)
  2012-04-12 17:02 ` [ruby-core:44313] " pabloh (Pablo Herrero)
                   ` (51 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: trans (Thomas Sawyer) @ 2012-04-12 14:28 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by trans (Thomas Sawyer).


Also, I think #+ is better.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25856

Author: pabloh (Pablo Herrero)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44313] [ruby-trunk - Feature #6284] Add composition for procs
  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 ` pabloh (Pablo Herrero)
  2012-04-12 21:26 ` [ruby-core:44319] " alexeymuranov (Alexey Muranov)
                   ` (50 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: pabloh (Pablo Herrero) @ 2012-04-12 17:02 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by pabloh (Pablo Herrero).


trans (Thomas Sawyer) wrote:
> Also, I think #+ is better.

I saw facets has some similar feature that uses #* instead, maybe because it looks a bit closer to Haskell's composition syntax. Nevertheless, I still like #<< better, it feels that your are "connecting" the blocks together.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25860

Author: pabloh (Pablo Herrero)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44319] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (2 preceding siblings ...)
  2012-04-12 17:02 ` [ruby-core:44313] " pabloh (Pablo Herrero)
@ 2012-04-12 21:26 ` 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)
                   ` (49 subsequent siblings)
  53 siblings, 1 reply; 57+ messages in thread
From: alexeymuranov (Alexey Muranov) @ 2012-04-12 21:26 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by alexeymuranov (Alexey Muranov).


I would vote for #*.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25865

Author: pabloh (Pablo Herrero)
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44320] Re: [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12 21:26 ` [ruby-core:44319] " alexeymuranov (Alexey Muranov)
@ 2012-04-12 21:39   ` Adam Prescott
  0 siblings, 0 replies; 57+ messages in thread
From: Adam Prescott @ 2012-04-12 21:39 UTC (permalink / raw)
  To: ruby-core

[-- Attachment #1: Type: text/plain, Size: 101 bytes --]

See also:
http://web.archive.org/web/20101228224741/http://drmcawesome.com/FunctionCompositionInRuby

[-- Attachment #2: Type: text/html, Size: 208 bytes --]

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44321] [ruby-trunk - Feature #6284][Assigned] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (3 preceding siblings ...)
  2012-04-12 21:26 ` [ruby-core:44319] " alexeymuranov (Alexey Muranov)
@ 2012-04-12 22:53 ` mame (Yusuke Endoh)
  2012-04-12 23:59 ` [ruby-core:44323] [ruby-trunk - Feature #6284] " pabloh (Pablo Herrero)
                   ` (48 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mame (Yusuke Endoh) @ 2012-04-12 22:53 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by mame (Yusuke Endoh).

Status changed from Open to Assigned
Assignee set to matz (Yukihiro Matsumoto)


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

Author: pabloh (Pablo Herrero)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44323] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (4 preceding siblings ...)
  2012-04-12 22:53 ` [ruby-core:44321] [ruby-trunk - Feature #6284][Assigned] " mame (Yusuke Endoh)
@ 2012-04-12 23:59 ` pabloh (Pablo Herrero)
  2012-04-13  5:56 ` [ruby-core:44327] " alexeymuranov (Alexey Muranov)
                   ` (47 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: pabloh (Pablo Herrero) @ 2012-04-12 23:59 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by pabloh (Pablo Herrero).


=begin

aprescott (Adam Prescott) wrote:
> See also: http://web.archive.org/web/20101228224741/http://drmcawesome.com/FunctionCompositionInRuby


Maybe #| could be a possibility. (Without implementing #> or #<).

But I find the article's proposition about the chaining order a bit missleading:

    transform = add1 | sub3 | negate

For me that feels more like "piping"  ((|add1|)) to ((|sub3|)) to ((|negate|)), from left to right, not the other way around.


If we choose to take that path I think the following code would be a plausible implementation:

    class Proc
      def | block
        proc { |*args| block.to_proc.call( self.call(*args) ) }
      end
    end
    
    class Symbol
      def | block
        self.to_proc | block
      end
    end
=end
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25869

Author: pabloh (Pablo Herrero)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44327] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (5 preceding siblings ...)
  2012-04-12 23:59 ` [ruby-core:44323] [ruby-trunk - Feature #6284] " pabloh (Pablo Herrero)
@ 2012-04-13  5:56 ` alexeymuranov (Alexey Muranov)
  2012-04-15  8:10 ` [ruby-core:44365] " trans (Thomas Sawyer)
                   ` (46 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: alexeymuranov (Alexey Muranov) @ 2012-04-13  5:56 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by alexeymuranov (Alexey Muranov).


What about #* for composing traditionally (right to left) and #| for piping (left to right)? In mathematics, depending of the area and the subject, both ways are used, and some argue that "piping" is more natural than "precomposing".
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25874

Author: pabloh (Pablo Herrero)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44365] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (6 preceding siblings ...)
  2012-04-13  5:56 ` [ruby-core:44327] " alexeymuranov (Alexey Muranov)
@ 2012-04-15  8:10 ` trans (Thomas Sawyer)
  2012-04-16  2:59 ` [ruby-core:44373] " pabloh (Pablo Herrero)
                   ` (45 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: trans (Thomas Sawyer) @ 2012-04-15  8:10 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by trans (Thomas Sawyer).


I agree, `#*` is appropriate for composition.



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

Author: pabloh (Pablo Herrero)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:44373] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (7 preceding siblings ...)
  2012-04-15  8:10 ` [ruby-core:44365] " trans (Thomas Sawyer)
@ 2012-04-16  2:59 ` pabloh (Pablo Herrero)
  2012-10-27  1:44 ` [ruby-core:48422] [ruby-trunk - Feature #6284][Feedback] " matz (Yukihiro Matsumoto)
                   ` (44 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: pabloh (Pablo Herrero) @ 2012-04-16  2:59 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by pabloh (Pablo Herrero).


alexeymuranov (Alexey Muranov) wrote:
> 
> Update: i think having the both was a bad idea, it would be redundant.

I was going to say the same thing. Having both #* and #| is redundant and also a bit confusing, since #| doesn't really feel to be the opposite operation of #* at any context. We should choose one or the other but not both. 
I still like #| (with association from left to right) a bit better, but I rather have #* than nothing.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-25917

Author: pabloh (Pablo Herrero)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:48422] [ruby-trunk - Feature #6284][Feedback] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (8 preceding siblings ...)
  2012-04-16  2:59 ` [ruby-core:44373] " pabloh (Pablo Herrero)
@ 2012-10-27  1:44 ` matz (Yukihiro Matsumoto)
  2012-11-09  9:56 ` [ruby-core:49156] [ruby-trunk - Feature #6284] " jballanc (Joshua Ballanco)
                   ` (43 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2012-10-27  1:44 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by matz (Yukihiro Matsumoto).

Status changed from Assigned to Feedback

Positive about adding function composition. But we need method name consensus before adding it?
Is #* OK for everyone?

Matz.

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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49156] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (9 preceding siblings ...)
  2012-10-27  1:44 ` [ruby-core:48422] [ruby-trunk - Feature #6284][Feedback] " matz (Yukihiro Matsumoto)
@ 2012-11-09  9:56 ` jballanc (Joshua Ballanco)
  2012-11-09 11:10 ` [ruby-core:49159] " rosenfeld (Rodrigo Rosenfeld Rosas)
                   ` (42 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: jballanc (Joshua Ballanco) @ 2012-11-09  9:56 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by jballanc (Joshua Ballanco).


Might I humbly suggest #<- :

    to_camel = :capitalize.to_proc
    add_header = ->val {"Title: " + val}
    
    format_as_title = add_header <- to_camel <- :strip

Seems to have a nice symmetry with #->
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32702

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49159] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (10 preceding siblings ...)
  2012-11-09  9:56 ` [ruby-core:49156] [ruby-trunk - Feature #6284] " jballanc (Joshua Ballanco)
@ 2012-11-09 11:10 ` rosenfeld (Rodrigo Rosenfeld Rosas)
  2012-11-09 11:23 ` [ruby-core:49161] " rohitarondekar (Rohit Arondekar)
                   ` (41 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: rosenfeld (Rodrigo Rosenfeld Rosas) @ 2012-11-09 11:10 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


I think "<-" reads better but I'm ok with '*' as well.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32706

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49161] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (11 preceding siblings ...)
  2012-11-09 11:10 ` [ruby-core:49159] " rosenfeld (Rodrigo Rosenfeld Rosas)
@ 2012-11-09 11:23 ` rohitarondekar (Rohit Arondekar)
  2012-11-09 16:25 ` [ruby-core:49169] " alexeymuranov (Alexey Muranov)
                   ` (40 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: rohitarondekar (Rohit Arondekar) @ 2012-11-09 11:23 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by rohitarondekar (Rohit Arondekar).


I'm with Joshua, I think #<- reads a lot better.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32708

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49169] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (12 preceding siblings ...)
  2012-11-09 11:23 ` [ruby-core:49161] " rohitarondekar (Rohit Arondekar)
@ 2012-11-09 16:25 ` alexeymuranov (Alexey Muranov)
  2012-11-09 17:42 ` [ruby-core:49171] " marcandre (Marc-Andre Lafortune)
                   ` (39 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: alexeymuranov (Alexey Muranov) @ 2012-11-09 16:25 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by alexeymuranov (Alexey Muranov).


=begin
I think that the meaning of  (({#<-}))  would not be symmetric with the meaning of  (({#->})).

Also, in mathematics, arrows are more like relations than operations.  When used to describe functions, usually function arguments go to the arrow's tail, function values to the arrow's head, and function's name, for example, goes on top of the arrow.
(In this sense Ruby's lambda syntax would look to me more natural in the form (({f = (a,b)->{ a + b }})) instead of (({f = ->(a,b){ a + b }})).)

The main drawback of #* in my opinion is that is does not specify the direction of composition ((f*g)(x) is f(g(x)) or g(f(x))?), but since in Ruby function arguments are written on the right ((({f(g(x))}))), i think it can be assumed that the inner function is on the right and the outer is on the left.
=end

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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49171] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (13 preceding siblings ...)
  2012-11-09 16:25 ` [ruby-core:49169] " alexeymuranov (Alexey Muranov)
@ 2012-11-09 17:42 ` marcandre (Marc-Andre Lafortune)
  2012-11-10  3:06 ` [ruby-core:49182] " duerst (Martin Dürst)
                   ` (38 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: marcandre (Marc-Andre Lafortune) @ 2012-11-09 17:42 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by marcandre (Marc-Andre Lafortune).


+1 for #*

The symbol used in mathematics for function composition is a circle (∘); the arrows are for the definitions of functions (like lambdas) only, so #<- or whatever make no sense to me.

Finally, the f ∘ g(x) is defined as f(g(x)), so there is no argument there either.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32723

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49182] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (14 preceding siblings ...)
  2012-11-09 17:42 ` [ruby-core:49171] " marcandre (Marc-Andre Lafortune)
@ 2012-11-10  3:06 ` 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)
                   ` (37 subsequent siblings)
  53 siblings, 1 reply; 57+ messages in thread
From: duerst (Martin Dürst) @ 2012-11-10  3:06 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by duerst (Martin Dürst).


marcandre (Marc-Andre Lafortune) wrote:
> +1 for #*
> 
> The symbol used in mathematics for function composition is a circle (∘); the arrows are for the definitions of functions (like lambdas) only, so #<- or whatever make no sense to me.

Very good point.

> Finally, the f ∘ g(x) is defined as f(g(x)), so there is no argument there either.

Not true. Depending on which field of mathematics you look at, either (f ∘ g)(x) is either f(g(x)), or it is g(f(x)). The later is in particular true in work involving relations, see e.g. http://en.wikipedia.org/wiki/Composition_of_relations#Definition.

Speaking from a more programming-related viewpoint, f(g(x)) is what is used e.g. in Haskell, and probably in many other functional languages, and so may be familiar with many programmers.

However, we should take into account that a functional language writes e.g. reverse(sort(array)), so it makes sense to define revsort = reverse * sort (i.e. (f ∘ g)(x) is f(g(x))). But in Ruby, it would be array.sort.reverse, so revsort = sort * reverseve may feel much more natural (i.e. (f ∘ g)(x) is g(f(x))). 

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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49184] Re: [ruby-trunk - Feature #6284] Add composition for procs
  2012-11-10  3:06 ` [ruby-core:49182] " duerst (Martin Dürst)
@ 2012-11-10  4:00   ` Matthew Kerwin
  0 siblings, 0 replies; 57+ messages in thread
From: Matthew Kerwin @ 2012-11-10  4:00 UTC (permalink / raw)
  To: ruby-core

[-- Attachment #1: Type: text/plain, Size: 2648 bytes --]

I agree that (f ∘ g)(x) is g(f(x)) is more intuitive from a purely
programmatic point of view.  It is "natural" for the operations to be
applied left to right, exactly like method chaining.


On 10 November 2012 13:06, duerst (Martin Dürst) <duerst@it.aoyama.ac.jp>wrote:

>
> Issue #6284 has been updated by duerst (Martin Dürst).
>
>
> marcandre (Marc-Andre Lafortune) wrote:
> > +1 for #*
> >
> > The symbol used in mathematics for function composition is a circle (∘);
> the arrows are for the definitions of functions (like lambdas) only, so #<-
> or whatever make no sense to me.
>
> Very good point.
>
> > Finally, the f ∘ g(x) is defined as f(g(x)), so there is no argument
> there either.
>
> Not true. Depending on which field of mathematics you look at, either (f ∘
> g)(x) is either f(g(x)), or it is g(f(x)). The later is in particular true
> in work involving relations, see e.g.
> http://en.wikipedia.org/wiki/Composition_of_relations#Definition.
>
> Speaking from a more programming-related viewpoint, f(g(x)) is what is
> used e.g. in Haskell, and probably in many other functional languages, and
> so may be familiar with many programmers.
>
> However, we should take into account that a functional language writes
> e.g. reverse(sort(array)), so it makes sense to define revsort = reverse *
> sort (i.e. (f ∘ g)(x) is f(g(x))). But in Ruby, it would be
> array.sort.reverse, so revsort = sort * reverseve may feel much more
> natural (i.e. (f ∘ g)(x) is g(f(x))).
>
> ----------------------------------------
> Feature #6284: Add composition for procs
> https://bugs.ruby-lang.org/issues/6284#change-32728
>
> Author: pabloh (Pablo Herrero)
> Status: Feedback
> Priority: Normal
> Assignee: matz (Yukihiro Matsumoto)
> Category:
> Target version: 2.0.0
>
>
> =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
>
>
> --
> http://bugs.ruby-lang.org/
>
>


-- 
  Matthew Kerwin, B.Sc (CompSci) (Hons)
  http://matthew.kerwin.net.au/
  ABN: 59-013-727-651

  "You'll never find a programming language that frees
  you from the burden of clarifying your ideas." - xkcd

[-- Attachment #2: Type: text/html, Size: 3649 bytes --]

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49190] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (15 preceding siblings ...)
  2012-11-10  3:06 ` [ruby-core:49182] " duerst (Martin Dürst)
@ 2012-11-10  9:23 ` alexeymuranov (Alexey Muranov)
  2012-11-10 12:33 ` [ruby-core:49197] " rosenfeld (Rodrigo Rosenfeld Rosas)
                   ` (36 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: alexeymuranov (Alexey Muranov) @ 2012-11-10  9:23 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by alexeymuranov (Alexey Muranov).


phluid61 (Matthew Kerwin) wrote:
> I agree that (f ∘ g)(x) is g(f(x)) is more intuitive from a purely
>  programmatic point of view.  It is "natural" for the operations to be
>  applied left to right, exactly like method chaining.
>

When functions are applied from left to right, the argument is usually (if not always) on the left.  The form (x)(fg)=((x)f)g may look awkward (though i personally used it in a math paper), so i think usually the "exponential" notation is preferred: x^(fg) = (x^f)^g, where x^f corresponds to f(x) in usual notation.

With method chaining, IMO, the "main argument" of a method is the receiver, and it is on the left.  `Lambda`s and `Proc`s are not chained in the same way as method calls.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32739

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49197] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (16 preceding siblings ...)
  2012-11-10  9:23 ` [ruby-core:49190] " alexeymuranov (Alexey Muranov)
@ 2012-11-10 12:33 ` rosenfeld (Rodrigo Rosenfeld Rosas)
  2012-11-24  1:31 ` [ruby-core:49947] " mame (Yusuke Endoh)
                   ` (35 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: rosenfeld (Rodrigo Rosenfeld Rosas) @ 2012-11-10 12:33 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


In Math multiplication is always associative, even for matrix. I.e: (A*B)*C == A*(B*C). If we use * for ∘ (composition) it resembles multiplication. Function composition is analog to matrix multiplication which are commonly used for transformation compositions as well. In fact, function composition is also associative.

So, when representing h = f ∘ g as h = f * g it makes sense to me (although Math preferring a different symbol for multiplication and composition is a good indication that we should consider this as well for Ruby - more on that later on). But Math representation is procedural, not object oriented. If we try to mix both approaches to fit Ruby philosophy this could lead to great confusion.

Ruby can be also used for procedural programming:

sqrt = ->(n){ Math.sqrt n } # Although I agree that (n)->{} would read more natural to me, just like in CoffeeScript
square_sum = ->(a, b) { a*a + b*b }
hypotenuse = sqrt * square_sum
5 == hypotenuse.call 3, 4 # equivalent to: sqrt.call square_sum.call 3, 4

This makes total sense to me using procedural notation. I'm not sure how would someone use this using some OO notation instead...

Now with regards to composition notation, I think a different notation could help those reading some code and trying to understand it. Suppose this method:

def bad_name(bad_argument_name, b)
  bad_argument_name * b # or bad_argument_name << b
end

You can't know beforehand if bad_argument_name is an array, a number or a proc/lambda. If we read this instead:

def bad_name(bad_argument_name, b)
  bad_argument_name <- b
end

we would then have a clear indication that bad_argument_name is probably a proc/lambda. I know the same argument could be used to differentiate << between strings and arrays among other cases. But I think that function composition is conceptually much different from those other operations (concatenation, multiplication) than concatenation (<<) is for strings and arrays. In both cases we are concatenating but concatenation means different things for strings and arrays in non surprising ways.

But then using this arrow notation I would expect that (a <- b) would mean "a before b" (b(a(...))) while (a ∘ b) means "a after b" (a(b(...))).

I find it a bit awful to use "hypotenuse = square_sum <- sqrt", although it is the way OO usually work ([4, 5].square_num.sqrt - pseudo-code of course). But we would not be using "[4, 5].hypotenuse", but "hypotenuse.call 4, 5", right? So, since we're using procedural notation for procs/lambdas we should be thinking of procedural programming when deciding which operator to use.

I would really prefer to have lambda syntax as "double = <-{|n| n * 2}" and function composition as "hypotenuse = sqrt -> square_sum" (sqrt after square_sum). But since I don't believe the lambda syntax won't ever change, let's try to see this over a different perspective.

Instead of reading (a <- b) as "a before b", I'll try to think of it as being "b applied to a" (a(b(...))). This also make sense to me so I can easily get used to this. It would work the same way as "*" but there would be a clear indication that this refers to function composition rather than some generic multiplication algorithm.

Having said that, I'd like to confirm that I'm ok with either * or <- and I'd really like to have function composition as part of Ruby.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-32748

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 2.0.0


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:49947] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (17 preceding siblings ...)
  2012-11-10 12:33 ` [ruby-core:49197] " rosenfeld (Rodrigo Rosenfeld Rosas)
@ 2012-11-24  1:31 ` mame (Yusuke Endoh)
  2012-12-01 19:54 ` [ruby-core:50461] " rits (First Last)
                   ` (34 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mame (Yusuke Endoh) @ 2012-11-24  1:31 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by mame (Yusuke Endoh).

Target version changed from 2.0.0 to next minor


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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:50461] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (18 preceding siblings ...)
  2012-11-24  1:31 ` [ruby-core:49947] " mame (Yusuke Endoh)
@ 2012-12-01 19:54 ` rits (First Last)
  2012-12-05  3:13 ` [ruby-core:50563] " boris_stitnicky (Boris Stitnicky)
                   ` (33 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: rits (First Last) @ 2012-12-01 19:54 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by rits (First Last).


proc composition is not commutative, so the operator should:

1. not imply commutativity
2. not conceal the order of application

i.e. the operator should be visually asymmetrical with clear directionality

e.g. <<, <<<, <-

a << b << c = a(b(c(x))) right associative

perhaps it also makes sense to have an opposite direction, left associative version: c >> b >> a = a(b(c(x)))


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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:50563] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (19 preceding siblings ...)
  2012-12-01 19:54 ` [ruby-core:50461] " rits (First Last)
@ 2012-12-05  3:13 ` boris_stitnicky (Boris Stitnicky)
  2012-12-05 10:47 ` [ruby-core:50568] " rosenfeld (Rodrigo Rosenfeld Rosas)
                   ` (32 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: boris_stitnicky (Boris Stitnicky) @ 2012-12-05  3:13 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by boris_stitnicky (Boris Stitnicky).


+1 to #*.
+1 to rosenfeld's first 2 paragraphs ( h = f ∘ g as h = f * g, and matrix multiplication analogy).
-1 to "<-". Rationale: It is too easy invent a guitar with one more string. Furthermore, when it comes to operators, I consider design by jury a weak approach.
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-34401

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:50568] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (20 preceding siblings ...)
  2012-12-05  3:13 ` [ruby-core:50563] " boris_stitnicky (Boris Stitnicky)
@ 2012-12-05 10:47 ` rosenfeld (Rodrigo Rosenfeld Rosas)
  2012-12-05 11:23 ` [ruby-core:50569] " alexeymuranov (Alexey Muranov)
                   ` (31 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: rosenfeld (Rodrigo Rosenfeld Rosas) @ 2012-12-05 10:47 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


I play a 7-string guitar and I can tell you that the extra string greatly improves our possibilities and it is pretty common in Samba and Choro Brazilian music styles:

http://www.youtube.com/watch?v=3mTdpRY6yMI
http://www.youtube.com/watch?v=_FNDXcVr1Pk (here we not only have a 7-string guitar but also a 10-string bandolim while the usual one has 8 strings)

I'm not against #*. I just slightly prefer "<-" over "*".
----------------------------------------
Feature #6284: Add composition for procs
https://bugs.ruby-lang.org/issues/6284#change-34407

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:50569] [ruby-trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (21 preceding siblings ...)
  2012-12-05 10:47 ` [ruby-core:50568] " rosenfeld (Rodrigo Rosenfeld Rosas)
@ 2012-12-05 11:23 ` alexeymuranov (Alexey Muranov)
  2015-06-14 16:55 ` [ruby-core:69586] [Ruby trunk " mudge
                   ` (30 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: alexeymuranov (Alexey Muranov) @ 2012-12-05 11:23 UTC (permalink / raw)
  To: ruby-core


Issue #6284 has been updated by alexeymuranov (Alexey Muranov).


rits (First Last) wrote:
> proc composition is not commutative, so the operator should:
> 
> 1. not imply commutativity

In algebra, multiplication is rarely commutative, see for example http://en.wikipedia.org/wiki/Quaternion


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

Author: pabloh (Pablo Herrero)
Status: Feedback
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: next minor


=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


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69586] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (22 preceding siblings ...)
  2012-12-05 11:23 ` [ruby-core:50569] " alexeymuranov (Alexey Muranov)
@ 2015-06-14 16:55 ` mudge
  2015-06-23 14:47 ` [ruby-core:69712] " mudge
                   ` (29 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-06-14 16:55 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.

File 0001-proc.c-Implement-Proc-for-Proc-composition.patch added
File 0002-proc.c-Implement-Method-for-Method-composition.patch added

Attached patches for `Proc#*` and `Method#*` for Proc and Method composition including test cases. Also raised as a pull request on GitHub at https://github.com/ruby/ruby/pull/935

One thing that might be worth discussing is the necessity of the type checking: should it be possible to compose any callable object (rather than explicitly `Proc`s and `Method`s)? This could be deferred to a later enhancement as I imagine it would complicate the implementation.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
0002-proc.c-Implement-Method-for-Method-composition.patch (2.71 KB)
0001-proc.c-Implement-Proc-for-Proc-composition.patch (3.73 KB)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69712] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (23 preceding siblings ...)
  2015-06-14 16:55 ` [ruby-core:69586] [Ruby trunk " mudge
@ 2015-06-23 14:47 ` mudge
  2015-06-29  8:39 ` [ruby-core:69768] " mudge
                   ` (28 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-06-23 14:47 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.

File 0003-proc.c-Support-any-callable-when-composing-Procs.patch added

Attached patch to support composing with any object that responds to `call` (rather than raising a `TypeError` if the object was not a `Proc` or `Method`), e.g.

~~~
class Foo
  def call(x, y)
    x + y
  end
end

f = proc { |x| x * 2 }
g = f * Foo.new

g.call(1, 2) #=> 6
~~~

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69768] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (24 preceding siblings ...)
  2015-06-23 14:47 ` [ruby-core:69712] " mudge
@ 2015-06-29  8:39 ` mudge
  2015-06-29 21:32 ` [ruby-core:69777] " pablodherrero
                   ` (27 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-06-29  8:39 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.


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

> The notation g ∘ f 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) #=> f(g(x))`
* `Proc#after`: `f.after(g) #=> f(g(x))`
* `Proc#following`: `f.following(g) #=> f(g(x))`
* `Proc#of`: `f.of(g) #=> f(g(x))`
* `Proc#on`: `f.on(g) #=> 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
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69777] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (25 preceding siblings ...)
  2015-06-29  8:39 ` [ruby-core:69768] " mudge
@ 2015-06-29 21:32 ` pablodherrero
  2015-06-30  8:21 ` [ruby-core:69812] " duerst
                   ` (26 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: pablodherrero @ 2015-06-29 21:32 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Pablo Herrero.


It would be nice to be able to compose functions in both ways, like in F#, you can do `g << f` or `g >> f`, sadly this was rejected before.

I would settle to have `Proc#*` for "regular" composition and `Proc#|` for "piping".
Last time there was no consensus about the syntax. Hopefully we can manage to solve this before 2.3 is released.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69812] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (26 preceding siblings ...)
  2015-06-29 21:32 ` [ruby-core:69777] " pablodherrero
@ 2015-06-30  8:21 ` duerst
  2015-06-30  8:35 ` [ruby-core:69813] " eregontp
                   ` (25 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: duerst @ 2015-06-30  8:21 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Martin Dürst.


I'm teaching Haskell in a graduate class, so I'm quite familiar with function composition and use it a lot, but the original example isn't convincing at all. For me, in Ruby, something like val.strip.capitalize reads much, much better than some artificially forced function composition. If there were a method String#prepend, it would be even more natural: val.strip.capitalize.prepend('Title: ').

If there are better examples that feel more natural in Ruby, please post them here.


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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69813] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (27 preceding siblings ...)
  2015-06-30  8:21 ` [ruby-core:69812] " duerst
@ 2015-06-30  8:35 ` eregontp
  2015-06-30 11:37 ` [ruby-core:69816] " pablodherrero
                   ` (24 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: eregontp @ 2015-06-30  8:35 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Benoit Daloze.


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

There is String#prepend, but it mutates the receiver.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69816] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (28 preceding siblings ...)
  2015-06-30  8:35 ` [ruby-core:69813] " eregontp
@ 2015-06-30 11:37 ` pablodherrero
  2015-06-30 12:39 ` [ruby-core:69817] " mudge
                   ` (23 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: pablodherrero @ 2015-06-30 11:37 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Pablo Herrero.


Martin Dürst wrote:
> I'm teaching Haskell in a graduate class, so I'm quite familiar with function composition and use it a lot, but the original example isn't convincing at all. For me, in Ruby, something like val.strip.capitalize reads much, much better than some artificially forced function composition. If there were a method String#prepend, it would be even more natural: val.strip.capitalize.prepend('Title: ').
> 
> 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
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69817] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (29 preceding siblings ...)
  2015-06-30 11:37 ` [ruby-core:69816] " pablodherrero
@ 2015-06-30 12:39 ` mudge
  2015-07-01 15:52 ` [ruby-core:69833] " tom
                   ` (22 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-06-30 12:39 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.


Pablo Herrero wrote:
> 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.

Transproc is what actually inspired me to submit a patch here: my hope is that having functional composition in the Ruby language itself will enable easier data pipelining using only `Proc`s, `Method`s and other objects implementing `call`. The presence of `curry` (http://ruby-doc.org/core-2.2.2/Proc.html#method-i-curry) seems like a good precedent for adding such functional primitives to the core.


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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:69833] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (30 preceding siblings ...)
  2015-06-30 12:39 ` [ruby-core:69817] " mudge
@ 2015-07-01 15:52 ` tom
  2015-08-07  7:21 ` [ruby-core:70264] " systho
                   ` (21 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: tom @ 2015-07-01 15:52 UTC (permalink / raw)
  To: ruby-core

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 composition, not method chaining. Its target audience is most likely to read `f * g` as “`f` after `g`”, so that’s 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 proposed implementation achieves that.

The asterisk isn’t ideal, but it’s the best choice available.

----------------------------------------
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
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:70264] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (31 preceding siblings ...)
  2015-07-01 15:52 ` [ruby-core:69833] " tom
@ 2015-08-07  7:21 ` systho
  2015-12-30 11:18 ` [ruby-core:72616] " mudge
                   ` (20 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: systho @ 2015-08-07  7:21 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Philippe Van Eerdenbrugghe.


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

I fully agree with you that a good example really helps to understand the problem which lead to a better solution.

The few times I've missed function composition is ruby is always when I wanted to replace the same pattern : 

~~~
res = step1(base)
res = step2(res)
res = step3(res)
...
res
~~~
I hate having to mutate the res variable and I would hate even more creating different temp variables especially when I do not know how many steps there will be. I would like to be able to combine the steps then apply the step combination to *base*
There are a lot of examples for this pattern so here is one : data retrieval with ActiveRecord ( I assume most people know this library but this is only an example )

~~~
messages  = all_messages
messages = restrict_to_owner(messages, owner)
filters.each do |filter|
  messages = filter.apply(messages)
end
messages = messages.order(created_at: :asc)
messages = paginate(messages)
~~~

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
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)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:72616] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (32 preceding siblings ...)
  2015-08-07  7:21 ` [ruby-core:70264] " systho
@ 2015-12-30 11:18 ` mudge
  2015-12-30 12:28 ` [ruby-core:72618] " mudge
                   ` (19 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-12-30 11:18 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.

File proc-compose.patch added

With the recent addition of `Hash#to_proc` and performance improvements to `Proc`s in 2.3.0, I have rebased my patch (attached) to add composition between `Proc`s, `Method`s and other objects responding to `call` with `*` in the hope of reviving this proposal.

c.f. https://github.com/ruby/ruby/pull/935

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
proc-compose.patch (10.3 KB)


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

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:72618] [Ruby trunk - Feature #6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (33 preceding siblings ...)
  2015-12-30 11:18 ` [ruby-core:72616] " mudge
@ 2015-12-30 12:28 ` mudge
  2016-10-09 14:30 ` [ruby-core:77534] [Ruby trunk Feature#6284] " bigbadmath
                   ` (18 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2015-12-30 12:28 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.

File 0001-proc.c-Implement-Proc-for-Proc-composition.patch added
File 0002-proc.c-Implement-Method-for-Method-composition.patch added
File 0003-proc.c-Support-any-callable-when-composing-Procs.patch added

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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--------------------------------
proc-compose.patch (10.3 KB)
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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:77534] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (34 preceding siblings ...)
  2015-12-30 12:28 ` [ruby-core:72618] " mudge
@ 2016-10-09 14:30 ` bigbadmath
  2016-10-11 20:18 ` [ruby-core:77595] " mudge
                   ` (17 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: bigbadmath @ 2016-10-09 14:30 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Alexander Moore-Niemi.


I wrote a gem with a C extension of `Proc#compose`: https://github.com/mooreniemi/proc_compose#usage

What motivated me was `map f (map g xs) = map (f . g) xs`, and what I still don't understand (being a newbie to extending Ruby or understanding its internals) is that `.map(&some_proc).map(&some_other_proc)` still behaves better than `.map(&(some_proc * some_other_proc))` given my current implementation of `compose`. Although I think composition has a lot of uses, the admittedly small but free performance benefit I expected to gain was top of my list.

I do think emphasis on composition suggests a somewhat different style of writing Ruby, but I think it can be a good one, actually.

Paul: what's the performance of your `compose`? If I have time later I can use https://github.com/mooreniemi/graph-function to try and see.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:77595] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (35 preceding siblings ...)
  2016-10-09 14:30 ` [ruby-core:77534] [Ruby trunk Feature#6284] " bigbadmath
@ 2016-10-11 20:18 ` mudge
  2016-10-12  8:28 ` [ruby-core:77597] " mudge
                   ` (16 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2016-10-11 20:18 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.


Alexander Moore-Niemi wrote:
> Paul: what's the performance of your `compose`? If I have time later I can use https://github.com/mooreniemi/graph-function to try and see.

I ran the following benchmark with benchmark-ips:

~~~
require 'benchmark/ips'

double = proc { |x| x * 2 }
quadruple = proc { |x| x * 4 }
octuple = double * quadruple
inline_octuple = proc { |x| x * 2 * 4 }
nested_octuple = proc { |x| quadruple.call(double.call(x)) }

numbers = [1, 2, 3, 4, 5]

Benchmark.ips do |x|
  x.report('composing procs') do
    numbers.map(&octuple)
  end

  x.report('chaining procs') do
    numbers.map(&double).map(&quadruple)
  end

  x.report('single proc') do
    numbers.map(&inline_octuple)
  end

  x.report('nested proc') do
    numbers.map(&nested_octuple)
  end

  x.compare!
end
~~~

And also see a performance drop with composition over chaining multiple `map`s:

~~~
Warming up --------------------------------------
     composing procs    27.822k i/100ms
      chaining procs    32.096k i/100ms
         single proc    49.021k i/100ms
         nested proc    27.337k i/100ms
Calculating -------------------------------------
     composing procs    341.874k (± 0.5%) i/s -      1.725M in   5.045764s
      chaining procs    389.031k (± 0.7%) i/s -      1.958M in   5.032912s
         single proc    666.544k (± 0.6%) i/s -      3.333M in   5.001266s
         nested proc    321.919k (± 0.8%) i/s -      1.613M in   5.010562s

Comparison:
         single proc:   666543.8 i/s
      chaining procs:   389031.4 i/s - 1.71x  slower
     composing procs:   341873.8 i/s - 1.95x  slower
         nested proc:   321919.1 i/s - 2.07x  slower
~~~

It might be interesting to look at object allocations as we effectively create a nested `Proc` which might account for the slow down.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:77597] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (36 preceding siblings ...)
  2016-10-11 20:18 ` [ruby-core:77595] " mudge
@ 2016-10-12  8:28 ` mudge
  2016-10-12  9:46 ` [ruby-core:77599] " duerst
                   ` (15 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: mudge @ 2016-10-12  8:28 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Paul Mucur.


Yukihiro Matsumoto wrote:
> Positive about adding function composition. But we need method name consensus before adding it?
> Is `#*` OK for everyone?

Aside from implementation details, is the lack of consensus on the method name (and the resulting behaviour re left vs right operand) the main blocker here?

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:77599] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (37 preceding siblings ...)
  2016-10-12  8:28 ` [ruby-core:77597] " mudge
@ 2016-10-12  9:46 ` duerst
  2017-01-22 18:50 ` [ruby-core:79219] " t.hirsch
                   ` (14 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: duerst @ 2016-10-12  9:46 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Martin Dürst.


Paul Mucur wrote:
> Yukihiro Matsumoto wrote:
> > Positive about adding function composition. But we need method name consensus before adding it?
> > Is `#*` OK for everyone?
> 
> Aside from implementation details, is the lack of consensus on the method name (and the resulting behaviour re left vs right operand) the main blocker here?

If Matz says so, then yes, this is the main blocker.

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
=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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:79219] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (38 preceding siblings ...)
  2016-10-12  9:46 ` [ruby-core:77599] " duerst
@ 2017-01-22 18:50 ` t.hirsch
  2017-03-13  8:13 ` [ruby-core:80110] " matz
                   ` (13 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: t.hirsch @ 2017-01-22 18:50 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Thorsten Hirsch.


fyi: Composing procs is way faster now. It beats all other algorithms in Paul's benchmark on ruby 2.3.3:

~~~

Warming up --------------------------------------
     composing procs   135.483k i/100ms
      chaining procs    55.595k i/100ms
         single proc    84.448k i/100ms
         nested proc    48.095k i/100ms
Calculating -------------------------------------
     composing procs      2.363M (± 6.2%) i/s -     11.787M in   5.011859s
      chaining procs    701.936k (± 3.4%) i/s -      3.558M in   5.074972s
         single proc      1.207M (± 3.6%) i/s -      6.080M in   5.044891s
         nested proc    579.005k (± 2.9%) i/s -      2.934M in   5.071310s

Comparison:
     composing procs:  2362658.0 i/s
         single proc:  1206768.2 i/s - 1.96x  slower
      chaining procs:   701936.2 i/s - 3.37x  slower
         nested proc:   579005.4 i/s - 4.08x  slower
~~~

Tested on linux and macOS (same result).

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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: 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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:80110] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (39 preceding siblings ...)
  2017-01-22 18:50 ` [ruby-core:79219] " t.hirsch
@ 2017-03-13  8:13 ` matz
  2017-04-28 19:47 ` [ruby-core:80931] " ritchie
                   ` (12 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: matz @ 2017-03-13  8:13 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by Yukihiro Matsumoto.


I want to make sure if everyone agrees with "*" instead of OP's "<<".
Besides that I also wanted to mention that Koichi concerns that function composition may be far slower than method chaining.

Matz.


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

* Author: Pablo Herrero
* Status: Feedback
* Priority: Normal
* Assignee: 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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:80931] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (40 preceding siblings ...)
  2017-03-13  8:13 ` [ruby-core:80110] " matz
@ 2017-04-28 19:47 ` ritchie
  2017-08-31  6:26 ` [ruby-core:82555] " ozaki
                   ` (11 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: ritchie @ 2017-04-28 19:47 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by RichOrElse (Richie Paul Buitre).


matz (Yukihiro Matsumoto) wrote:
> I want to make sure if everyone agrees with "*" instead of OP's "<<".
> Besides that I also wanted to mention that Koichi concerns that function composition may be far slower than method chaining.
> 
> Matz.

+1 for #*

Initially I thought of the F# convention #<< and it's counter part #>> as intuitive. But after giving it some thought, and practice, I prefer #* and #+.

~~~ ruby
class Proc
  def +(other) # forward compose operation
    other.to_proc * self
  end

  def *(other) # backward compose operation
    -> arg { self.call other.(arg) } # with only one argument for performance reason.
  end
end

f = -> n { n  * 2 }
g = -> n { n  * 4 }
h = f * g                            # equivalent to (g + f)

puts h.(5)                           #=> 40
puts  (h  + :odd?).call(3)           #=> false
~~~

Instead of composition I see potential use for shovel operations #<< and #>> for piping purposes similar to Elixir's #|>.

~~~ ruby
class Object
  def >>(transform) # piping operation
    transform.(self)
  end
end

class Integer
  def >>(num_or_func)
    return num_or_func.(self) if num_or_func.respond_to? :call
    super
  end
end

class Proc
  def <<(input) # feed value
    call input
  end
end

add_header = ->val {"Title: " + val}
format_as_title = add_header + :capitalize + :strip

puts 'Title goes here.' >> format_as_title  #=> Title: title goes here.
puts format_as_title << 'Title goes there.' #=> Title: title goes there.
~~~





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

* 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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:82555] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (41 preceding siblings ...)
  2017-04-28 19:47 ` [ruby-core:80931] " ritchie
@ 2017-08-31  6:26 ` ozaki
  2018-01-29  7:49 ` [ruby-core:85213] " zverok.offline
                   ` (10 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: ozaki @ 2017-08-31  6:26 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:85213] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (42 preceding siblings ...)
  2017-08-31  6:26 ` [ruby-core:82555] " ozaki
@ 2018-01-29  7:49 ` zverok.offline
  2018-04-20 18:34 ` [ruby-core:86632] " keystonelemur
                   ` (9 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: zverok.offline @ 2018-01-29  7:49 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by zverok (Victor Shepelev).


Please take this to next Developers Meeting Agenda? 

The best possible name is discussed for 6 years already, and feature seems pretty nice to have. I believe that sometimes it is just good to make "executive decision" about name once and for all, at least better than postpone feature for years. 

(I still unhappy with `yield_self` name, though :))

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

* 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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:86632] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (43 preceding siblings ...)
  2018-01-29  7:49 ` [ruby-core:85213] " zverok.offline
@ 2018-04-20 18:34 ` keystonelemur
  2018-04-30 18:51 ` [ruby-core:86780] " keystonelemur
                   ` (8 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: keystonelemur @ 2018-04-20 18:34 UTC (permalink / raw)
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:86780] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (44 preceding siblings ...)
  2018-04-20 18:34 ` [ruby-core:86632] " keystonelemur
@ 2018-04-30 18:51 ` keystonelemur
  2018-05-17  5:56 ` [ruby-core:87100] [Ruby trunk Feature#6284][Open] " matz
                   ` (7 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: keystonelemur @ 2018-04-30 18:51 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by baweaver (Brandon Weaver).


I just realized that infix operators evaluate before `to_proc`, allowing something like this:

```ruby
[1,2,3].map(&:succ.to_proc >> :to_str.to_proc)
```

Codified it into a gem for kicks: https://github.com/baweaver/mf

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

* 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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:87100] [Ruby trunk Feature#6284][Open] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (45 preceding siblings ...)
  2018-04-30 18:51 ` [ruby-core:86780] " keystonelemur
@ 2018-05-17  5:56 ` matz
  2018-07-30  8:13 ` [ruby-core:88188] [Ruby trunk Feature#6284] " melentievm
                   ` (6 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: matz @ 2018-05-17  5:56 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by matz (Yukihiro Matsumoto).

Status changed from Feedback to Open

Considering the combination of OOP and FP, it seems a good idea to adding both forward and reverse combination of procs. So we pick Groovy way (adding `<<` and `>>` methods to `Proc`).

We need more discussion if we would add combination methods to the `Symbol` class.

Matz.


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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:88188] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (46 preceding siblings ...)
  2018-05-17  5:56 ` [ruby-core:87100] [Ruby trunk Feature#6284][Open] " matz
@ 2018-07-30  8:13 ` melentievm
  2018-08-08  5:41 ` [ruby-core:88339] " ko1
                   ` (5 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: melentievm @ 2018-07-30  8:13 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by printercu (Max Melentiev).


matz (Yukihiro Matsumoto) wrote:
> Considering the combination of OOP and FP, it seems a good idea to adding both forward and reverse combination of procs. So we pick Groovy way (adding `<<` and `>>` methods to `Proc`).

What do you think about selecting only single operation? Here is Groovy example from provided link:

```
final times2 = { it * 2 }
final plus1 = { it + 1 }
 
final plus1_then_times2 = times2 << plus1
final times2_then_plus1 = times2 >> plus1
```

It looks like `<<` is less intuitive and readable. I'm also really afraid of having chance to read something like `a >> b << c >> d`.


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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:88339] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (47 preceding siblings ...)
  2018-07-30  8:13 ` [ruby-core:88188] [Ruby trunk Feature#6284] " melentievm
@ 2018-08-08  5:41 ` ko1
  2018-08-08 18:37 ` [ruby-core:88356] " shannonskipper
                   ` (4 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: ko1 @ 2018-08-08  5:41 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by ko1 (Koichi Sasada).

Assignee changed from matz (Yukihiro Matsumoto) to nobu (Nobuyoshi Nakada)

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:88356] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (48 preceding siblings ...)
  2018-08-08  5:41 ` [ruby-core:88339] " ko1
@ 2018-08-08 18:37 ` shannonskipper
  2018-08-10 19:58 ` [ruby-core:88442] " shannonskipper
                   ` (3 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: shannonskipper @ 2018-08-08 18:37 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by shan (Shannon Skipper).


matz (Yukihiro Matsumoto) wrote:
> We need more discussion if we would add combination methods to the `Symbol` class.
> 
> Matz.

A little sugar on Symbol does look really nice: https://gist.github.com/havenwood/d305b42f5b542e9de1eaa8e56ba6bdd7#file-compose_procs-rb-L32-L45

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:88442] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (49 preceding siblings ...)
  2018-08-08 18:37 ` [ruby-core:88356] " shannonskipper
@ 2018-08-10 19:58 ` shannonskipper
  2018-11-12 12:43 ` [ruby-core:89775] " nobu
                   ` (2 subsequent siblings)
  53 siblings, 0 replies; 57+ messages in thread
From: shannonskipper @ 2018-08-10 19:58 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by shan (Shannon Skipper).


I used this proposal along with #14781 for a fun solution to a problem proposed in the #ruby irc channel: https://gist.github.com/havenwood/b6d55b412fbf583758c91b8ee339a822

I like these features a lot and love that you can just implement them in the meanwhile. <3 Ruby!

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:89775] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (50 preceding siblings ...)
  2018-08-10 19:58 ` [ruby-core:88442] " shannonskipper
@ 2018-11-12 12:43 ` nobu
  2018-11-14 17:33 ` [ruby-core:89796] " pablodherrero
  2018-11-15  2:22 ` [ruby-core:89801] " nobu
  53 siblings, 0 replies; 57+ messages in thread
From: nobu @ 2018-11-12 12:43 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by nobu (Nobuyoshi Nakada).


I've forgotten to post the patch to use `<<` and `>>`.
https://github.com/nobu/ruby/tree/feature/6284-proc-composition

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:89796] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (51 preceding siblings ...)
  2018-11-12 12:43 ` [ruby-core:89775] " nobu
@ 2018-11-14 17:33 ` pablodherrero
  2018-11-15  2:22 ` [ruby-core:89801] " nobu
  53 siblings, 0 replies; 57+ messages in thread
From: pablodherrero @ 2018-11-14 17:33 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by pabloh (Pablo Herrero).


nobu (Nobuyoshi Nakada) wrote:
> I've forgotten to post the patch to use `<<` and `>>`.
> https://github.com/nobu/ruby/tree/feature/6284-proc-composition

Is adding composition methods to the Symbol class still being considered?

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

* [ruby-core:89801] [Ruby trunk Feature#6284] Add composition for procs
  2012-04-12  6:21 [ruby-core:44303] [ruby-trunk - Feature #6284][Open] Add composition for procs pabloh (Pablo Herrero)
                   ` (52 preceding siblings ...)
  2018-11-14 17:33 ` [ruby-core:89796] " pablodherrero
@ 2018-11-15  2:22 ` nobu
  53 siblings, 0 replies; 57+ messages in thread
From: nobu @ 2018-11-15  2:22 UTC (permalink / raw)
  To: ruby-core

Issue #6284 has been updated by nobu (Nobuyoshi Nakada).


pabloh (Pablo Herrero) wrote:
> Is adding composition methods to the `Symbol` class still being considered?

No, I don't think that we consider a `Symbol` object a method.

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

* Author: pabloh (Pablo Herrero)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
* Target version: 
----------------------------------------
It would be nice to be able to compose procs like functions in functional programming languages:

```ruby
to_camel = :capitalize.to_proc
add_header = ->val {"Title: " + val}

format_as_title = add_header << to_camel << :strip
```

instead of:

```ruby
format_as_title = lambda {|val| "Title: " + val.strip.capitalize }
```

It's pretty easy to implement in pure ruby:

```ruby
class Proc
  def << block
    proc { |*args| self.call( block.to_proc.call(*args) ) }
  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/

^ permalink raw reply	[flat|nested] 57+ messages in thread

end of thread, other threads:[~2018-11-15  2:23 UTC | newest]

Thread overview: 57+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [ruby-core:82555] " ozaki
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

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).