News for hackers. Stuff that matters.
Re:What if you couldn’t program with loops? by sk5t on Monday July 15, 2019 @12:00AM (#20438028)
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.
Re:What if you couldn’t program with loops? by deaddodo on Monday July 15, 2019 @02:54AM (#20438687)
I 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.
Re:What if you couldn’t program with loops? by maury91 on Monday July 15, 2019 @10:32AM (#20441173)
By using rest is possible to write it in an immutable (and nicer) way:

  const highlightProducts = ([product, ...productsToProcess], processedProducts = []) => {
    if(productsToProcess.length === 0) {
      return processedProducts;
    }

    processedProducts.push({
      ...product,
      highlighted: product.name.indexOf('mountain') !== -1
        || product.stock > 100 && product.price < 5
        || product.category == 'outdoors' && product.price < 35 && product.price > 20
    })
    return highlightProducts(productsToProcess, processedProducts);
  }

or in shorter:

  const highlightProducts = ([product, ...productsToProcess], processedProducts = []) =>
    productsToProcess.length === 0
      ? processedProducts
      : highlightProducts(productsToProcess, [
          ...processedProducts,
          {
            ...product,
            highlighted: product.name.indexOf('mountain') !== -1
             || product.stock > 100 && product.price < 5
             || product.category == 'outdoors' && product.price < 35 && product.price > 20
          }
        ])
Re:What if you couldn’t program with loops? by deaddodo on Tuesday July 16, 2019 @12:51AM (#20447556)
I agree this is a better solution and immutability is preferable.
Re:What if you couldn’t program with loops? by dan-robertson on Monday July 15, 2019 @05:57AM (#20439347)
I 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.
Re:What if you couldn’t program with loops? by deaddodo on Monday July 15, 2019 @04:44PM (#20445056)
forEach is a loop, so this doesn't answer the basic question OP presented.
Re:What if you couldn’t program with loops? by dan-robertson on Monday July 15, 2019 @07:23PM (#20446207)
The 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.
Re:What if you couldn’t program with loops? by deaddodo on Tuesday July 16, 2019 @12:48AM (#20447545)
You're splitting hairs. I think we can both agree that no implementation does it as such. And if you're going to go to that low of a level, there's no such thing as a loop beyond emergent behavior (they're all conditional branches/jumps).
Re:What if you couldn’t program with loops? by dhbradshaw on Monday July 15, 2019 @12:04PM (#20442087)
Want a job?

;-D

Re:What if you couldn’t program with loops? by bctnry on Monday July 15, 2019 @12:33AM (#20438142)
I was hoping for APL/J-style stuff or something like using `map` and `forEach` but no. That's a big letdown.
Re:What if you couldn’t program with loops? by necovek on Monday July 15, 2019 @01:02AM (#20438275)
I would consider those "some kind of loop".
Re:What if you couldn’t program with loops? by squiggleblaz on Monday July 15, 2019 @01:53AM (#20438484)
I 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 }}`.

Re:What if you couldn’t program with loops? by akavi on Monday July 15, 2019 @10:27AM (#20441118)
`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)

Re:What if you couldn’t program with loops? by thaumasiotes on Monday July 15, 2019 @03:35PM (#20444345)
But `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".
Re:What if you couldn’t program with loops? by myhf on Monday July 15, 2019 @02:14AM (#20438554)
For some clarification of the "loopless" terminology in J:

https://code.jsoftware.com/wiki/Vocabulary/Loopless

Re:What if you couldn’t program with loops? by csours on Monday July 15, 2019 @12:10AM (#20438062)
I was hoping for GOTO or JMP
Re:What if you couldn’t program with loops? by deaddodo on Monday July 15, 2019 @02:54AM (#20438685)
In javascript, labels can only be used in the context of loops (break and continue to a label). But that would be the proper solution in most languages.
Re:What if you couldn’t program with loops? by ozzmotik on Monday July 15, 2019 @01:06AM (#20438293)
i knew i couldn't be the only one
Re:What if you couldn’t program with loops? by setr on Monday July 15, 2019 @12:18AM (#20438082)
Really 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
Re:What if you couldn’t program with loops? by jonathankoren on Monday July 15, 2019 @12:42AM (#20438190)
I 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.

Re:What if you couldn’t program with loops? by hawski on Monday July 15, 2019 @02:51AM (#20438672)
I was hoping for something about how eBPF verifier does not allow loops.