~> operator

Hello.

I sometimes write code in irb/pry and I want to send the result into
another function.

Something like

    Foo.bar(baz)

Then, it turns out I wanted to write

    f(Foo.bar(baz))

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

I was wondering if we can discuss some new operator, like following:

    a ~> b
    defined as b(a)

Some other languages already have such an operator.

Ideas? Discussion?

Makes my life easier:

    Foo.bar(baz) ~> f

It could be called the "goes into" operator. It's similar in concept to
unix pipe I guess.

Thanks.

Why dont't you propose it as a new feature to ruby core?

···

On Friday, 1 July 2016, Felipe Tavares <felipetavres@gmail.com> wrote:

Seems pretty nice and promising.
El 01/07/2016 14:37, "Samuel Williams" <space.ship.traveller@gmail.com > <javascript:_e(%7B%7D,'cvml','space.ship.traveller@gmail.com');>> > escribió:

Hello.

I sometimes write code in irb/pry and I want to send the result into
another function.

Something like

    Foo.bar(baz)

Then, it turns out I wanted to write

    f(Foo.bar(baz))

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

I was wondering if we can discuss some new operator, like following:

    a ~> b
    defined as b(a)

Some other languages already have such an operator.

Ideas? Discussion?

Makes my life easier:

    Foo.bar(baz) ~> f

It could be called the "goes into" operator. It's similar in concept to
unix pipe I guess.

Thanks.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
<javascript:_e(%7B%7D,'cvml','ruby-talk-request@ruby-lang.org');>
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Seems pretty nice and promising.

···

El 01/07/2016 14:37, "Samuel Williams" <space.ship.traveller@gmail.com> escribió:

Hello.

I sometimes write code in irb/pry and I want to send the result into
another function.

Something like

    Foo.bar(baz)

Then, it turns out I wanted to write

    f(Foo.bar(baz))

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

I was wondering if we can discuss some new operator, like following:

    a ~> b
    defined as b(a)

Some other languages already have such an operator.

Ideas? Discussion?

Makes my life easier:

    Foo.bar(baz) ~> f

It could be called the "goes into" operator. It's similar in concept to
unix pipe I guess.

Thanks.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

The way I see this could lead to much more readable code when you have
deeply nested function calls.

Foo.bar(baz) ~> faa ~> goo ~> zuuu

instead of
zoo(goo(faa(Foo.bar(baz))))

From a pratical point of view I think it is a valid feature. This would
make function composition a more viable code paradigm in ruby. Not sure
though this is the desired path for the language, conceptually.

Samuel

Very true, makes me think of Elixir put like that!

Ooop, didn't see Recursive's elixir post. :S

1. As far as I could remember, Matz showed an intention of something
similar in one of presentations, operator was |>. It was targeted for Ruby
3.
Can't find any documentation to support this memory...

2. Similar concept (which everyone feels useful, but nobody could name) is
"apply block and return its return result":
Foo.bar(baz).yield_self { |val| f(val) }
# or
Foo.bar(baz).yield_self(&method(:f))

Feature #6721: Object#yield_self - Ruby master - Ruby Issue Tracking System (and numerous "related issues" with
similar concepts).

···

2016-07-01 15:36 GMT+03:00 Samuel Williams <space.ship.traveller@gmail.com>:

Hello.

I sometimes write code in irb/pry and I want to send the result into
another function.

Something like

    Foo.bar(baz)

Then, it turns out I wanted to write

    f(Foo.bar(baz))

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

I was wondering if we can discuss some new operator, like following:

    a ~> b
    defined as b(a)

Some other languages already have such an operator.

Ideas? Discussion?

Makes my life easier:

    Foo.bar(baz) ~> f

It could be called the "goes into" operator. It's similar in concept to
unix pipe I guess.

Thanks.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

An operator like this is also available in Elixir language:

   "foo" |> bar(1) |> baz(2, 3)

In that implementation the result of each expression is passed as the first argument to the next function call, i.e.:
- bar is called as bar("foo", 1)
- baz is called as baz("foo", 2, 3)

How did you intend to handle multiple arguments in your proposal?

