Issue #15799 has been updated by cichol (cichol tsai).


Hi,

I want to introduce a way of organizing pipelined calling in Ruby.

I imagined that the Ruby-styled pipelined calling should be like:
```ruby
1.pipe do
  call 1 + _ # => after this line, _ becomes 2
  call _ * 2 # => after this line, _ becomes 6
end # => _ is returned as 6
```
With a seemed redundant `call` method, this `pipe` method can be implemented in current version of Ruby.

I think it would be so good if we can remove the need of `call` and allow a line result capturer defined as a hook.

The example now becomes:
```ruby
1.pipe do
  1 + _
  _ * 2
end
```

Within the block following `pipe`, results of every line are captured and passed to a hook for assignments of `_`.

IMHO, this implementation of pipeline is preferable over the pipeline operator suggested above, for these reasons:

1. This method utilize the placeholder `_` to pass the argument into any position of parameters instead of the last one.
If you are using pipeline operator like those in functional languages, you will need to carefully deal with the order of parameters, which adds mental overhead.
And most existing Ruby methods are not implemented with currying in mind. With use of placeholder we can simply re-use previous methods without additional costs.

2. Explicitly calling by a method `pipe` and a block.
This allow less aggressive modification to the language over adding operators. It is more Ruby-way and clearer.

3. The abstraction of line result capturer can be useful for other use cases.
For example, if we want to inspect every steps of a pipe, we can imagine a special pipe that prints out every step:
```ruby
1.inspected_pipe do
  1 + _ # => puts 2
  _ * 2 # => puts 6
  SomeService.new.process _ # => puts whatever the return value is
end
```
It helps debugging. We can apply tiny modification to make a block loggable.

4. BTW, to avoid patheses for range, we can simply:
```ruby
x = pipe do
  1..
  take 10
  map{|e| e*2}
end
```
to achieve the same functionality asked in the first post.

----------------------------------------
Feature #15799: pipeline operator
https://bugs.ruby-lang.org/issues/15799#change-78570

* Author: nobu (Nobuyoshi Nakada)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Implemented the pipeline operator `|>`, a topic of "ruby committers vs the world" in RubyKaigi 2019.
Also a casual idea of rightward assignment.

```ruby
1.. |> take 10 |> map {|e| e*2} |> (x)
p x #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
```

https://github.com/nobu/ruby/tree/feature/pipeline




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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>