Consider this chain of methods that manipulates the array and results in a set:
[...Array(20).keys()]
.filter(j => j % 3)
.map(j => j * 5)
.reduce((j, k) => { j.add(k); return j }, new Set())
==>
Set { 5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95 }
I know this can be rewritten by wrapping the chain:
new Set([...Array(20).keys()]
.filter(j => j % 3)
.map(j => j * 5))
==>
Set { 5, 10, 20, 25, 35, 40, 50, 55, 65, 70, 80, 85, 95 }
but that breaks the chain and forces the reader to go backwards.
Is there a way to write the last step:
.reduce((j, k) => { j.add(k); return j }, new Set())
in a simpler way using only Javascript builtin functions and without breaking the chain?
filter()and themap()when you can do all of it in thereduce()?reduceif possible. There'sArray.from, I guess I'm looking forArray.intoso I could do[].filter(j => j % 3).map(j => j * 5).into(new Set())or something like that. Similar to Java's collect where you can.collect(Collectors.toSet())