Skip to content

Commit ad69caf

Browse files
committed
Functions in scala can be declared in various syntax style
1 parent 9b8f415 commit ad69caf

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

Functions/Functions_1.scala

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Functions declared with syntax style - 1
3+
* Here same functions f0, f1, f2 are defined in different style.
4+
* Check other file Functions_2.scala, Functions_3.scala, Functions_4.scala
5+
*/
6+
7+
object Functions_1 extends App
8+
{
9+
val f1:Function1[Int, Int] = new Function1[Int, Int]
10+
{
11+
def apply(x:Int):Int = x+1
12+
}
13+
14+
val f0:Function0[Int] = new Function0[Int]
15+
{
16+
def apply() = 1
17+
}
18+
19+
20+
val f2:Function2[Int, String, String] = new Function2[Int, String, String]
21+
{
22+
def apply(x:Int, y:String) = y + x
23+
}
24+
25+
println("f0() :" + f0())
26+
println("f1(3) :" + f1(3))
27+
println("f2(3, cool) :" + f2(3, "Cool"))
28+
}
29+
30+
/**
31+
Sample Output:
32+
33+
f0() :1
34+
f1(3) :4
35+
f2(3, cool) :Cool3
36+
37+
*/

Functions/Functions_2.scala

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Functions declared with syntax style - 2
3+
**/
4+
5+
object Functions_2 extends App
6+
{
7+
val f1:Int => Int = new Function1[Int, Int]
8+
{
9+
def apply(x:Int):Int = x+1
10+
}
11+
12+
val f0:() => Int = new Function0[Int]
13+
{
14+
def apply() = 1
15+
}
16+
17+
18+
val f2:(Int, String) => String = new Function2[Int, String, String]
19+
{
20+
def apply(x:Int, y:String) = y + x
21+
}
22+
23+
println("f0() :" + f0())
24+
println("f1(3) :" + f1(3))
25+
println("f2(3, cool) :" + f2(3, "Cool"))
26+
}
27+
28+
/**
29+
Sample Output:
30+
31+
f0() :1
32+
f1(3) :4
33+
f2(3, cool) :Cool3
34+
35+
*/

Functions/Functions_3.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Functions declared with syntax style - 3
3+
**/
4+
5+
object Functions_3 extends App
6+
{
7+
val f1:Int => Int = (x:Int) => x+1
8+
9+
val f0:() => Int = () => 1
10+
11+
val f2:(Int, String) => String = (x:Int, y:String) => y + x
12+
13+
println("f0() :" + f0())
14+
println("f1(3) :" + f1(3))
15+
println("f2(3, cool) :" + f2(3, "Cool"))
16+
}
17+
18+
/**
19+
Sample Output:
20+
21+
f0() :1
22+
f1(3) :4
23+
f2(3, cool) :Cool3
24+
25+
*/

Functions/Functions_4.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Functions declared with syntax style - 4
3+
* 1. Functions are a trait that is instantiated anonymously
4+
* 2. apply method in function in not needed to call explicitly
5+
* 3. Using tuple ,you can return multiple items from a function
6+
*
7+
**/
8+
9+
object Functions_4 extends App
10+
{
11+
val f1 = (x:Int) => x+1
12+
13+
val f0 = () => 1
14+
15+
val f2 = (x:Int, y:String) => y + x
16+
17+
//here f3 returns a tuple of 2 elements
18+
val f3 = (x:String) => (x, x.size)
19+
20+
println("f0() :" + f0())
21+
println("f1(3) :" + f1(3))
22+
println("f2(3, cool) :" + f2(3, "Cool"))
23+
println("f3(\"Spider\") :" + f3("Spider"))
24+
}
25+
26+
27+
28+
/**
29+
Sample Output:
30+
31+
f0() :1
32+
f1(3) :4
33+
f2(3, cool) :Cool3
34+
f3("Spider") :(Spider,6)
35+
36+
*/

Functions/MethodsOrFunctions.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Functions are their own objects
3+
* Methods are not Functions
4+
* Methods belong to context
5+
*
6+
*/
7+
8+
object MyObject
9+
{
10+
val f = (x:Int) => x+1
11+
def g(x:Int) = x+1
12+
}
13+
14+
object MethodsOrFunctions extends App
15+
{
16+
println("MyObject.f(4) :" + MyObject.f(4))
17+
println("MyObject.f.apply(4) :" + MyObject.f.apply(4))
18+
println("MyObject.g(4) :" + MyObject.g(4))
19+
}
20+
21+
/**
22+
Sample Output
23+
MyObject.f(4) :5
24+
MyObject.f.apply(4) :5
25+
MyObject.g(4) :5
26+
27+
28+
*/

0 commit comments

Comments
 (0)