O'Reilly logo

Learning Scala Programming by Vikash Sharma

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

Function literals

A function literal, in simple terms, is a representation of an action that can be performed to specify the input and output parameter types:

(value1: Int, value2: Int) => Int 

This line represents a function literal, which is easily readable. It displays a function that takes two values, value1 and value2 of type Int, and returns another, Int. We've seen some examples of it, such as our ColorPrinter example where we were simply able to print color as well as simple black and white pages using just one function named printPages:

def printPages(doc: Document, lastIndex: Int, print: (Int) => Unit) = if(lastIndex <= doc.numOfPages) for(i <- 1 to lastIndex) print(i) val colorPrint = (index: Int) => println(s"Printing Color Page ...

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