SlideShare a Scribd company logo
Scala Functions & Closures




Neelkanth Sachdeva

Consultant / Software Engineer

Knoldus Software LLP , New Delhi

neelkanthsachdeva.wordpress.com

neelkanth@knoldus.com
Methods

The most common way to define a function is
as a member of some object.Such a function is
called a Method.
/*
 * Normal Functions
 */

object LongLines extends App{

    def ProcessFile(fileName: String, width: Int)
{
        val source = Source.fromFile(fileName)
        for (line <- source.getLines)
          processLine(fileName, width, line)
    }

    private def processLine(filename: String,
      width: Int, line: String) {
      if (line.length > width)
        println(filename + ": " + line)
    }
}
Local functions

Programs should be decomposed into many small functions that each
do a well-defined task. One problem with this approach is that all the
helper function names can pollute the program name space
In Java →
1.Defining private methods.
In Scala →
1. Defining private methods also works
                 +
2. You can define functions inside other functions
/*
 * Defining Local Functions More Easily
 */
object LongLines extends App {

    def ProcessFile(fileName: String,width: Int) {

        def processLine(line: String) {
          if (line.length > width)
            println(fileName + ": " + line)
        }

        val source = Source.fromFile(fileName)
        for (line <- source.getLines)
          processLine(line)
    }
}
First-class functions

Scala has first-class functions. Not only can you
define functions and call them, but you can write
down functions as unnamed literals and then
pass them around as values.
Functions & Closures in Scala
Function Literals

A function literal is compiled into a class that
when instantiated at runtime is a function value.
Then What is the difference ?
Thus the distinction between function literals and
values is that function literals exist in the source
code, whereas function values exist as objects at
runtime.
Example:

Simple function literal :-

 (x: Int) => x + 1

The => designates that this function converts the thing on the left (any
integer x) to the thing on the right (x + 1). So, this is a function mapping
any integer x to x + 1.
Function values are objects, so

→ you can store them in variables

 scala> var increase = (x: Int) => x + 1
 increase: (Int) => Int = <function>

They are functions too , so

→ you can invoke them using the usual parentheses
  function-call notation.

   scala> increase(10)
   res0: Int = 11
We've seen the nuts and bolts of function literals and function
values. Many Scala libraries give you opportunities to use
them e.g

foreach method:

scala> val someNumbers = List(-11, -10, -5, 0, 5, 10)
someNumbers: List[Int] = List(-11, -10, -5, 0, 5, 10)
scala> someNumbers.foreach((x: Int) => println(x))

filter method:

scala> someNumbers.filter((x: Int) => x > 0)
Short forms of function literals
Scala provides a number of ways to leave out redundant information and
write function literals more briefly. Keep your eyes open for these
opportunities, because they allow you to remove clutter from your code.


One way to make a function literal more brief is to leave off the parameter
types. Thus, the previous example with filter could be written like this:


scala> someNumbers.filter((x) => x > 0)
res7: List[Int] = List(5, 10)
Even leave the parentheses around x
scala> someNumbers.filter( x => x > 0)
res7: List[Int] = List(5, 10)
Placeholder syntax

To make a function literal even more concise, you
can use underscores as placeholders for one or
more parameters, so long as each parameter
appears only one time within the function literal.


scala> someNumbers.filter( _ > 0)
res9: List[Int] = List(5, 10)
Partially applied functions

someNumbers.foreach( println _ )
Scala treats this short form exactly as if you had
written the following:
someNumbers.foreach(x => println(x))


When you use an underscore in this way, you are
writing a partially applied function.
Examples:



scala> def sum(a: Int, b: Int, c: Int) = a + b + c
sum: (Int,Int,Int)Int

scala> sum(1, 2, 3)
res0: Int = 6

scala> val a = sum _
a: (Int, Int, Int) => Int = <function>

scala> a.apply(1, 2, 3)
res1: Int = 6
scala> val b = sum(1, _: Int, 3)
b: (Int) => Int = <function>

scala> b(2)
res15: Int = 6

In this case, b.apply invoked sum(1, 2, 3).

scala> val c = sum

