Hacker News new | ask | show | jobs
by oillio 2080 days ago
A functional coding technique is strictly more powerful.

Say we want to reuse your cat search code. Sometimes we want to log it, and sometimes we want to export it as a different data structure.

You could write a function that took a company and returned a list of pets.

What if you wanted to take the first 10 of 10 million. Returning the full list wouldn't be very efficient, you would need to alter your function to support limits.

What if you were then given a list of companies. You would need to modify the function, or write a wrapper.

What if it isn't a list anymore but an async stream of companies. More modifications.

With functional techniques, we can abstract the concept of "filtering a company for pets" without worrying about the concrete implementation of how the returned pets are stored. This allows the one function to be reused efficiently in many different applications, in ways not possible with imperative code.

This is just one example of abstraction that is possible. Functional techniques are not a good fit for all problems, but they are a good tool to have in your toolbox as they make some problems much easier to reason about.

Paul Graham has written a lot on the power of functional languages. OnLisp is a good place to start: http://www.paulgraham.com/onlisp.html

Or some of his early blog posts discuss these concepts: http://www.paulgraham.com/articles.html

Another classic that will change the way you look at code design is Structure and Interpretation of Computer Programs: https://web.mit.edu/alexmv/6.037/sicp.pdf