2 ms·
I find it interesting that you use reduce so often. Most python functions that handle two inputs handle it generically by handling a list of inputs, so passing
by nhumrich 3y ago
I find it interesting that you use reduce so often. Most python functions that handle two inputs handle it generically by handling a list of inputs, so passing in a full list exploded as args is sufficient and reduce isn't needed.
All of pythons built-ins at least work this way like sum(), print(), etc. As such, I have not ever reached for reduce. Sometimes a comprehension inside a function call though.
What types of situations do you use reduce for?
- corethree 3y agoIt's used to replace all looping. A combination of map and reduce is an alternative to loops. Also you don't need map. You can use list comprehension to replace map. But list comprehensions can't replace reduce. Technically reduce can also replace map. The only primitive you need to do all your iterations is technically just reduce. If you want to get more elegant than you don't even need reduce. You can eliminate all redundant primitives and use functions themselves for iteration. It's Just recursion.
- laurent_du 3y agoIf you use reduce to replace looping, your coworkers probably hate you. Don't write Clojure in Python.
- corethree 3y agoIt's called functional programming. Coworkers don't hate it it's more readable in general to split between map and reduce and forego looping. It's less readable to use reduce for all looping even though technically you can.