ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90804] [Ruby trunk Feature#15483] Proc or Method combination with Symbol
Date: Sat, 29 Dec 2018 12:19:33 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75978.20181229121932.53458b0f1735762d@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15483.20181229104018@ruby-lang.org

Issue #15483 has been updated by shevegen (Robert A. Heiler).


I am biased so I do not want to digress from this thread too much while explaining my bias. However had,
I still want to state a few things:

- In regards to Symbol, this is a language design decision, how Symbols are to be used. I think we can
have valid arguments for both main variants, e. g. to keep Symbols simple, or to allow more flexibility.
Personally I'd rather prefer them simple, largely because I don't feel most proposals for change make
them better and most definitely not prettier; but I have no real problem either way here.

Still, in regards to proposals allowing for more flexibility of Symbols, this leads me to:

- **Syntax consideration**. To me personally the proposed syntax is not very elegant.

In particular:

    .map(&(&:to_i >> &:chr))

Is really not pretty. We use '& three' times there; and the new >>. It does not really feel consistent
with other parts of ruby in my opinion, syntax-wise alone. I have less of a problem with a single & but
I also dislike that I have to look carefully, e. g to distinguish between a** .map(&:)** versus a **.map(&)**
variant. Do we really want to have to look for & now carefully and a : or no :, on top of it? The second
variant also packs a lot more information into the method-call, which makes it a bit hard to see what
is going on to me, e. g. **.map(&(&:to_i >> :chr.to_proc)))**. And the >> which I am also not a big fan of,
but as said in the beginning, I am biased already, so my comments will be biased as well.

- Another issue I have, and this is more general, that I do not really see the massive benefit. This is not
solely confined to the proposal here, and is obviously subject to  personal opinion/evaluation and how
you use ruby ("more than one way to use ruby", too), but more generally about some other related
proposals too, where I am not really sure if the change is needed or provides a lot of really useful
things that we need.

I understand it if the goal is more flexibility in what we can do; for example, I think I also stated before
that I am in agreement with proposals to allow arguments to methods given rather than solely be able
to use e. g.  .map(&:method SOME WAY FOR ARGUMENTS HERE). The major problem I have with most
of these proposals I have seen so far is syntax-wise. We do not have that many characters while staying
in ASCII land, but the core of ruby is very elegant and quite simple, syntax-wise (for me). Several of the
proposals in the last ~3 years or so, are, to me, syntax-wise, not really elegant. Syntax is not everything
but if I have to stare at code a lot then I'd rather look at good syntax than bad one.

Anyway, I'll close my comment here.

----------------------------------------
Feature #15483: Proc or Method combination with Symbol
https://bugs.ruby-lang.org/issues/15483#change-75978

* 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/

  parent reply	other threads:[~2018-12-29 12:19 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 ` shevegen [this message]
2019-01-08 14:47 ` [ruby-core:90930] [Ruby trunk Feature#15483] " manga.osyo
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-75978.20181229121932.53458b0f1735762d@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).