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:
/** 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.
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??