Higher Order Functions

Scala Comments

In this blog post, you’ll learn

  1. 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 -

  1. sum takes in 3 parameters - a function, an Int and an Int.
  2. The first parameter is a function which takes an Int and returns an Int.
  3. This can be called like these -

Kaushik Rangadurai

Code. Learn. Explore

Share this post