3 ms·
Functional programming is the answer. In python the answer is just this: def action(thing) ... map(action, array_of_things) I'm sure there's some s
by rthomas6 7y ago
Functional programming is the answer. In python the answer is just this:
def action(thing)
...
map(action, array_of_things)
I'm sure there's some similar solution in Javascript.
- rejschaap 7y agoAgreed, that is a much better direction to go. In this particular case you could combine map with filter instead of hiding the filter inside "action"
- doc_gunthrop 7y agoBetter yet just combine map and filter into a list comprehension: def action(thing): ... return [func(x) for x in thing if func2(x)]
- ldd 7y agoyup. const action = (thing) => ... array_of_things.map(action)
- IceDane 7y agoI agree, but I think it's worth pointing out that this abstraction costs more in a language like python. Unless I'm mistaken, I don't believe python has any way to fuse these higher order operations into a single pass.
- fnord123 7y ago[action(x) for x in array_of_things if x > 7] Action is the function you are applying. `for x in ...` will pull an iterator from a generator which could be doing a lot of interesting stuff. And `if x > 7` is the filter.
- raiflip 7y agoI think its a bit of a misnomer to say functional programming is the answer because you can call a function instead of doing a loop, because all you're doing is shifting responsibility to the function you're calling to implement the iteration (which is probably done via tail recursion in FP language). Really the answer to avoiding loops via FP is to use tail recursion, but tail recursion is not always more natural than a loop.