How does Closure function works in Scala?

0 votes
Can anyone explain how closure function works in Scala?
Jul 26, 2019 in Apache Spark by Mehnaj
39 views

1 answer to this question.

0 votes

Hi,

A closure in Scala is a function whose value depends on variables declared outside of it. Let’s take an example:

scala> var c=5

c: Int = 5

scala> val mul2=(a:Int,b:Int)=>(a+b)*c

mul2: (Int, Int) => Int = $$Lambda$1533/239864031@6d90e705

scala> mul2(2,3)

The output will be:

Int = 25

scala> c=7

c: Int = 7

scala> mul2(2,3)

The output will be:

Int = 35

Here mul2 reads the new value of ‘c’ when we call it a second time.

answered Jul 26, 2019 by Gitika
• 25,440 points

Related Questions In Apache Spark

0 votes
1 answer

How to execute a function in apache-scala?

Hi, Here is a simple example of how ...READ MORE

answered Jul 1, 2019 in Apache Spark by Gitika
• 25,440 points
56 views
0 votes
1 answer

How can we iterate any function using "foreach" function in scala?

Hi, Yes, "foreach" function you use because it will ...READ MORE

answered Jul 5, 2019 in Apache Spark by Gitika
• 25,440 points
235 views
0 votes
1 answer

How to use nested function in Scala?

Hey, With Scala, we can define a Scala ...READ MORE

answered Jul 26, 2019 in Apache Spark by Gitika
• 25,440 points
73 views
0 votes
1 answer

How does partitioning work in Spark?

By default a partition is created for ...READ MORE

answered May 31, 2018 in Apache Spark by nitinrawat895
• 10,840 points
88 views
+1 vote
1 answer
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,840 points
3,842 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 10,840 points
525 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyFr ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
20,336 views
0 votes
1 answer

What is recursion in Scala function? How does it works?

Hey, A Scala function involves recursion when it ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 25,440 points
37 views
0 votes
1 answer

How 'Scala Higher Order function' works in Scala?

Hey, Scala allows the definition of a higher-order function. These ...READ MORE

answered Jul 30, 2019 in Apache Spark by Gitika
• 25,440 points
42 views