O'Reilly logo

Pragmatic Scala by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Limitations of Normal Functions

At the core of functional programming are functions or so-called higher-order functions. To get a feel for what these are, let’s start with a familiar function.

To find the sum of values in a given range 1 to number we’d probably write code like this:

 
def sum(number: Int) = {
 
var result = 0
 
for​(i <- 1 to number) {
 
result += i
 
}
 
result
 
}

That’s familiar code—we’ve all written code like this a million times over in different languages. That’s called imperative style—you tell not only what to do, but also how to do it. That’s dictating a low level of details. In Scala, you can write imperative code like this where it make sense, but you’re not restricted to that.

While this code got the work done, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required