yield_self is more awesome than you could think

Two things in the blog post I've wanted to share with the community:

* Why yield_self is really nice addition to Ruby core;
* Some pretty fascinating implications of the fact it can return Enumerator

Have fun! http://zverok.github.io/blog/2018-01-24-yield_self.html

V.

1 Like

Good one, thanks Victor, this improved my understanding of yield_self a lot.

···

On 1/26/18 2:51 AM, Victor Shepelev wrote:

Two things in the blog post I've wanted to share with the community:

* Why yield_self is really nice addition to Ruby core;
* Some pretty fascinating implications of the fact it can return Enumerator

Have fun! yield_self is more awesome than you could think

I really like how yield_self to enumerator interacts with other functions. Thanks for the great article.

This is a great bit of new ruby functionality but it still feels incomplete to me since "yielding self" can't really support chaining for function composition & pipelining more directly yet. It's possible that yield_self could just be the underlying piece for that, so maybe it's fine if it has an unfortunate and cumbersome name right now.

I was always hoping for something like:

     class Proc
       def yield_self
         return super unless block_given?
         proc { |*args| yield self.call(*args) }
       end
     end

I'm not sure what other purpose yielding a callable proc would have, if not to evaluate it! (I suggested it before, but it was rejected: I'm curious to understand the objection, since this seems more consistent with other languages, e.g. Lisp.)

Then we could define pipelines something like this:

     reverse = proc(&:reverse)
     upcase = proc(&:upcase)

     pipeline = reverse.yield_self(&upcase)

     puts "ybur".yield_self(&pipeline)

Or, more clearly with an added helper:

     class Proc
       def | other
         yield_self(&other)
       end
     end

     pipeline = reverse | upcase |->x { x + "!" }

Using a method like `|` on Proc could make sense since none of the logical operators are currently implemented on Proc. Suddenly with a lambda `|->` looks awfully close to Erlang's excellent pipeline operator!

Very interesting, thanks!

Dne 26.01.2018 v 8:51 Victor Shepelev napsal(a):

···

Two things in the blog post I've wanted to share with the community:

* Why yield_self is really nice addition to Ruby core;
* Some pretty fascinating implications of the fact it can return Enumerator

Have fun! yield_self is more awesome than you could think

V.

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

Andrew, there is an open discussion on procs composition:

It seems to be in the same "naming issues" hell the `yield_self` have been
(both was first proposed >5 years ago, both are pretty easy to implement,
yet nobody can agree on a possible name).

···

2018-01-27 22:38 GMT+02:00 Andrew Vit <andrew@avit.ca>:

I really like how yield_self to enumerator interacts with other functions.
Thanks for the great article.

This is a great bit of new ruby functionality but it still feels
incomplete to me since "yielding self" can't really support chaining for
function composition & pipelining more directly yet. It's possible that
yield_self could just be the underlying piece for that, so maybe it's fine
if it has an unfortunate and cumbersome name right now.

I was always hoping for something like:

    class Proc
      def yield_self
        return super unless block_given?
        proc { |*args| yield self.call(*args) }
      end
    end

I'm not sure what other purpose yielding a callable proc would have, if
not to evaluate it! (I suggested it before, but it was rejected: I'm
curious to understand the objection, since this seems more consistent with
other languages, e.g. Lisp.)

Then we could define pipelines something like this:

    reverse = proc(&:reverse)
    upcase = proc(&:upcase)

    pipeline = reverse.yield_self(&upcase)

    puts "ybur".yield_self(&pipeline)

Or, more clearly with an added helper:

    class Proc
      def | other
        yield_self(&other)
      end
    end

    pipeline = reverse | upcase |->x { x + "!" }

Using a method like `|` on Proc could make sense since none of the logical
operators are currently implemented on Proc. Suddenly with a lambda `|->`
looks awfully close to Erlang's excellent pipeline operator!

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