Reader small image

You're reading from  Learning Scala Programming

Product typeBook
Published inJan 2018
Reading LevelBeginner
PublisherPackt
ISBN-139781788392822
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Vikash Sharma
Vikash Sharma
author image
Vikash Sharma

Vikash Sharma is a software developer and open source technology evangelist. He tries to keep things simple, which helps him write clean and manageable code. He has invested a large amount of time learning and implementing Scala code, and he has authored video courses for Scala. He works as a developer at SAP Labs.
Read more about Vikash Sharma

Right arrow

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("...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Learning Scala Programming
Published in: Jan 2018Publisher: PacktISBN-13: 9781788392822

Author (1)

author image
Vikash Sharma

Vikash Sharma is a software developer and open source technology evangelist. He tries to keep things simple, which helps him write clean and manageable code. He has invested a large amount of time learning and implementing Scala code, and he has authored video courses for Scala. He works as a developer at SAP Labs.
Read more about Vikash Sharma