Also I wonder if there could be a way to fill multiple arguments, like

   *foo ~> bar

meaning

   bar(*foo)

Not sure if this is possible from parser perspective :slight_smile:

It looks like the idea is out there for quite some time.

Just found this:
https://medium.com/@franzejr/ruby-3-mri-and-gil-a302577c6634#.yp5xs79es,
where the author talks about that MAYBE Ruby 3.0 will have a pipe operator.

Also Matz talked about it a bit on the EuRuKo 2015, and he seems to
currently do some experiments in form of streem:

···

2016-07-01 17:10 GMT+02:00 Rhys Stansfield <rhys.m.stansfield@gmail.com>:

Ooop, didn't see Recursive's elixir post. :S

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

In my opinion, this is not an issue that should be resolved on the
programming language level. Rather use the correct (readline)
keybindings that are available in IRB just fine: Use CTRL+A for
immediately moving your cursor to the beginning of the line and CTRL+E
for moving to the end of the line. Currently, IRB appearently does not
support wordwise jumping with CTRL+arrow keys as most shells do, but
that can surely be implemented in IRB. No need to change the programming
language for that.

There might be other reasons (see below) for a new piping operator, but
that particular reason just strikes me as a very odd one that seems
resolved better by improving IRB.

It could be called the "goes into" operator. It's similar in concept
to unix pipe I guess.

Eric recently started a thread (message
<20160602015532.GA31921@dcvr.yhbt.net>) that was asking for a "pipe
into" feature with relation to process output, resembling Unix pipes.
If we had such a pipe operator, that could be more easily implemented.

If we get such an operator, it would be cool if it was a method as most
other operators are so that one can do creative things with it on custom
classes. Maybe one can pass it a Method instance as the argument.

If you create the issue in rubymine all the discussion will happen
there as it should be otherwise you will get two discussions.

Not everyone follows the tracker -- it contains so *many* discussions,
not all of which interest everyone --, and ultimately someone would drop
a link to the issue on the tracker here anyway, and then we also have
two discussions. I appreciate it if such a deep change is discussed on
Ruby-Talk prior (or in parallel) to while it is discussed on the
tracker. Otherwise there are chances that the change goes unnoticed
until release, which is too late to provide feedback.

Valete,
Marvin

···

Am Sat, 2 Jul 2016 00:36:51 +1200 schrieb Samuel Williams <space.ship.traveller@gmail.com>:

--
Blog: http://www.guelkerdev.de
PGP/GPG ID: F1D8799FBCC8BC4F

Hi,

Why "~>"? It's not particularly more useful nor beautiful than, for
example, "|>" in Elixir.

              matz.

···

In message "Re: ~> operator" on Sat, 2 Jul 2016 00:36:51 +1200, Samuel Williams <space.ship.traveller@gmail.com> writes:

Hello.

I sometimes write code in irb/pry and I want to send the result into
another function.

Something like

   Foo.bar(baz)

Then, it turns out I wanted to write

   f(Foo.bar(baz))

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

I was wondering if we can discuss some new operator, like following:

   a ~> b
   defined as b(a)

Some other languages already have such an operator.

Ideas? Discussion?

Makes my life easier:

   Foo.bar(baz) ~> f

It could be called the "goes into" operator. It's similar in concept to
unix pipe I guess.

Thanks.

Why dont't you propose it as a new feature to ruby core?

I will be happy to write a full proposal but I’d like to discuss it, find out any improvements or ideas from others, etc :slight_smile:

It was just an example :slight_smile: Peace.

···

On 2/07/2016, at 3:54 AM, Marvin Gülker <m-guelker@phoenixmail.de> wrote:

Am Sat, 2 Jul 2016 00:36:51 +1200
schrieb Samuel Williams <space.ship.traveller@gmail.com <mailto:space.ship.traveller@gmail.com>>:

So I have to move the cursor all the way to the left and insert f and
perhaps some more brackets, etc.

In my opinion, this is not an issue that should be resolved on the
programming language level. Rather use the correct (readline)
keybindings that are available in IRB just fine: Use CTRL+A for
immediately moving your cursor to the beginning of the line and CTRL+E
for moving to the end of the line. Currently, IRB appearently does not
support wordwise jumping with CTRL+arrow keys as most shells do, but
that can surely be implemented in IRB. No need to change the programming
language for that.

