# Proposal: \`&\` operator as shorthand for items in iterations

**URL:** <https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799>\
**Category:** 💡 Ideas\
**Tags:** proposal, iterator-helpers\
**Created:** [May 28, 2021, 2:55pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799 "2021-05-28T14:55:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bprogyan](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bprogyan/32/841_2.png) [@bprogyan](https://es.discourse.group/u/bprogyan)\
**Post date:** [May 28, 2021, 2:55pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/1 "2021-05-28T14:55:10Z")

</div>

Hi Everyone,

This is one usecase I see a lot common these days, where we want to iterate over items in a list and invoke some method. The current syntax allows us to do this by the following pattern:

```javascript
const newArray = array.map(item => item.someMethod())

```

My proposal is to create new operator `&` to replace the common boilerplate part and introduce the pattern such as following:

```javascript
const newArray = array.map(&.someMethod())

```

This will reduce some cognitive complexity and thus improving developer productivity, and reduce a good amount of bundle size.

PS: This is my first post here. Please let me know if I am missing something in the process. Thanks.

---

<div class="post-metadata">

**Author:** ![bprogyan](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bprogyan/32/841_2.png) [@bprogyan](https://es.discourse.group/u/bprogyan)\
**Post date:** [May 28, 2021, 3:11pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/2 "2021-05-28T15:11:52Z")

</div>

Example from Ruby here: [to\_proc (Symbol) - APIdock](https://apidock.com/rails/Symbol/to_proc)

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [May 28, 2021, 3:28pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/3 "2021-05-28T15:28:10Z")

</div>

That seems like a lot to add to the language to avoid what could be 4 characters `_=>_` - it seems to me like it would be _more_ complexity to add a new way to do this?

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [May 28, 2021, 3:35pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/4 "2021-05-28T15:35:26Z")

</div>

This [partial application](https://github.com/tc39/proposal-partial-application) proposal is related. It allows you to put a "?" as one of the function parameters to create a partially applied function, that can later be used in places like array.map()

For example:

```javascript
const newArray = array.map(transformElement(?))

```

It's certainly much more restrictive than what you're asking for. There's been a little talk (in the bug reports section, feel free to look around) about allowing partial expressions, where the "?" can be placed anywhere in the expression (see [this](https://github.com/tc39/proposal-partial-application/issues/20) for example). This, unfortunately, carries a lot of issues. For example, you now need some algorithm to determine where the boundaries of this partial expression should lie, or have some explicit syntax to mark the boundaries. You also need to figure out what should happen if two "?" appear in the same expression (is that one function with two parameters, or two separate functions - there's use cases for both). By the time you achieve this, you may be left with some fairly complicated syntax, and @ljharb's arrow function suggestion would have been simpler.

---

<div class="post-metadata">

**Author:** ![bprogyan](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bprogyan/32/841_2.png) [@bprogyan](https://es.discourse.group/u/bprogyan)\
**Post date:** [May 28, 2021, 6:50pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/5 "2021-05-28T18:50:58Z")

</div>

> to avoid what could be 4 characters `_=>_`

If you take account for currying this could effect in compounding manner. I definitely see merit in that.  
Even though I won't say that writing extra syntax is hurting a lot, but will certainly won't like to settle for less.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [May 28, 2021, 7:45pm UTC](https://es.discourse.group/t/proposal-operator-as-shorthand-for-items-in-iterations/799/6 "2021-05-28T19:45:37Z")

</div>

Given that Javascript is generally an uncurried language by default, and most Javascript users don't do currying, I would hope/expect multiple uses of `&` in a single expression to result in a single, multi-parameter function, not a curried function, as that would be a more useful result for most people. This is actually the direction that the partial-application proposal is taking too - multiple uses of `?` will result in a single, multi-parameter function.
