Summary
In this chapter, we looked at higher order functions, namely functions that take other functions as parameters, return a function as a result, or both.
We looked at the strategy pattern's Java implementation. We used Scala's higher order functions instead, to implement the algorithm encapsulation. Instead of Java classes implementing an interface, we passed in function literals. Then, we looked at three important patterns related to higher order functions.
We looked at Functors and then at the collection's map method. The second pattern was a Monad. We also looked at the flatMap
method. The third pattern was monoids. In a related context, we also looked at foldLeft
.
We applied the know-how to write an inverted index program. We looked at both the Java and Scala versions. The Scala code was short and idiomatic. We discussed the groupBy
method and related idioms. Then, we used the foldLeft
method instead of groupBy
and saw how it works on a Monoid.
We wrapped up with a...