Welcome back to part two of functional programming. In the last article, I discussed some of the core concepts of functional programming. This week, I will discuss a few more fundamental concepts and next week I will wrap up by talking about where and how FP is used.
Currying is the idea of taking a function that needs multiple arguments, and breaking it up into multiple, nested functions where each takes only one argument.
The idea of currying comes from the world of mathematics; the idea being that functions are easier to reason about if there is only parameter. You probably remember this concept from Algebra where you broke up f(x, y) into f(x)f(y) and so on.
Since functional programming is based off of pure functions which simply take an input and return an output and don't affect anything outside of their own function scope, we can make the assumption that given the same input, we will always get the same output. This let's us cache, or memoize, the results of the function: If we pass in an input to a function once, we can cache the result, and in the future, use the cached result instead of running the whole expensive function all over again.
Other types of programming generally use loops to iterate through items. In a functional style of programming, functions call themselves over and over again until an exit condition is met. You can also have multiple functions calling each other recursively which is called 'mutual recursion.'
What do you think of these mini articles? Leave a comment and let me know.
See you next week!