Scala Tutorials Part #27 - Higher Order Functions


Higher Order Functions

This is part 27 of the Scala tutorial series. Check here for the full series.

Index

Introduction

In part 19, we saw how functions are treated as first class citizens in Scala. In this article we are going to take a look at higher order functions. Higher order functions are functions which are,

Example with collections

First we will define a List with some values.

val list = List(10,11,12,13)

Next, lets define a function that doubles each value that is given to it.

def doubleValue = (x: Int) => x * x

The List class has something called a map function which takes in other function and produces a new List. We are going to feed our doubleValue function to it.

val doubledList = list.map(x => doubleValue(x))

This gives us a list with values (100, 121, 144, 169). The map function is a classic example of a higher order function. We can also give it an anonymous function in case we do not want to define a function explicitly.

val doubledList = list.map(x => x * x)

Creating a custom higher order function

In order to fully understand Higher Order functions, its best that we learn to create one ourselves. Let’s create a function that applies a pattern to a given string.

 //Function that decorates the given string with the given logic
def HTMLStringDecorator(text:String,f:String => String): String = {
f(text)
}

This is an example of a function that takes another function as a parameter. f is a variable which is of type function and one which takes in a String and gives out another String. Next, we will create a function which given a string appends a <title> tag to it and which can be passed into the HTMLStringDecorator function.

 //Function that appends the title tag
def titleTag(data:String): String => String = {
_ : String => s"<title>$data</title>"
}

Unlike HTMLStringDecorator this returns a function itself. We can then call the decorator with the title tag appender function.

val message = "hello"

println(
HTMLStringDecorator(
message,
titleTag(message)
)
)

//Prints
//<title>hello</title>

This might seem simple, but using such patterns we can build powerful DSL based syntax and languages. Higher order functions are just one piece of the puzzle, we still have partially applied functions, closures, currying etc., which can help build systems that make complex patterns much simpler to work with.

Closing notes

We saw a very basic overview of higher order functions. At this point this knowledge should be enough to move forward. But we must remember that with such power comes responsibility and not everything must be dealt with a DSL/Higher order functions. Design and best practices is a completely different area which we will explore in later tutorials.


Tagged Under


Scala


Search this website...




Keeping up with blogs...

I blog occasionally and mostly write about Software engineering and the likes. If you are interested in keeping up with new blog posts, you should follow me on twitter where I usually tweet when I publish them. You can also use the RSS feed , or even subscribe via email below.

Feedio Subscribe


Share & Like

If you like this post, then you can either share/discuss/vote up on the below sites.



Thoughts ...

Please feel free to share your comments below for discussion

Blog comments powered by Disqus