5 ms·
So, I was sort of expecting this article to use recursion instead of a regular loop, but it was not to be, as the author went ahead and used a for-loop anyway a
by sk5t 7y ago
So, I was sort of expecting this article to use recursion instead of a regular loop, but it was not to be, as the author went ahead and used a for-loop anyway and abstracted a bit of logic into a separate function. Silly premise overall.
- csours 7y agoI was hoping for GOTO or JMP
- setr 7y agoReally its the same kind of thing as teaching people to "run the code in their heads" when trying to debug -- an extremely primitive yet necessary skill... but not a utility interesting enough to bother running by HN IMO
- bctnry 7y agoI was hoping for APL/J-style stuff or something like using `map` and `forEach` but no. That's a big letdown.
- necovek 7y agoI would consider those "some kind of loop".
- squiggleblaz 7y agoI was hoping for a massive parallel solution! You just run the same function on a million cores at once, with different inputs. But I think there's a big difference between a language that lets you expect that iteration b will happen after iteration a, vs a language that merely gives you guarantee the result is derived from application of the function onto the necessary inputs. The latter can be correctly parallelised, but the former can only ever be executed in serial. `map f [1,2,3]` (in Haskell) can be solved in parallel or in serial. `[1,2,3].map(f)` (in modern JS) must be executed in serial, since f might be the result of `() => { var j = 0; return (i) => { j += i; return j }}`.
- myhf 7y agoFor some clarification of the "loopless" terminology in J: https://code.jsoftware.com/wiki/Vocabulary/Loopless https://code.jsoftware.com/wiki/Vocabulary/Loopless
- akavi 7y ago`map` is strictly less "powerful" than a "loop", so I'd argue that's misleading. Which I'd also argue is a good thing. Using the least powerful construct available both communicates to future readers of the code, and allows higher level optimization (eg, `map` is trivially parallelizable, an arbitrary loop is not)
- thaumasiotes 7y agoBut `map` is not a primitive. It's just a function implemented with... a loop. It's hard to argue that that doesn't make it "some kind of loop".
- jonathankoren 7y agoI agree. Honestly, I thought he was going to be using Scheme, and maybe derive a for loop by using recursion. But honestly, I hate the misleading title of this post, because it completely misdirects what the post is about, which is just breaking logic up into a functions. Typically, you'd teach this by explaining the concept of functions, rather than focusing on loops, which is completely irrelevant to the proper use of functions.
- deleted 7y ago[deleted]
- hawski 7y agoI was hoping for something about how eBPF verifier does not allow loops.
- deaddodo 7y agoI agree. The correct solution (given javascript as the language of choice) is something along the following lines: let highlightProducts = (products, index = 0) => { if(index >= products.length) { return; } product = products[index]; if(product.name.indexOf('mountain') !== -1) { products[index].highlighted = true; } else if(product.stock > 100 && product.price < 5) { products[index].highlighted = true; } else if(product.category == 'outdoors' && product.price < 35 && product.price > 20) { products[index].highlighted = true; } highlightProducts(products, index+1); } Personally, I think this exercise is completely fruitless except to teach recursion.
- dan-robertson 7y agoI was expecting the answer to look something like: products.forEach((p) => p.highlighted = ( /mountain/.test(p.name) || (p.stock > 100 && p.price < 5) || (p.category == 'outdoors' && 20 < p.price && p.price < 35))) And if one wants the (implicitly hidden) behaviour that highlighted products stay highlighted forever, just add the condition p.highlighted to the disjunction above.
- dhbradshaw 7y agoWant a job? ;-D
- deaddodo 7y agoforEach is a loop, so this doesn't answer the basic question OP presented.
- dan-robertson 7y agoThe standard doesn’t specify whether or not forEach is a “loop” or not. One could implement it using tail recursion. FWIW I would claim that such recursion should also be called a loop, because the control flow goes in a loop.
- 7y ago