ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: cichol@live.cn
To: ruby-core@ruby-lang.org
Subject: [ruby-core:93141] [Ruby trunk Feature#15799] pipeline operator
Date: Fri, 14 Jun 2019 13:15:22 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-78570.20190614131521.186d5fec62aafb91@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15799.20190427083121@ruby-lang.org

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/

  parent reply	other threads:[~2019-06-14 13:15 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15799.20190427083121@ruby-lang.org>
2019-04-27  8:31 ` [ruby-core:92432] [Ruby trunk Feature#15799] pipeline operator nobu
2019-04-27 10:28 ` [ruby-core:92433] " duerst
2019-04-27 13:23 ` [ruby-core:92436] " eregontp
2019-04-27 14:19 ` [ruby-core:92438] " shevegen
2019-04-28  1:26 ` [ruby-core:92453] " nobu
2019-05-01  1:53 ` [ruby-core:92506] " duerst
2019-05-19 22:05 ` [ruby-core:92725] " jonathan
2019-05-25  1:28 ` [ruby-core:92838] " merch-redmine
2019-05-28  4:54 ` [ruby-core:92869] " konsolebox
2019-05-28  7:37 ` [ruby-core:92872] " konsolebox
2019-05-28  9:33 ` [ruby-core:92873] " matthew
2019-05-28 11:41 ` [ruby-core:92876] " zverok.offline
2019-05-28 13:45 ` [ruby-core:92877] " konsolebox
2019-06-12  6:36 ` [ruby-core:93060] " nobu
2019-06-12 13:35 ` [ruby-core:93073] " zverok.offline
2019-06-13 10:00 ` [ruby-core:93103] " eregontp
2019-06-13 11:43 ` [ruby-core:93105] " shevegen
2019-06-13 13:23 ` [ruby-core:93110] " dgutov
2019-06-13 14:09 ` [ruby-core:93111] " ttilberg
2019-06-14  0:12 ` [ruby-core:93118] " chris
2019-06-14  2:01 ` [ruby-core:93120] " joshua.goodall
2019-06-14  2:51 ` [ruby-core:93122] " keystonelemur
2019-06-14  3:00 ` [ruby-core:93125] " matz
2019-06-14  3:18 ` [ruby-core:93127] " merch-redmine
2019-06-14  5:42 ` [ruby-core:93129] " chris
2019-06-14  6:15 ` [ruby-core:93130] " keystonelemur
2019-06-14  7:45 ` [ruby-core:93133] " matz
2019-06-14  8:28 ` [ruby-core:93134] " keystonelemur
2019-06-14 10:30 ` [ruby-core:93135] " mail
2019-06-14 11:03 ` [ruby-core:93137] " rogeriocfj
2019-06-14 13:15 ` cichol [this message]
2019-06-14 16:56 ` [ruby-core:93144] " sean.m.huber
2019-06-14 19:30 ` [ruby-core:93145] " konsolebox
2019-06-14 23:12 ` [ruby-core:93149] " dgutov
2019-06-16  0:17 ` [ruby-core:93167] " mame
2019-06-16  0:49 ` [ruby-core:93168] " samuel
2019-06-16  8:11 ` [ruby-core:93175] " josh.cheek
2019-06-16 15:29 ` [ruby-core:93188] " shannonskipper
2019-06-30  9:52 ` [ruby-core:93424] " eregontp
2019-06-30 10:19 ` [ruby-core:93425] " eregontp
2019-06-30 10:38 ` [ruby-core:93426] " eregontp
2019-06-30 11:53 ` [ruby-core:93428] " eregontp
2019-08-29  5:39 ` [ruby-core:94645] [Ruby master " matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-78570.20190614131521.186d5fec62aafb91@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).