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?)
Yes. curriculum starts off with recursion, then moves on to higher order functions and nobody touches recursion again.
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?
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.)
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))
```
```
function highlightProducts(aListOfProducts, i=0){
}```
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.)