I teach Ruby and Python to a lot of people — in formal courses, and in one-on-one pairing sessions, both online and in person. I’ve found that for many people, the whole notion of functional programming seems strange and difficult, as well as something of a waste of time. After all, if you have objects, why would you need functional programming?
The answer, of course, is that no single paradigm has all of the answers; each has its strengths and weaknesses. Understanding how to use them together can provide great benefits. As a result, I spend time when teaching both Ruby and Python on the basics of functional programming, and then the various functions and methods that each language provides in this area.
Of these, the function that most often causes people to wrinkle their noses and/or get confused is “reduce”. And to be honest, I often tell students in my classes that “reduce” is one of those functions that is incredibly powerful and clever, but for which you sometimes need to wait in order to find a use case. I decided to explore some use cases, and ways in which “reduce” could be used — and I hope that these have been useful.
To summarize, here are the posts that I wrote on this topic:
- Introduction to “reduce”
- Calculating Scrabble scores with “reduce”
- Implementing “min” and “max” with “reduce”
- Creating collections with “reduce”
- Python dictionaries with “reduce”
- Ruby hashes with “reduce”
- Implementing “map” with “reduce”
- Implementing “filter” with “reduce”
I hope that this series has been useful and interesting, and would appreciate hearing ideas for additional deep-dives into areas of Python, Ruby, or programming in general. What subjects do you find confusing? What methods do you think are somewhat useless? Let me know, and I’ll try to address them in future blog posts!