There might be other reasons (see below) for a new piping operator, but
that particular reason just strikes me as a very odd one that seems
resolved better by improving IRB.

If you create the issue in rubymine all the discussion will happen there as
it should be otherwise you will get two discussions.
For me the idea seems pretty clear.
You don't need anything other than what you presented.
Look at the issue tracker in rubymine and see how simple it is.

···

On Friday, 1 July 2016, Samuel Williams <space.ship.traveller@gmail.com> wrote:

> Why dont't you propose it as a new feature to ruby core?

I will be happy to write a full proposal but I’d like to discuss it, find
out any improvements or ideas from others, etc :slight_smile:

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org <javascript:;>
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Interesting idea, although I can envisage it being non trivial to implement
though due to Ruby's normal parse order and not requiring () to be appended
to method references to actually call them. Getting it to work as expected
may be a lot more work than people might be interested in for something
that occasionally saves people two key strokes in IRB. I don't know for
sure though, could be worth discussing it with the core team.

Wow, Matz here!

Can you please confirm or deny that (some kind of) this operator makes its
way into Ruby 3?..

Thanks!

V.

···

2016-07-01 18:39 GMT+03:00 Yukihiro Matsumoto <matz@ruby.or.jp>:

Hi,

Why "~>"? It's not particularly more useful nor beautiful than, for
example, "|>" in Elixir.

                                                        matz.

In message "Re: ~> operator" > on Sat, 2 Jul 2016 00:36:51 +1200, Samuel Williams < > space.ship.traveller@gmail.com> writes:

>Hello.
>
>I sometimes write code in irb/pry and I want to send the result into
>another function.
>
>Something like
>
> Foo.bar(baz)
>
>Then, it turns out I wanted to write
>
> f(Foo.bar(baz))
>
>So I have to move the cursor all the way to the left and insert f and
>perhaps some more brackets, etc.
>
>I was wondering if we can discuss some new operator, like following:
>
> a ~> b
> defined as b(a)
>
>Some other languages already have such an operator.
>
>Ideas? Discussion?
>
>Makes my life easier:
>
> Foo.bar(baz) ~> f
>
>It could be called the "goes into" operator. It's similar in concept to
>unix pipe I guess.
>
>Thanks.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

For me, |> looks so harsh.

It looks like a wall that none shall pass. It doesn’t give me the feeling that things are going from left to right.

For me ~> feels like a path from left to right, sort of like, a meandering garden path that I would like to walk along.

I’m not sure if I can explain it better, but my naive preference would be ~> or something similar. Perhaps ->?

Kind regards,
Samuel

···

On 2/07/2016, at 3:39 AM, Yukihiro Matsumoto <matz@ruby.or.jp> wrote:

Hi,

Why "~>"? It's not particularly more useful nor beautiful than, for
example, "|>" in Elixir.

              matz.

-> is already used for stabby lambda syntax. |> makes some sense because
it's used in other languages (e.g. Elixir) and is similarish to a shell
pipe.

-> is already used for stabby lambda syntax.

That’s precisely why it might be a good idea, it avoids introducing new syntax. Stabby lambda syntax probably requires ->( or ->{ to parse.

>> makes some sense because it's used in other languages (e.g. Elixir) and is similarish to a shell pipe.

That’s not why it makes [some] sense, that’s just convention.

Of course, I’m open to ideas and welcome your input :slight_smile: But, in this case, I believe in Ruby, we should seek the best idea not just because others do it.

Kind regards,
Samuel

···

On 2/07/2016, at 3:50 AM, Rhys Stansfield <rhys.m.stansfield@gmail.com> wrote:

One other idea I had was for assigning to variables.

expression ~> @variable

It’s the same as @variable = expression

Sometimes I’m thinking - compute this thing and then put it into this variable.

e.g.

if blah
    compute()
end ~> @result

avoids ugly indentation of

     @result = if
        compute
     end

especially applicable to case statements and I feel easier to read.