Higher Order Functions
In this blog post, you’ll learn
- How to treat functions as first class citizens.
Consider the following example,
Definitely, there is a pattern between sumInts and sumCubes. In fact, we’re used to writing this as a mathematical expression -
\[\begin{align*} \sum_{x=a}^{b} f(x) \end{align*}\]We can achieve the same abstraction in Scala, through the following sytax -
Let us understand this function definition -
- sum takes in 3 parameters - a function, an Int and an Int.
- The first parameter is a function which takes an Int and returns an Int.
- This can be called like these -
Kaushik Rangadurai
Code. Learn. Explore