<console>:5: error: missing arguments for method sum...
follow this method with `_' if you want to treat it as
a partially applied function
val c = sum
          ˆ
Closures
x → by contrast, is a bound variable, because it
does have a meaning in the context of the
function: it is defined as the function’s lone
parameter, an Int.


more → more is a free variable, because the
Function literal does not itself give a meaning to it.
Functions & Closures in Scala
So the closures is :

The function value (the object) that’s created at
runtime from this function literal is called a
closure.
(x: Int) => x + 1, is called a closed term,
Thus a function value created at runtime from
this function literal is not a closure in the strictest
Sense .


(x: Int) => x + more, is an open term. Therefore,
any function value created at runtime from (x: Int) => x +
more will by definition require that a binding for its free
variable, more, be captured. The resulting function value,
which will contain a reference to the captured more variable,
is called a closure,
Repeated Parameters

Scala allows you to indicate that the last
parameter to a function may be repeated. This
allows clients to pass variable length argument
lists to the function.
Functions & Closures in Scala
Thank You
Ad

More Related Content

What's hot (19)

Java Script Language Tutorial
Java Script Language TutorialJava Script Language Tutorial
Java Script Language Tutorial
vikram singh
 
Javascript
JavascriptJavascript
Javascript
Gita Kriz
 
Namespace in C++ Programming Language
Namespace in C++ Programming LanguageNamespace in C++ Programming Language
Namespace in C++ Programming Language
Himanshu Choudhary
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Books
BooksBooks
Books
Steven Foster Murray
 
Linq Introduction
Linq IntroductionLinq Introduction
Linq Introduction
Neeraj Kaushik
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Lambdas HOL
Lambdas HOLLambdas HOL
Lambdas HOL
Oleg Tsal-Tsalko
 
Scala oo (1)
Scala oo (1)Scala oo (1)
Scala oo (1)
Sandip Kumar
 
Python functions
Python functionsPython functions
Python functions
Aliyamanasa
 
Basics of Functional Programming
Basics of Functional ProgrammingBasics of Functional Programming
Basics of Functional Programming
Sartaj Singh
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Functional Java 8 in everyday life
Functional Java 8 in everyday lifeFunctional Java 8 in everyday life
Functional Java 8 in everyday life
Andrea Iacono
 
Chapter 16 Dictionaries
Chapter 16 DictionariesChapter 16 Dictionaries
Chapter 16 Dictionaries
Praveen M Jigajinni
 
Intro to functional programming
Intro to functional programmingIntro to functional programming
Intro to functional programming
Assaf Gannon
 
First approach in linq
First approach in linqFirst approach in linq
First approach in linq
Vignesh Nethaji
 
Functions1
Functions1Functions1
Functions1
DrUjwala1
 
Pxb For Yapc2008
Pxb For Yapc2008Pxb For Yapc2008
Pxb For Yapc2008
maximgrp
 

Viewers also liked (17)

Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
Knoldus Inc.
 
Traits in scala
Traits in scalaTraits in scala
Traits in scala
Knoldus Inc.
 
Simple build tool
Simple build toolSimple build tool
Simple build tool
Knoldus Inc.
 
Slickdemo
SlickdemoSlickdemo
Slickdemo
Knoldus Inc.
 
Knolx session
Knolx sessionKnolx session
Knolx session
Knoldus Inc.
 
Intro lift
Intro liftIntro lift
Intro lift
Knoldus Inc.
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Intoduction on Playframework
Intoduction on PlayframeworkIntoduction on Playframework
Intoduction on Playframework
Knoldus Inc.
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in Scala
Knoldus Inc.
 
WorkingWithSlick2.1.0
WorkingWithSlick2.1.0WorkingWithSlick2.1.0
WorkingWithSlick2.1.0
Knoldus Inc.
 
Traits inscala
Traits inscalaTraits inscala
Traits inscala
Knoldus Inc.
 
Akka vikas hazrati
Akka vikas hazratiAkka vikas hazrati
Akka vikas hazrati
Knoldus Inc.
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
Knoldus Inc.
 
FitNesse With Scala
FitNesse With ScalaFitNesse With Scala
FitNesse With Scala
Knoldus Inc.
 
Dependency injection in Scala
Dependency injection in ScalaDependency injection in Scala
Dependency injection in Scala
Knoldus Inc.
 
Highcharts
HighchartsHighcharts
Highcharts
Knoldus Inc.
 
Reactive programming with scala and akka
Reactive programming with scala and akkaReactive programming with scala and akka
Reactive programming with scala and akka
Knoldus Inc.
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
Knoldus Inc.
 
Implicit conversion and parameters
Implicit conversion and parametersImplicit conversion and parameters
Implicit conversion and parameters
Knoldus Inc.
 
Intoduction on Playframework
Intoduction on PlayframeworkIntoduction on Playframework
Intoduction on Playframework
Knoldus Inc.
 
String Interpolation in Scala
String Interpolation in ScalaString Interpolation in Scala
String Interpolation in Scala
Knoldus Inc.
 
WorkingWithSlick2.1.0
WorkingWithSlick2.1.0WorkingWithSlick2.1.0
WorkingWithSlick2.1.0
Knoldus Inc.
 
Akka vikas hazrati
Akka vikas hazratiAkka vikas hazrati
Akka vikas hazrati
Knoldus Inc.
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
Knoldus Inc.
 
FitNesse With Scala
FitNesse With ScalaFitNesse With Scala
FitNesse With Scala
Knoldus Inc.
 
Dependency injection in Scala
Dependency injection in ScalaDependency injection in Scala
Dependency injection in Scala
Knoldus Inc.
 
Reactive programming with scala and akka
Reactive programming with scala and akkaReactive programming with scala and akka
Reactive programming with scala and akka
Knoldus Inc.
 
Ad

Similar to Functions & Closures in Scala (20)

Scala functions
Scala functionsScala functions
Scala functions
Knoldus Inc.
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
ehsoon
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
Knoldus Inc.
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
mikisato746
 
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
lemike859
 
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
maedakarina479
 
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
villar97897
 
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
jafiradnan336
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Python functions part12
Python functions  part12Python functions  part12
Python functions part12
Vishal Dutt
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
andreassenrolf537
 
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
balliuvilla512
 
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
barajasfr42922
 
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
arcosarturo900
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
ehsoon
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
Knoldus Inc.
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
Martin Odersky
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
home
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
美洲杯买球-美洲杯买球在哪个软件买球-美洲杯买球买球软件下载|【​网址​🎉ac55.net🎉​】
mikisato746
 
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
世预赛下注-世预赛下注投注-世预赛下注投注网|【​网址​🎉ac10.net🎉​】
lemike859
 
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
欧洲杯体彩-网上怎么押注欧洲杯体彩-欧洲杯体彩押注app官网|【​网址​🎉ac99.net🎉​】
maedakarina479
 
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
世预赛投注-网上怎么押注世预赛投注-世预赛投注押注app官网|【​网址​🎉ac123.net🎉​】
villar97897
 
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩投注app-欧洲杯足彩下注app|【​网址​🎉ac22.net🎉​】
jafiradnan336
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
Sandip Kumar
 
Python functions part12
Python functions  part12Python functions  part12
Python functions part12
Vishal Dutt
 
OOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT TutorialOOPS Object oriented Programming PPT Tutorial
OOPS Object oriented Programming PPT Tutorial
amitnitpatna
 
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
ESIT135 Problem Solving Using Python Notes of Unit-2 and Unit-3
prasadmutkule1
 
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩买球从哪买-欧洲杯足彩买球去哪买|【​网址​🎉ac22.net🎉​】
andreassenrolf537
 
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
美洲杯投注-美洲杯投注比分-美洲杯投注比分投注|【​网址​🎉ac44.net🎉​】
balliuvilla512
 
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
欧洲杯买球-欧洲杯买球猜球-欧洲杯买球猜球网站|【​网址​🎉ac10.net🎉​】
barajasfr42922
 
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
欧洲杯足彩-欧洲杯足彩怎么押注-欧洲杯足彩押注怎么玩|【​网址​🎉ac22.net🎉​】
arcosarturo900
 
Ad

More from Knoldus Inc. (20)

Angular Hydration Presentation (FrontEnd)
Angular Hydration Presentation (FrontEnd)Angular Hydration Presentation (FrontEnd)
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Optimizing Test Execution: Heuristic Algorithm for Self-HealingOptimizing Test Execution: Heuristic Algorithm for Self-Healing
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
Self-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - HealeniumSelf-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
Kanban Metrics Presentation (Project Management)
Kanban Metrics Presentation (Project Management)Kanban Metrics Presentation (Project Management)
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
Java 17 features and implementation.pptx
Java 17 features and implementation.pptxJava 17 features and implementation.pptx
Java 17 features and implementation.pptx
Knoldus Inc.
 
Chaos Mesh Introducing Chaos in Kubernetes
Chaos Mesh Introducing Chaos in KubernetesChaos Mesh Introducing Chaos in Kubernetes
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
GraalVM - A Step Ahead of JVM Presentation
GraalVM - A Step Ahead of JVM PresentationGraalVM - A Step Ahead of JVM Presentation
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
DAPR - Distributed Application Runtime Presentation
DAPR - Distributed Application Runtime PresentationDAPR - Distributed Application Runtime Presentation
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
Introduction to Azure Virtual WAN Presentation
Introduction to Azure Virtual WAN PresentationIntroduction to Azure Virtual WAN Presentation
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
Introduction to Argo Rollouts Presentation
Introduction to Argo Rollouts PresentationIntroduction to Argo Rollouts Presentation
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
Intro to Azure Container App Presentation
Intro to Azure Container App PresentationIntro to Azure Container App Presentation
Intro to Azure Container App Presentation
Knoldus Inc.
 
Insights Unveiled Test Reporting and Observability Excellence
Insights Unveiled Test Reporting and Observability ExcellenceInsights Unveiled Test Reporting and Observability Excellence
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
Introduction to Splunk Presentation (DevOps)
Introduction to Splunk Presentation (DevOps)Introduction to Splunk Presentation (DevOps)
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
Code Camp - Data Profiling and Quality Analysis Framework
Code Camp - Data Profiling and Quality Analysis FrameworkCode Camp - Data Profiling and Quality Analysis Framework
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
AWS: Messaging Services in AWS Presentation
AWS: Messaging Services in AWS PresentationAWS: Messaging Services in AWS Presentation
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
Amazon Cognito: A Primer on Authentication and Authorization
Amazon Cognito: A Primer on Authentication and AuthorizationAmazon Cognito: A Primer on Authentication and Authorization
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
ZIO Http A Functional Approach to Scalable and Type-Safe Web DevelopmentZIO Http A Functional Approach to Scalable and Type-Safe Web Development
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
Managing State & HTTP Requests In Ionic.
Managing State & HTTP Requests In Ionic.Managing State & HTTP Requests In Ionic.
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 
Angular Hydration Presentation (FrontEnd)
Angular Hydration Presentation (FrontEnd)Angular Hydration Presentation (FrontEnd)
Angular Hydration Presentation (FrontEnd)
Knoldus Inc.
 
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Optimizing Test Execution: Heuristic Algorithm for Self-HealingOptimizing Test Execution: Heuristic Algorithm for Self-Healing
Optimizing Test Execution: Heuristic Algorithm for Self-Healing
Knoldus Inc.
 
Self-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - HealeniumSelf-Healing Test Automation Framework - Healenium
Self-Healing Test Automation Framework - Healenium
Knoldus Inc.
 
Kanban Metrics Presentation (Project Management)
Kanban Metrics Presentation (Project Management)Kanban Metrics Presentation (Project Management)
Kanban Metrics Presentation (Project Management)
Knoldus Inc.
 
Java 17 features and implementation.pptx
Java 17 features and implementation.pptxJava 17 features and implementation.pptx
Java 17 features and implementation.pptx
Knoldus Inc.
 
Chaos Mesh Introducing Chaos in Kubernetes
Chaos Mesh Introducing Chaos in KubernetesChaos Mesh Introducing Chaos in Kubernetes
Chaos Mesh Introducing Chaos in Kubernetes
Knoldus Inc.
 
GraalVM - A Step Ahead of JVM Presentation
GraalVM - A Step Ahead of JVM PresentationGraalVM - A Step Ahead of JVM Presentation
GraalVM - A Step Ahead of JVM Presentation
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)Nomad by HashiCorp Presentation (DevOps)
Nomad by HashiCorp Presentation (DevOps)
Knoldus Inc.
 
DAPR - Distributed Application Runtime Presentation
DAPR - Distributed Application Runtime PresentationDAPR - Distributed Application Runtime Presentation
DAPR - Distributed Application Runtime Presentation
Knoldus Inc.
 
Introduction to Azure Virtual WAN Presentation
Introduction to Azure Virtual WAN PresentationIntroduction to Azure Virtual WAN Presentation
Introduction to Azure Virtual WAN Presentation
Knoldus Inc.
 
Introduction to Argo Rollouts Presentation
Introduction to Argo Rollouts PresentationIntroduction to Argo Rollouts Presentation
Introduction to Argo Rollouts Presentation
Knoldus Inc.
 
Intro to Azure Container App Presentation
Intro to Azure Container App PresentationIntro to Azure Container App Presentation
Intro to Azure Container App Presentation
Knoldus Inc.
 
Insights Unveiled Test Reporting and Observability Excellence
Insights Unveiled Test Reporting and Observability ExcellenceInsights Unveiled Test Reporting and Observability Excellence
Insights Unveiled Test Reporting and Observability Excellence
Knoldus Inc.
 
Introduction to Splunk Presentation (DevOps)
Introduction to Splunk Presentation (DevOps)Introduction to Splunk Presentation (DevOps)
Introduction to Splunk Presentation (DevOps)
Knoldus Inc.
 
Code Camp - Data Profiling and Quality Analysis Framework
Code Camp - Data Profiling and Quality Analysis FrameworkCode Camp - Data Profiling and Quality Analysis Framework
Code Camp - Data Profiling and Quality Analysis Framework
Knoldus Inc.
 
AWS: Messaging Services in AWS Presentation
AWS: Messaging Services in AWS PresentationAWS: Messaging Services in AWS Presentation
AWS: Messaging Services in AWS Presentation
Knoldus Inc.
 
Amazon Cognito: A Primer on Authentication and Authorization
Amazon Cognito: A Primer on Authentication and AuthorizationAmazon Cognito: A Primer on Authentication and Authorization
Amazon Cognito: A Primer on Authentication and Authorization
Knoldus Inc.
 
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
ZIO Http A Functional Approach to Scalable and Type-Safe Web DevelopmentZIO Http A Functional Approach to Scalable and Type-Safe Web Development
ZIO Http A Functional Approach to Scalable and Type-Safe Web Development
Knoldus Inc.
 
Managing State & HTTP Requests In Ionic.
Managing State & HTTP Requests In Ionic.Managing State & HTTP Requests In Ionic.
Managing State & HTTP Requests In Ionic.
Knoldus Inc.
 

Recently uploaded (20)

Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
Toru Tamaki
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
DNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in NepalDNF 2.0 Implementations Challenges in Nepal
DNF 2.0 Implementations Challenges in Nepal
ICT Frame Magazine Pvt. Ltd.
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Agentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community MeetupAgentic Automation - Delhi UiPath Community Meetup
Agentic Automation - Delhi UiPath Community Meetup
Manoj Batra (1600 + Connections)
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 
Understanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdfUnderstanding SEO in the Age of AI.pdf
Understanding SEO in the Age of AI.pdf
Fulcrum Concepts, LLC
 
Slack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teamsSlack like a pro: strategies for 10x engineering teams
Slack like a pro: strategies for 10x engineering teams
Nacho Cougil
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
React Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for SuccessReact Native for Business Solutions: Building Scalable Apps for Success
React Native for Business Solutions: Building Scalable Apps for Success
Amelia Swank
 
Config 2025 presentation recap covering both days
Config 2025 presentation recap covering both daysConfig 2025 presentation recap covering both days
Config 2025 presentation recap covering both days
TrishAntoni1
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025Top 5 Qualities to Look for in Salesforce Partners in 2025
Top 5 Qualities to Look for in Salesforce Partners in 2025
Damco Salesforce Services
 
Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)Design pattern talk by Kaya Weers - 2025 (v2)
Design pattern talk by Kaya Weers - 2025 (v2)
Kaya Weers
 
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
論文紹介:"InfLoRA: Interference-Free Low-Rank Adaptation for Continual Learning" ...
Toru Tamaki
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
AI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamsonAI-proof your career by Olivier Vroom and David WIlliamson
AI-proof your career by Olivier Vroom and David WIlliamson
UXPA Boston
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025Zilliz Cloud Monthly Technical Review: May 2025
Zilliz Cloud Monthly Technical Review: May 2025
Zilliz
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Crazy Incentives and How They Kill Security. How Do You Turn the Wheel?
Christian Folini
 

Functions & Closures in Scala

  • 1. Scala Functions & Closures Neelkanth Sachdeva Consultant / Software Engineer Knoldus Software LLP , New Delhi neelkanthsachdeva.wordpress.com neelkanth@knoldus.com
  • 2. Methods The most common way to define a function is as a member of some object.Such a function is called a Method.
  • 3. /* * Normal Functions */ object LongLines extends App{ def ProcessFile(fileName: String, width: Int) { val source = Source.fromFile(fileName) for (line <- source.getLines) processLine(fileName, width, line) } private def processLine(filename: String, width: Int, line: String) { if (line.length > width) println(filename + ": " + line) } }
  • 4. Local functions Programs should be decomposed into many small functions that each do a well-defined task. One problem with this approach is that all the helper function names can pollute the program name space In Java → 1.Defining private methods. In Scala → 1. Defining private methods also works + 2. You can define functions inside other functions
  • 5. /* * Defining Local Functions More Easily */ object LongLines extends App { def ProcessFile(fileName: String,width: Int) { def processLine(line: String) { if (line.length > width) println(fileName + ": " + line) } val source = Source.fromFile(fileName) for (line <- source.getLines) processLine(line) } }
  • 6. First-class functions Scala has first-class functions. Not only can you define functions and call them, but you can write down functions as unnamed literals and then pass them around as values.
  • 8. Function Literals A function literal is compiled into a class that when instantiated at runtime is a function value. Then What is the difference ? Thus the distinction between function literals and values is that function literals exist in the source code, whereas function values exist as objects at runtime.
  • 9. Example: Simple function literal :- (x: Int) => x + 1 The => designates that this function converts the thing on the left (any integer x) to the thing on the right (x + 1). So, this is a function mapping any integer x to x + 1.
  • 10. Function values are objects, so → you can store them in variables scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function> They are functions too , so → you can invoke them using the usual parentheses function-call notation. scala> increase(10) res0: Int = 11
  • 11. We've seen the nuts and bolts of function literals and function values. Many Scala libraries give you opportunities to use them e.g foreach method: scala> val someNumbers = List(-11, -10, -5, 0, 5, 10) someNumbers: List[Int] = List(-11, -10, -5, 0, 5, 10) scala> someNumbers.foreach((x: Int) => println(x)) filter method: scala> someNumbers.filter((x: Int) => x > 0)
  • 12. Short forms of function literals Scala provides a number of ways to leave out redundant information and write function literals more briefly. Keep your eyes open for these opportunities, because they allow you to remove clutter from your code. One way to make a function literal more brief is to leave off the parameter types. Thus, the previous example with filter could be written like this: scala> someNumbers.filter((x) => x > 0) res7: List[Int] = List(5, 10) Even leave the parentheses around x scala> someNumbers.filter( x => x > 0) res7: List[Int] = List(5, 10)
  • 13. Placeholder syntax To make a function literal even more concise, you can use underscores as placeholders for one or more parameters, so long as each parameter appears only one time within the function literal. scala> someNumbers.filter( _ > 0) res9: List[Int] = List(5, 10)
  • 14. Partially applied functions someNumbers.foreach( println _ ) Scala treats this short form exactly as if you had written the following: someNumbers.foreach(x => println(x)) When you use an underscore in this way, you are writing a partially applied function.
  • 15. Examples: scala> def sum(a: Int, b: Int, c: Int) = a + b + c sum: (Int,Int,Int)Int scala> sum(1, 2, 3) res0: Int = 6 scala> val a = sum _ a: (Int, Int, Int) => Int = <function> scala> a.apply(1, 2, 3) res1: Int = 6
  • 16. scala> val b = sum(1, _: Int, 3) b: (Int) => Int = <function> scala> b(2) res15: Int = 6 In this case, b.apply invoked sum(1, 2, 3). scala> val c = sum <console>:5: error: missing arguments for method sum... follow this method with `_' if you want to treat it as a partially applied function val c = sum ˆ
  • 18. x → by contrast, is a bound variable, because it does have a meaning in the context of the function: it is defined as the function’s lone parameter, an Int. more → more is a free variable, because the Function literal does not itself give a meaning to it.
  • 20. So the closures is : The function value (the object) that’s created at runtime from this function literal is called a closure.
  • 21. (x: Int) => x + 1, is called a closed term, Thus a function value created at runtime from this function literal is not a closure in the strictest Sense . (x: Int) => x + more, is an open term. Therefore, any function value created at runtime from (x: Int) => x + more will by definition require that a binding for its free variable, more, be captured. The resulting function value, which will contain a reference to the captured more variable, is called a closure,
  • 22. Repeated Parameters Scala allows you to indicate that the last parameter to a function may be repeated. This allows clients to pass variable length argument lists to the function.
  翻译: