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 $index.") val simplePrint = (index: Int) => println(s"Printing Simple Page $index.") println("---------Method V1-----------") printPages(Document(15, "DOCX"), 5, colorPrint) println("...