ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:91300] [Ruby trunk Feature#15557] A new class that stores a condition and the previous receiver
Date: Sun, 27 Jan 2019 05:22:13 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76546.20190127052211.dfcb59d3f9f5ff45@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15557.20190123091554@ruby-lang.org

Issue #15557 has been updated by nobu (Nobuyoshi Nakada).


zverok (Victor Shepelev) wrote:
> > `Enumerable#grep`
> 
> Well, it is not "it accepts callable", it is a part of "it accepts any pattern (including Proc)", and passing Proc into `grep` makes sense only in "polymorphic pattern situation" (we have a pattern variable, which can be Proc, or Regexp, or Range)... I mean, nobody probably writes just
> ```ruby
> something.grep(->(x) { x.odd? })
> ```

I meant an example of condition and processing by one method.
```ruby
something.grep(->(x) { x.odd? }) {|x| do_odd_thing(x)}
```

> ...because they write
> ```ruby
> something.select(&:odd?)
> ```

`Enumerable#select` with a block needs chained methods.


----------------------------------------
Feature #15557: A new class that stores a condition and the previous receiver
https://bugs.ruby-lang.org/issues/15557#change-76546

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I often see code like this:

 ```ruby
foo = default_definition
foo = some_method(foo) if some_condition(foo)
foo = another_method(foo) if another_condition(foo)
...
```

It would be nice if we can write this as a method chain. Since we now have the method `then`, I thought it would be a nice fit to introduce a method called `when`, such that putting it right in front of `then` would execute the `then` method as ordinarily only when the condition is satisfied, and returns the previous receiver otherwise so that the code above can be rewritten as:

```ruby
foo =
default_definition
.when{|foo| some_condition(foo)}
.then{|foo| some_method(foo)}
.when{|foo| another_condition(foo)}
.then{|foo| another_method(foo)}
```

This proposal is also a generalization of what I intended to cover by https://bugs.ruby-lang.org/issues/13807. That is,

```ruby
a.some_condition ? a : b
```

would rewritten as:

```ruby
a.when(&:some_condition).then{b}
```

The proposal can be implemented by introducing a class called `Condition`, which stores a condition and the previous receiver, and works with `then` in a particular way.

```ruby
class Object
  def when
    Condition.new(self, yield(self))
  end
end

class Condition
  def initialize default, condition
    @default, @condition = default, condition
  end

  def then
    @condition ? yield(@default) : @default
  end
end
```

And additionally, if we introduce a negated method `unless` (or `else`) as follows:

```ruby
class Object
  def unless
    Condition.new(self, !yield(self))
  end
end
```

then we can use that for purposes such as validation of a variable as follows:

```ruby
bar =
gets
.unless{|bar| some_validation(bar)}
.then{raise "The input is bad."}
.unless{|bar| another_validation(bar)}
.then{raise "The input is bad in another way."}
```




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

      parent reply	other threads:[~2019-01-27  5:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15557.20190123091554@ruby-lang.org>
2019-01-23  9:15 ` [ruby-core:91220] [Ruby trunk Feature#15557] A new class that stores a condition and the previous receiver sawadatsuyoshi
2019-01-23  9:27 ` [ruby-core:91223] " nobu
2019-01-23 10:21 ` [ruby-core:91225] " zverok.offline
2019-01-23 10:24 ` [ruby-core:91226] " shevegen
2019-01-25 15:47 ` [ruby-core:91263] " nobu
2019-01-25 16:03 ` [ruby-core:91264] " zverok.offline
2019-01-27  5:22 ` nobu [this message]

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-76546.20190127052211.dfcb59d3f9f5ff45@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).