3 ms·
I'm not sure if it would be easier to explain a map / filter to a first year student vs implementing the patten manually using a for loop and if statement... S
by jpc0 1y ago
I'm not sure if it would be easier to explain a map / filter to a first year student vs implementing the patten manually using a for loop and if statement...
Seems like a pretty easy example to make practically, for map have a collection of things, say balls or black. Pick up each one and do a thing to them, paint them blue for example.
For filter do the same except have two different colour balls, if they are yellow they get thrown away, of they are blue they get put in a bucket.
A for loop doing exactly the same you would need to explain the topic at hand, as well as explain iterating an index etc...
- renerick 1y agoExplaining loops is independent of the concepts of collections though. It's also more general, since map/filter/reduce use some kind of loops under the hood anyway, the fact that probably shouldn't be ignored in education process. Unless of course you go with pure functional recursive iterator, but good luck explaining that one. Maps and filters also require understanding of higher order functions and the very idea of passing function around as a value. I would argue that implementing map/filter with a loop and then demonstrating how this pattern is generalized as .map()/.filter() functions is better and more accessible