...

/

Summation with Currying

Summation with Currying

In this lesson, we will rewrite our sum function using the currying method.

So far, we have been using specific summation functions along with a sum function to perform summation operations. Is it possible to remove the need for the summation functions and use the sum function directly?

Currying will help us in doing just that. But for this, we will still need helper functions. For a recap, the helper functions cube, factorial, and id are defined below.

def id(x: Int): Int = x
def cube(x: Int): Int = x*x*x
def factorial(x: Int): Int = if (x==0) 1 else x * factorial(x-1)

Let’s now look at how we should define our sum function using currying.

Breaking it Down

Firstly, we need a general function that can be used to sum the numbers between two integers. ...