Scala Day 2 Practice:
==============================================
-----------------------------------------------------
Functions in Scala:
------------------------------------------------------
1. anonymous functions
2. named functions
3. curried functions
( x : Int ) => { x + 1 }
val add = ( x : Int ) : Int => { x + 1 }
add(1)
add(10)
-----------------------------------------------------
scala> val add = ( x : Int ) => { x + 1 }
add: Int => Int = <function1>
scala> ( x : Int ) => { x + 1 }
res2: Int => Int = <function1>
scala> add(1)
res3: Int = 2
scala> add(10)
res4: Int = 11
-----------------------------------------------------
def add( x : Int ) = { x + 1 }
def add( x : Int ) : Int = { x + 1 }
scala> def add( x : Int ) = { x + 1 }
add: (x: Int)Int
scala> def add( x : Int ) : Int = { x + 1 }
add: (x: Int)Int
scala> add(1)
res5: Int = 2
scala> add(10)
res6: Int = 11
-----------------------------------------------------
val id = 1
if( id == 1) {
println("condtion match")
} else {
println("condtion doesn't match")
}
def validate(id : Int) = {
if( id == 1) {
println("condtion match")
} else {
println("condtion doesn't match")
}
}
validate(id)
scala> def validate(id : Int) = {
| if( id == 1) {
| println("condtion match")
| } else {
| println("condtion doesn't match")
| }
| }
validate: (id: Int)Unit
scala> validate(id)
condtion match
scala> val id = 2
id: Int = 2
scala> validate(id)
condtion doesn't match
-----------------------------------------------------
def factorial( n : Int ) : Int = {
if ( n == 1 ) 1
else n * factorial( n - 1 )
}
scala> def factorial( n : Int ) : Int = {
| if ( n == 1 ) 1
| else n * factorial( n - 1 )
| }
factorial: (n: Int)Int
scala> factorial(1)
res14: Int = 1
scala> factorial(5)
res15: Int = 120
scala> factorial(6)
res16: Int = 720
-----------------------------------------------------
scala> 1 to 5
res17: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)
scala> 1 to 5 by 2
res18: scala.collection.immutable.Range = Range(1, 3, 5)
scala> 1 to 5 by 3
res19: scala.collection.immutable.Range = Range(1, 4)
scala> 1 until 5
res20: scala.collection.immutable.Range = Range(1, 2, 3, 4)
scala> 1 until 5 by 2
res21: scala.collection.immutable.Range = Range(1, 3)
-----------------------------------------------------
Scala Collections:
-------------------
1. scala.collection.immutable
2. scala.collection.mutable
var arr1 = Array(1,2,3,4,5)
var arr1 = Array[Int](1,2,3,4,5)
var arr2 = Array("anil", "venkat", "raj", "anvith", "rohith")
var arr2 = Array[String]("anil", "venkat", "raj", "anvith", "rohith")
var list1 = List("anil", "venkat", "raj", "anvith", "rohith")
var list1 = List[String]("anil", "venkat", "raj", "anvith", "rohith")
var set1 = Set("anil", "venkat", "raj", "anvith", "rohith")
var set1 = Set[String]("anil", "venkat", "raj", "anvith", "rohith")
var seq1 = Seq("anil", "venkat", "raj", "anvith", "rohith")
var seq1 = Seq[String]("anil", "venkat", "raj", "anvith", "rohith")
var vec1 = Vector("anil", "venkat", "raj", "anvith", "rohith")
var vec1 = Vector[String]("anil", "venkat", "raj", "anvith", "rohith")
val any1 = List(1, 2.5, true, "anil")
val any1 = List[Any](1, 2.5, true, "anil")
-----------------------------------------------------
scala> var arr1 = Array(1,2,3,4,5)
arr1: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr1 = Array[Int](1,2,3,4,5)
arr1: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr2 = Array("anil", "venkat", "raj", "anvith", "rohith")
arr2: Array[String] = Array(anil, venkat, raj, anvith, rohith)
scala> var arr2 = Array[String]("anil", "venkat", "raj", "anvith", "rohith")
arr2: Array[String] = Array(anil, venkat, raj, anvith, rohith)
scala> var list1 = List("anil", "venkat", "raj", "anvith", "rohith")
list1: List[String] = List(anil, venkat, raj, anvith, rohith)
scala> var set1 = Set("anil", "venkat", "raj", "anvith", "rohith")
set1: scala.collection.immutable.Set[String] = Set(raj, anvith, rohith, venkat, anil)
scala> val any1 = List(1, 2.5, true, "anil")
any1: List[Any] = List(1, 2.5, true, anil)
-----------------------------------------------------
scala> list1.foreach(println)
anil
venkat
raj
anvith
rohith
scala> any1.foreach(println)
1
2.5
true
anil
scala> list1.foreach(println)
anil
venkat
raj
anvith
rohith
scala> list1.foreach(x => println(x))
anil
venkat
raj
anvith
rohith
scala> list1.foreach((x : String) => { println(x) })
anil
venkat
raj
anvith
rohith
scala> for(x <- list1) println(x)
anil
venkat
raj
anvith
rohith
scala> for(x <- 1 to 6) println(x)
1
2
3
4
5
6
scala> for(x <- 1 to 6 by 2) println(x)
1
3
5
scala> for(x <- 1 to 6) { if( x % 2 == 0 ) println(x) }
2
4
6
scala> for(x <- 1 to 6 if x % 2 == 0) println(x)
2
4
6
scala> val op = for(x <- 1 to 6 if x % 2 == 0) yield x
op: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6)
scala> op.foreach(println)
2
4
6
-----------------------------------------------------
def factorial( n : Int ) : Int = {
var res = 1
for ( x <- 1 to n ) res = res * x
res
}
factorial(1)
factorial(5)
scala> def factorial( n : Int ) : Int = {
| var res = 1
| for ( x <- 1 to n ) res = res * x
| res
| }
factorial: (n: Int)Int
scala> factorial(1)
res34: Int = 1
scala> factorial(5)
res35: Int = 120
-----------------------------------------------------
curried functions:
------------------------------------------------------
def add( x : Int, y : Int ) : Int = { x + y }
add(1,2)
def add( x : Int )( y : Int ) : Int = { x + y }
add(1)(2)
def addOne(y : Int) = add(1)(y)
def addTwo(y : Int) = add(2)(y)
-----------------------------------------------------
scala> def add( x : Int, y : Int ) : Int = { x + y }
add: (x: Int, y: Int)Int
scala> add(1,2)
res36: Int = 3
scala> def add( x : Int )( y : Int ) : Int = { x + y }
add: (x: Int)(y: Int)Int
scala> add(1)(2)
res37: Int = 3
scala> def addOne(y : Int) = add(1)(y)
addOne: (y: Int)Int
scala> def addTwo(y : Int) = add(2)(y)
addTwo: (y: Int)Int
scala> addOne(5)
res38: Int = 6
scala> addTwo(5)
res39: Int = 7
-----------------------------------------------------
==============================================
-----------------------------------------------------
Functions in Scala:
------------------------------------------------------
1. anonymous functions
2. named functions
3. curried functions
( x : Int ) => { x + 1 }
val add = ( x : Int ) : Int => { x + 1 }
add(1)
add(10)
-----------------------------------------------------
scala> val add = ( x : Int ) => { x + 1 }
add: Int => Int = <function1>
scala> ( x : Int ) => { x + 1 }
res2: Int => Int = <function1>
scala> add(1)
res3: Int = 2
scala> add(10)
res4: Int = 11
-----------------------------------------------------
def add( x : Int ) = { x + 1 }
def add( x : Int ) : Int = { x + 1 }
scala> def add( x : Int ) = { x + 1 }
add: (x: Int)Int
scala> def add( x : Int ) : Int = { x + 1 }
add: (x: Int)Int
scala> add(1)
res5: Int = 2
scala> add(10)
res6: Int = 11
-----------------------------------------------------
val id = 1
if( id == 1) {
println("condtion match")
} else {
println("condtion doesn't match")
}
def validate(id : Int) = {
if( id == 1) {
println("condtion match")
} else {
println("condtion doesn't match")
}
}
validate(id)
scala> def validate(id : Int) = {
| if( id == 1) {
| println("condtion match")
| } else {
| println("condtion doesn't match")
| }
| }
validate: (id: Int)Unit
scala> validate(id)
condtion match
scala> val id = 2
id: Int = 2
scala> validate(id)
condtion doesn't match
-----------------------------------------------------
def factorial( n : Int ) : Int = {
if ( n == 1 ) 1
else n * factorial( n - 1 )
}
scala> def factorial( n : Int ) : Int = {
| if ( n == 1 ) 1
| else n * factorial( n - 1 )
| }
factorial: (n: Int)Int
scala> factorial(1)
res14: Int = 1
scala> factorial(5)
res15: Int = 120
scala> factorial(6)
res16: Int = 720
-----------------------------------------------------
scala> 1 to 5
res17: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)
scala> 1 to 5 by 2
res18: scala.collection.immutable.Range = Range(1, 3, 5)
scala> 1 to 5 by 3
res19: scala.collection.immutable.Range = Range(1, 4)
scala> 1 until 5
res20: scala.collection.immutable.Range = Range(1, 2, 3, 4)
scala> 1 until 5 by 2
res21: scala.collection.immutable.Range = Range(1, 3)
-----------------------------------------------------
Scala Collections:
-------------------
1. scala.collection.immutable
2. scala.collection.mutable
var arr1 = Array(1,2,3,4,5)
var arr1 = Array[Int](1,2,3,4,5)
var arr2 = Array("anil", "venkat", "raj", "anvith", "rohith")
var arr2 = Array[String]("anil", "venkat", "raj", "anvith", "rohith")
var list1 = List("anil", "venkat", "raj", "anvith", "rohith")
var list1 = List[String]("anil", "venkat", "raj", "anvith", "rohith")
var set1 = Set("anil", "venkat", "raj", "anvith", "rohith")
var set1 = Set[String]("anil", "venkat", "raj", "anvith", "rohith")
var seq1 = Seq("anil", "venkat", "raj", "anvith", "rohith")
var seq1 = Seq[String]("anil", "venkat", "raj", "anvith", "rohith")
var vec1 = Vector("anil", "venkat", "raj", "anvith", "rohith")
var vec1 = Vector[String]("anil", "venkat", "raj", "anvith", "rohith")
val any1 = List(1, 2.5, true, "anil")
val any1 = List[Any](1, 2.5, true, "anil")
-----------------------------------------------------
scala> var arr1 = Array(1,2,3,4,5)
arr1: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr1 = Array[Int](1,2,3,4,5)
arr1: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr2 = Array("anil", "venkat", "raj", "anvith", "rohith")
arr2: Array[String] = Array(anil, venkat, raj, anvith, rohith)
scala> var arr2 = Array[String]("anil", "venkat", "raj", "anvith", "rohith")
arr2: Array[String] = Array(anil, venkat, raj, anvith, rohith)
scala> var list1 = List("anil", "venkat", "raj", "anvith", "rohith")
list1: List[String] = List(anil, venkat, raj, anvith, rohith)
scala> var set1 = Set("anil", "venkat", "raj", "anvith", "rohith")
set1: scala.collection.immutable.Set[String] = Set(raj, anvith, rohith, venkat, anil)
scala> val any1 = List(1, 2.5, true, "anil")
any1: List[Any] = List(1, 2.5, true, anil)
-----------------------------------------------------
scala> list1.foreach(println)
anil
venkat
raj
anvith
rohith
scala> any1.foreach(println)
1
2.5
true
anil
scala> list1.foreach(println)
anil
venkat
raj
anvith
rohith
scala> list1.foreach(x => println(x))
anil
venkat
raj
anvith
rohith
scala> list1.foreach((x : String) => { println(x) })
anil
venkat
raj
anvith
rohith
scala> for(x <- list1) println(x)
anil
venkat
raj
anvith
rohith
scala> for(x <- 1 to 6) println(x)
1
2
3
4
5
6
scala> for(x <- 1 to 6 by 2) println(x)
1
3
5
scala> for(x <- 1 to 6) { if( x % 2 == 0 ) println(x) }
2
4
6
scala> for(x <- 1 to 6 if x % 2 == 0) println(x)
2
4
6
scala> val op = for(x <- 1 to 6 if x % 2 == 0) yield x
op: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6)
scala> op.foreach(println)
2
4
6
-----------------------------------------------------
def factorial( n : Int ) : Int = {
var res = 1
for ( x <- 1 to n ) res = res * x
res
}
factorial(1)
factorial(5)
scala> def factorial( n : Int ) : Int = {
| var res = 1
| for ( x <- 1 to n ) res = res * x
| res
| }
factorial: (n: Int)Int
scala> factorial(1)
res34: Int = 1
scala> factorial(5)
res35: Int = 120
-----------------------------------------------------
curried functions:
------------------------------------------------------
def add( x : Int, y : Int ) : Int = { x + y }
add(1,2)
def add( x : Int )( y : Int ) : Int = { x + y }
add(1)(2)
def addOne(y : Int) = add(1)(y)
def addTwo(y : Int) = add(2)(y)
-----------------------------------------------------
scala> def add( x : Int, y : Int ) : Int = { x + y }
add: (x: Int, y: Int)Int
scala> add(1,2)
res36: Int = 3
scala> def add( x : Int )( y : Int ) : Int = { x + y }
add: (x: Int)(y: Int)Int
scala> add(1)(2)
res37: Int = 3
scala> def addOne(y : Int) = add(1)(y)
addOne: (y: Int)Int
scala> def addTwo(y : Int) = add(2)(y)
addTwo: (y: Int)Int
scala> addOne(5)
res38: Int = 6
scala> addTwo(5)
res39: Int = 7
-----------------------------------------------------
Share this article with your friends.
No comments :
Post a Comment