From: manga.osyo@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90930] [Ruby trunk Feature#15483] Proc or Method combination with Symbol
Date: Tue, 08 Jan 2019 14:47:51 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-76128.20190108144750.0b3081719aa105aa@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15483.20181229104018@ruby-lang.org
Issue #15483 has been updated by osyo (manga osyo).
I am thinking like this.
NOTE: Here we define it as follows.
* functional object
* defined `#call` (and `#<<` `#>>`) object
* e.g. `Proc` `Method`
* blockable object
* defined `#to_proc` object
* e.g. `Symbol` `Hash`
## Current
* `Proc#<<` and `Proc#>>` arguments is functional object
* call `#call`.
* `Proc#<<` and `Proc#>>` is not call `#to_proc`
* `Proc#<<` and `Proc#>>` is not accept block argument
## Composite function in Ruby
* Composite function is functional object and functional object
* `functional object >> functional object # => OK
* `functional object >> other object` # => NG
* `other object >> functional object` # => NG
## `Symbol` is functional object
* `Symbol` is blockable object
* `Symbol` is not functional object
* Handling `Symbol` with compositing functions is incorrect
* What about other blockable objects?
* e.g. `Hash`
* `Hash` is functional object?
## `Proc#<<` is call `#to_proc` ?
* It should be explicitly converted to `Proc` (functional object) with` # to_proc`
* `proc << :hoge` => NG: `:hoge` is not `Proc`
* `proc << :hoge.to_proc` => OK : Explicitly convert `:hoge` to `Proc`
* Same as not handling `"42"` as a `Integer`
* `1 + "42"` => NG : `"42"` is not `Integer`
* `1 + "42".to_i` => OK : Explicitly convert `"42"` to `Proc`
## Proposal1 : `Symbol` to functional object
* define `Symbol#>>` `Symbol#<<` `Symbol#call`
* What about other blockable objects?
* `Hash` is functional object?
* Is it really necessary for `Symbol` ?
* Is `Symbol` really a "functinal object" ?
```ruby
# Symbol to functional object
class Symbol
def call(*args, &block)
to_proc.call(*args, &block)
end
def <<(other)
to_proc << other
end
def >>(other)
to_proc >> other
end
end
p %w{72 101 108 108 111}.map(&(:to_i >> :chr))
# => ["H", "e", "l", "l", "o"]
```
## Proposal2 : `Symbol` to functional object
* `Proc#<<(other)` to `Proc#<<(other, &block)`
* Prioritize `other` ?
```ruby
class Proc
prepend Module.new {
def <<(other = nil, &block)
# other or block?
super(other || block)
end
def >>(other = nil, &block)
# other or block?
super(other || block)
end
}
end
# :to_i convert to Proc
# must be `.>>`
p %w{72 101 108 108 111}.map(&(:to_i.to_proc.>> &:chr))
# => ["H", "e", "l", "l", "o"]
```
## Proposal3 : Define syntax sugar for `#to_proc`
* For example, define `#to_proc` to `@~`.
* or other Unary operator
* `@+` `@-` `@!` `&` ?
* Do not change current specifications
* I think this is good
```ruby
# Add ~@
class Object
# ~ is to_proc
# ~ or other unary operator?
def ~@
to_proc
end
end
# Use Symbol#to_proc
p %w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr.to_proc))
# alias ~ is to_proc
p %w{72 101 108 108 111}.map(&~:to_i >> ~:chr)
```
Thank you :)
[Japanese](https://gist.github.com/osyo-manga/1725a4a670aac54452eca92269a3822b)
----------------------------------------
Feature #15483: Proc or Method combination with Symbol
https://bugs.ruby-lang.org/issues/15483#change-76128
* Author: aycabta (aycabta .)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
In [Feature #6284], Matz said
> We need more discussion if we would add combination methods to the Symbol class.
Right, let's get started to discuss.
For your information, recent a few months I'm discussing this with @osyo .
## This is a discussion of "design"
I understand that all features of this issue have both merits and demerits, but I guess that language design is most important. All features of this issue related to each other.
## Abstract
At present, you can use `Proc#>>` or `Proc#<<` with `Symbol#to_proc`.
```ruby
%w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr.to_proc))
# => ["H", "e", "l", "l", "o"]
```
This is convenient but methods that take block can take a proc with `&` syntax sugar instead of `#to_proc` by right, like `[1, 2, 3].map(&:to_s)`. So `Symbol#to_proc` looks like too long for `Proc#>>` or `Proc#<<`. Therefore, you need new syntax sugar.
## Receiver
### `Symbol#>>` and `Symbol#<<`
`Symbol#>>` and `Symbol#<<` will be considered, but this means that `Symbol` is treated as `Proc` partially. The `[1, 2, 3].map(&:to_s)` treats `Symbol` as `Proc` partially too, but it's with pre-positioned `&`.
```ruby
%w{72 101 108 108 111}.map(&(:to_i >> :chr.to_proc))
# => ["H", "e", "l", "l", "o"]
```
I can't come up with other ideas for the `Symbol` receiver.
### New `&:symbol_name` syntax sugar for `:symbol_name.to_proc`
```ruby
%w{72 101 108 108 111}.map(&(&:to_i >> :chr.to_proc)))
# => ["H", "e", "l", "l", "o"]
```
## Argument
### Calls `#to_proc` by `Proc#>>` or `Proc#<<` internally as a duck typing
```ruby
%w{72 101 108 108 111}.map(&(:to_i.to_proc >> :chr))
# => ["H", "e", "l", "l", "o"]
```
In this case, `Proc#>>`(`:to_i.to_proc >>`) calls `Symbol#to_proc`(for `:chr`) inside.
This is useful to use with `Hash#to_proc`:
```ruby
h = { Alice: 30, Bob: 60, Cris: 90 }
%w{Alice Bob Cris}.map(&(:to_sym.to_proc >> h))
# => [30, 60, 90]
```
### `Proc#>>` and `Proc#<<` take block as an argument
```ruby
%w{72 101 108 108 111}.map(&(:to_i.to_proc >> &:chr))
```
## Combination of receiver and argument
`Symbol#>>` and calling `#to_proc` internally:
```ruby
%w{72 101 108 108 111}.map(&(:to_i >> :chr))
# => ["H", "e", "l", "l", "o"]
```
`&:symbol_name` syntax sugar for `:symbol_name.to_proc` and `Symbol#>>` and taking block:
```ruby
%w{72 101 108 108 111}.map(&(&:to_i >> &:chr))
# => ["H", "e", "l", "l", "o"]
```
--
https://bugs.ruby-lang.org/
next prev parent reply other threads:[~2019-01-08 14:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <redmine.issue-15483.20181229104018@ruby-lang.org>
2018-12-29 10:40 ` [ruby-core:90798] [Ruby trunk Bug#15483] Proc or Method combination with Symbol aycabta
2018-12-29 12:19 ` [ruby-core:90804] [Ruby trunk Feature#15483] " shevegen
2019-01-08 14:47 ` manga.osyo [this message]
2019-01-09 5:40 ` [ruby-core:90936] " nobu
2019-01-09 6:48 ` [ruby-core:90938] " manga.osyo
2019-01-09 7:18 ` [ruby-core:90939] " nobu
2019-01-09 10:27 ` [ruby-core:90944] " nobu
2019-01-09 12:37 ` [ruby-core:90945] " manga.osyo
2019-01-10 5:38 ` [ruby-core:90968] [Ruby trunk Feature#15483][Rejected] " 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-76128.20190108144750.0b3081719aa105aa@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).