Project

General

Profile

Feature #13600

yield_self should be chainable/composable

Added by avit (Andrew Vit) about 1 year ago. Updated 12 months ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:81395]

Description

One of the main use cases that would make yield_self a helpful addition is to build functional pipelines (function composition). This is currently not possible since Proc/Lambda do not handle yield_self in the expected way.

I believe that callable.yield_self(&other) should build up a composition, something like this:

class Proc
  def yield_self(&other)
    proc { |input| other.call( self.call(input) ) }
  end
end

I originally proposed an example for this using pipe | syntax here:

https://bugs.ruby-lang.org/issues/10095#note-22

Maybe Proc should use | or & for this syntax instead of changing yield_self directly.


Related issues

Related to Ruby trunk - Feature #6284: Add composition for procsOpen

History

#1 [ruby-core:81396] Updated by avit (Andrew Vit) about 1 year ago

In case the description is unclear, the goal is to support something like this:

pipeline = (&:upcase).yield_self(&:reverse)
"ruby".yield_self(pipeline)
#=> "YRUB"

or else:

pipeline = (&:upcase) | (&:reverse)
"ruby".yield_self(pipeline)
#=> "YRUB"

#2 Updated by shyouhei (Shyouhei Urabe) about 1 year ago

#3 [ruby-core:81406] Updated by shyouhei (Shyouhei Urabe) about 1 year ago

I'm not sure if this should be an extension to yield_self or a separate new method.

#4 [ruby-core:82562] Updated by matz (Yukihiro Matsumoto) 12 months ago

  • Status changed from Open to Rejected

Function composition should not be implemented by yield_self.

Matz.

Also available in: Atom PDF