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

Get Pragmatic Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.