# Iterative Assignment

**URL:** <https://es.discourse.group/t/iterative-assignment/672>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [February 28, 2021, 9:24am UTC](https://es.discourse.group/t/iterative-assignment/672 "2021-02-28T09:24:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [February 28, 2021, 9:24am UTC](https://es.discourse.group/t/iterative-assignment/672/1 "2021-02-28T09:24:12Z")

</div>

This proposal was inspired by the **Julia** programming language.  
In this language, there are several operators for easily manipulating arrays.  
we can use:  
`.+ .- .* ./ .% .^ etc.`  
and their compound assignment versions:  
`.+= .-= .*= ./= .^= etc.`

I would like to generalise this and propose a generalised **compound iterative assignment operator(.=)**

It works a bit like map / forEach

syntax:

```javascript
/ **Iterative Assignment** /

let arr = [1, 2, 3, 4]
arr .= ([k, v]) => v + 1

let obj = { a: 1, b: 2, c: 3, d: 4 }
obj .= ([k, v]) => v + 1

console.log(arr)
// [2, 3, 4, 5]

console.log(obj)
// { a: 2, b: 3, c: 4, d: 5 }

```

currently this operator can simplify the follow common and tedious code.

```javascript
let arr = [1, 2, 3, 4],
fn = f => arr.map( (v, k) => f(k, v) )
arr = fn( (k, v) => v + 1 )

let obj = { a: 1, b: 2, c: 3, d: 4 }
fn = f => {
    let pairs = []
    for( [k, v] of Object.entries(obj) )
        pairs.push( f(k, v) )
    return Object.fromEntries( pairs )
}
obj = fn( (k, v) => [k, v + 1] )

console.log(arr)
// [2, 3, 4, 5]

console.log(obj)
// { a: 2, b: 3, c: 4, d: 5 }

```

What are the pros and cons of this??  
What do you guys think of this idea??

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [February 28, 2021, 10:33am UTC](https://es.discourse.group/t/iterative-assignment/672/2 "2021-02-28T10:33:11Z")

</div>

Would the left-side be limited to Array and Object, or would other collections be allowed like TypedArray, Map, Set etc?

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [February 28, 2021, 11:33am UTC](https://es.discourse.group/t/iterative-assignment/672/3 "2021-02-28T11:33:05Z")

</div>

I guess anything that has a key and a value would work. i.e iterables

---

<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:** [February 28, 2021, 3:19pm UTC](https://es.discourse.group/t/iterative-assignment/672/4 "2021-02-28T15:19:19Z")

</div>

Here's some more background on this proposal, based on what I understand of these dot operators in Julia:

Element-wise operators such as `.*` are found in statistical-focused languages such as Julia, R, and MatLab, and allow one to operate on each element of a vector or matrix.

i.e., if `.*` were added to javascript, it would look something like this (javascript doesn't really have a notion of row and column vectors, so there's multiple ways to make this interpretation):

```javascript
// Multiplying two "row" vectors
[1, 0, 3] .* [2, 3, 7] // [2, 0, 21]
// Multiplying two "column" vectors
[[1], [0], [3]] .* [[2], [3], [7]] // [[2], [0], [21]]
// Multiplying two matricies
[[1, 2], [3, 4]] .* [[2, 3], [4, 5]] // [[2, 6], [12, 20]]
// Multiplying a row and a column vector to produce a matrix
[1, 2] .* [[1], [2]] // [[1, 2], [2, 4]]
// Multiplying a vector with a scalar
[1, 2] .* 3 // [3, 6]

```

If I understand this proposed generalized operator correctly, it's mostly a `.map()` followed by an assignment. If this is the case, I presume this proposal is mostly focussing on the last case - operating on a vector with a scalar, as a .map() by itself doesn't have the power to do any of the other tasks.

As javascript isn't designed to be a statistical language, most websites don't need that kind of power, I feel like statistical functionality is best suited for external libraries - meaning, I wouldn't want to copy over operators such as ".\*" verbatum into javascript. _however_, if there is something useful to be gleaned from these statistical languages, that can be adapted to work in a general-purpose way, then it's worth considering.

---

<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:** [February 28, 2021, 3:44pm UTC](https://es.discourse.group/t/iterative-assignment/672/5 "2021-02-28T15:44:42Z")

</div>

The iterator protocol is a common way to read values from objects; there is no common protocol to add or change them. Arrays have push, Maps have set, Sets have add, strings are immutable (or have + or concat), etc.

I’m also very skeptical of the claim that this kind of code is common.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [March 2, 2021, 12:32pm UTC](https://es.discourse.group/t/iterative-assignment/672/6 "2021-03-02T12:32:42Z")

</div>

I am, too, though it's worth noting the broad use of lenses in Haskell that more or less fill exactly this role and their limited use in JS as well.

---

<div class="post-metadata">

**Author:** ![jithujoshyjy](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jithujoshyjy/32/399_2.png) [@jithujoshyjy](https://es.discourse.group/u/jithujoshyjy)\
**Post date:** [March 2, 2021, 1:13pm UTC](https://es.discourse.group/t/iterative-assignment/672/7 "2021-03-02T13:13:08Z")

</div>

Funny that there are nothing to focus in JS 😂 😂

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [March 2, 2021, 1:19pm UTC](https://es.discourse.group/t/iterative-assignment/672/8 "2021-03-02T13:19:42Z")

</div>

[Lenses aren't exactly a built-in thing in Haskell, either. 😉](https://github.com/ekmett/lens)
