ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: shannonskipper@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:94517] [Ruby master Feature#16113] Partial application
Date: Fri, 23 Aug 2019 23:51:39 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-80953.20190823235139.fdb8d083e290f257@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16113.20190819100716@ruby-lang.org

Issue #16113 has been updated by shan (Shannon Skipper).


I like Hanmac's idea.

``` ruby
Klass.:meth.curry.()
```

Seems very close to:

``` ruby
Klass.:meth.w()
```
 
 I know its previously been said that `#curry` is a bit of an easter egg. Would it be acceptable to add keyword argument support for `#curry`?

@zverok If `#curry` gets support for keyword arguments, would it suffice for this case or do you think `#w` would still be called for?

----------------------------------------
Feature #16113: Partial application
https://bugs.ruby-lang.org/issues/16113#change-80953

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
**Preface:** One of the main "microstructures" of the code we use is chaining methods-with-blocks; and we really love to keep those blocks DRY when they are simple. Currently, for DRY-ing up simple blocks, we have:

* `foo(&:symbol)`
* `foo(&some.method(:name))` (as of 2.7, `foo(&some.:name)`)
* Currently disputed "nameless block args": `foo { something(@1) }` or `foo { something(@) }` or `foo { something(it) }`

**Proposal:** I argue that short and easy-to-remember partial application of blocks and methods can make methods-with-blocks much more pleasant and consistent to write, and continue softly shifting Ruby towards "functional" (while staying true to language's spirit). 

In order to achieve this, I propose method `{Symbol,Method,Proc}#w` (from `with`), which will produce `Proc` with _last_ arguments bound.

Example of usability:

```ruby
# No-shortcuts: fetch something and parse as JSON:
fetch(urls).map { |body| JSON.parse(body) }
# Could be already (2.7+) shortened to:
fetch(urls).map(&JSON.:parse)

# But if you have this:
fetch(urls).map { |body| JSON.parse(body, symbolize_names: true) }
# How to shorten it, to don't repeat body?
# "Nameless block args" answer:
fetch(urls).map { JSON.parse(@1, symbolize_names: true) }
# Partial application answer:
fetch(urls).map(&JSON.:parse.w(symbolize_names: true))
```

I believe that the latter (while can be easily met with usual "hard to understand for a complete novice") provides the added value of producing proper "functional object", that can be stored in variables and constants, and generally lead to new approaches to writing Ruby code. 

Another example:
```ruby
(6..11).map(&:**.w(2)).map(&:clamp.w(20, 50))
# => [36, 49, 50, 50, 50, 50]
```

Reference implementation:
```ruby
class Symbol
  def w(*args)
    proc { |receiver, *rest| receiver.send(self, *rest, *args) }
  end
end

class Method
  def w(*args)
    proc { |receiver, *rest| self.call(receiver, *rest, *args) }
  end
end

class Proc
  def w(*args)
    prc = self
    proc { |*rest| prc.call(*rest, *args) }
  end
end
```



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

  parent reply	other threads:[~2019-08-23 23:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16113.20190819100716@ruby-lang.org>
2019-08-19 10:07 ` [ruby-core:94432] [Ruby master Feature#16113] Partial application zverok.offline
2019-08-19 11:11 ` [ruby-core:94433] " shevegen
2019-08-19 12:23 ` [ruby-core:94434] " hanmac
2019-08-20  1:28 ` [ruby-core:94440] " shannonskipper
2019-08-23 23:51 ` shannonskipper [this message]
2019-08-24 10:57 ` [ruby-core:94529] " zverok.offline

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-80953.20190823235139.fdb8d083e290f257@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).