function* range(x, y) {
while (x <= y) {yield x++}
// or ‘<‘ if you want half-open one
}
for (let n of range(3, 5)){
console.log(n)
}
array = [...range(3, 5)]
console.log(array)Array.prototype.indexOf is also pretty simple as are many core functions.
'clamp' and 'sortBy', as in Lodash.
Set methods for union, intersection, difference, etc.
let bar = [{id: zz, }, {id: y},...]
let foo = {}
bar.forEach(v => foo[v.id] = v)
I'd love to have something like:
let foo = bar.toMap(v => [v.id, v])
You still need to create the middle datastructure ( [[k1,v1], [k2, v2]] ) but it is an improvement :)
let foo = Object.fromEntries(bar.map((v) => [v.id, v])); import { map } from "my-iter-lib";
Object.fromEntries(map((v) => [v.id, v], bar));
Or with the proposed iterator-helpers: Object.fromEntries(
Iterator.from(bar).map((v) => [v.id, v]),
)
However the inner arrays are harder to get rid of... In fact even OP’s oneliner defines the inner arrays. I would hope there were some engine optimizations though which could minimize their footprint.It started as a Function Composition proposal (using the pipe operator |>) but after a change of leadership it has turned into something much different. We might need another perspective on the current trajectory of this proposal, as in its current form it seems to many in the community it might take JS in the wrong direction.
Thanks!
await chain(
Object.keys(envars)
.map(envar => `${envar}=${envars[envar]}`)
.join(' '),
text => `$ ${text}`,
text => chalk.dim(text, 'node', args.join(' ')),
colored => console.log(colored),
)
I don’t want to sound too negative, but this seems like a pointless extension only to bring the burden of support for some syntactic flavor.React example is special though. It feels like very helpful in expr-only context, but in-array ifs and fors would do better there:
<ul>
{[
for (v of vs) {
const text = foo(v)
yield <li>{text}</li>
},
if (vs.length == 0) {
yield <li>No items</li>
},
]}