7 ms·
Well, that's an unconventional way to write a for loop. Maybe the website is commending you on that :-)
by johnfn 6y ago
Well, that's an unconventional way to write a for loop. Maybe the website is commending you on that :-)
- notJim 6y agoThis is your brain on javascript :) Guessing it's bc one-liners are easier to write than for loops in the console.
- mrcartmenez 6y agoIt’s also because it’s good practice to use the lowest power loop possible. A for loop can implement a reduce, a reduce can implement a map, a map can implement a forEach, but not the inverse.
- thaumasiotes 6y agoHuh? I don't follow. In [1]: def lred(function, iterable, initializer): ...: acc = initializer ...: def reductor(el): ...: nonlocal acc ...: acc = function(acc, el) ...: for n in map(reductor, iterable): pass ...: return acc ...: In [2]: lred(lambda a,b: 10*a + b, [2,4,6,3,1], 40) Out[2]: 4024631 What's stopping you from running these equivalencies in either direction?
- jessedhillon 6y agoThey mean that the forEach runs code per element, but cannot return a transformed list of values, you need map for that. A map can run code per list element but the result must be injective (one-to-one). Reduce can do all of the above, but must return a lower dimension result from its input. And finally, a for loop is basically omnipotent.
- DangitBobby 6y ago> Reduce can do all of the above, but must return a lower dimension result from its input. What do you mean by this? You can accumulate just about anything into the resulting object, including a copy of the original array.
- SamBam 6y agoI think it's just being confusingly stated. GP already said that reduce could implement map. I think it's merely that reduce can just output a single result, of any type. (Which is already more powerful than map, which must output an array.) Of course, in fact you could hack anything in any of these, since you could be doing other work in the function called. But I think the general principle is sound.
- thaumasiotes 6y ago> (Which is already more powerful than map, which must output an array.) Don't tell Common Lisp.
- eru 6y agoEspecially, a for-loop can break out early or skip elements.
- thaumasiotes 6y agoSo can reduce. You'd put that logic in the function you pass to the call to reduce. So can map. Ditto. So can foreach. Ditto again. for el in values: if el == 3: pass else: do_something_with( el )
- mekkkkkk 6y agoNo, that's just a guard condition. Parent post was referring to break, i.e. skip the rest of the iteration.
- vsareto 6y ago>Guessing it's bc one-liners are easier to write than for loops in the console. Firefox has a pretty good multi-line editor now if anyone was looking for that
- hahajk 6y agoI haven’t had an off-by-one error since I stopped using for loops.
- halfmatthalfcat 6y agoCan save more bits by doing: Array.from({ length: 10000 }, button.click);
- phogster 6y agoNever heard of a "from" loop before.
- franky47 6y agoArray.from lets you build an array from an iterable source (often used in conjunction with `new Set` to eliminate duplicates), or in this case, of a fixed length.
- deleted 6y ago[deleted]
- klyrs 6y agoNot JavaScript, but... https://en.m.wikipedia.org/wiki/COMEFROM https://en.m.wikipedia.org/wiki/COMEFROM
- franky47 6y agoI did not know this variant, thanks. Having to fill the array with anything (even `undefined`) to iterate on it seemed such a weird concept (but then again, JS is weird).
- anonu 6y agoIt's how you would do it with a language like APL or q.