- high order functions
- laziness
- composing
reduce, filter and map.
Comparing with the java code, Jim describes how these 3 methods can help us reduce the duplicated boilerplate java code: like loop, iteration. He even emphasizes that these 3 methods - reduce, filter and map can reduce 90% of the loop code!
Copied from his slide, the benefits of the high order functions are:
- Allows common code structures to reused
- Abstracts away details
- Isolates the essential parts of the code
- Allows easier unit testing
- Lets you think at a higher level
- Allows easy parallelism
As a java developer, the implementation code is not new to me, mostly the template method pattern is used. Actually I already saw the similar benefit in Spring framework: it is so amazing the JdbcTemplate can removed lots of the JDBC boilerplate code! But I did not aware that it shares the same idea with the function programming.
Since I learned that high order functions are so important, so I decide to investigate more and see how to implement them in other OO languages beside Java.
Javascript
Joel has a nice article about function programming using javascript: Can Your Programming Language Do This?
Groovy
I checked the Groovy API and recalled what I learned before, so I found the equivalent methods for reduce, filter and map in Collection API are:
reduce => inject()
filter => findAll()
map => collect()
Groovy functional programming link
Ruby
Groovy is very similar with Ruby, especially when I learned Groovy, I suspect if Groovy copies some concept from Ruby, since the collection API is so similar.
reduce => reduce(), inject()
map => map(), collect()
filter => select(), find_all()
This article is really great to explain them more in Ruby
Other resources
Why functional programming matters
Conclusion
- To use functional programming, it would be better to use the language that support functional programming in first class. I have to say Java implementation is too ugly, Groovy and Ruby are much better.
- Changing mindset to functional programming is hard but really important. Functional programming is another way of programming to elevate your abstraction layer.
Next Action
To pursue the way of Functional Programming, you need to pick up a new Functional Programming language ASAP, since I learned LISP through SICP, so Clojure would be my next choice.
No comments:
Post a Comment