News for hackers. Stuff that matters.
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.