News for hackers. Stuff that matters.
Re:What if you couldn’t program with loops? by yjhoney on Monday July 15, 2019 @12:54AM (#20438241)
I teach a JS coding class and I had the luxury of creating the curriculum. To the horror of my peers, I decided that coding with loops was not necessary for beginners and got rid of loops altogether from our curriculum. I thought OP was proposing the same thing, but I was wrong. the refactoring was good, but my students will probably end up with the following result (more in line with your no loops title):

```

function highlightProducts(aListOfProducts, i=0){

  if (i === aListOfProducts.length) return null

  if(productShouldBeHighlighted(product)){

    product.highlighted = true

  }

  return highlightProducts(aListOfProducts, i + 1)
}

```

The added benefit of the above approach is that debugging is easier! If you want to debug the last 5 items, you can simply call the function like this:

highlightProducts(aListOfProducts, aListOfProducts.length – 5)

Ultimately, though, everyone should be using higher order functions (map, reduce filter, find, etc.)

Re:What if you couldn’t program with loops? by taneq on Monday July 15, 2019 @05:13AM (#20439186)
Our first year coding lecturer did similar, but instead of teaching us Java (which is what most other classes used at the time) he insisted we use Gofer.

From the point of view of a student who was excited to finally learn "real" programming, having this toy language crammed down our throats instead, along with a side-serving of smugly academic FP-superiority, was enough to sour me on FP for a long time.

Hopefully you gave your students this more as "here's a neat trick to add to your toolbox" and not "listen up plebs, iteration is for losers."

Re:What if you couldn’t program with loops? by BalinKing on Monday July 15, 2019 @01:48AM (#20438463)
This seems a bit worrying to me as a general principle, since Javascript's lack of tail call optimization (TCO) means that you can blow the stack pretty easily by manually recursing. Just my two cents :-)
Re:What if you couldn’t program with loops? by BalinKing on Monday July 15, 2019 @01:49AM (#20438466)
(Out of curiosity, have you considered starting at raw recursion – like in your example – but then moving on to using utility functions like `map` from e.g. Ramda or lodash?)
Re:What if you couldn’t program with loops? by yjhoney on Monday July 15, 2019 @02:11AM (#20438544)
Yes. curriculum starts off with recursion, then moves on to higher order functions and nobody touches recursion again.
Re:What if you couldn’t program with loops? by yjhoney on Monday July 15, 2019 @02:10AM (#20438540)
eventually everyone defaults to using forEach, every, map, reduce, filter, etc. Nobody writes recursion in codebases.
Re:What if you couldn’t program with loops? by helloiloveyou on Monday July 15, 2019 @01:01AM (#20438272)
Hey! you are the one from the blog comment :) It must be pretty fun to create those curriculums!! I agree with you that the restriction of not using loops can also direct you to apply recursion.

However do you believe recursion makes it easier to debug certain parts of the array when you can adjust the starting and stoping points in the guard of the for loop?

Re:What if you couldn’t program with loops? by yjhoney on Monday July 15, 2019 @02:16AM (#20438560)
Ultimately, everyone should use higher order functions. In your case (as other commenters have said), map or forEach should have been used.

I don't really want to make a statement that recursion makes debugging easier because I don't code like that at work. I use utility functions (map, reduce, filter, etc.)

Re:What if you couldn’t program with loops? by jtgi on Monday July 15, 2019 @02:08AM (#20438534)
thought you were going to say you teach functional basics like map, filter, reduce, etc. converting between recursive/iterative is a good exercise but teaching one and not the other seems unnecessary, especially considering you see more loops in the wild. --- also fwiw can just ``` highlightProducts(aListOfProducts.splice(-5)) ```