Monday, 19 September 2016

SCALA BASICS DAY 2 Practice on 20 Sept 2016






















--------------------------------------------------------

scala> val list1 = List(1,2,3)
list1: List[Int] = List(1, 2, 3)

scala> val list2 = List("aa","bb","cc")
list2: List[String] = List(aa, bb, cc)

scala> val arr1 = Array(1,2,3)
arr1: Array[Int] = Array(1, 2, 3)

scala> val arr2 = Array("aa","bb","cc")
arr2: Array[String] = Array(aa, bb, cc)

scala> val seq1 = Seq(1,2,3)
seq1: Seq[Int] = List(1, 2, 3)

scala> val set1 = Set(1,2,3)
set1: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> val vec1 = Vector(1,2,3)
vec1: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

--------------------------------------------------------

scala> val map1 = Map( 1 -> "aa", 2 -> "bb", 3 -> "cc")
map1: scala.collection.immutable.Map[Int,String] = Map(1 -> aa, 2 -> bb, 3 -> cc)

scala> list1
res0: List[Int] = List(1, 2, 3)

scala> list2
res1: List[String] = List(aa, bb, cc)

scala> val map2 = list1.zip(list2)
map2: List[(Int, String)] = List((1,aa), (2,bb), (3,cc))

scala> val map12 = map2.toMap
map12: scala.collection.immutable.Map[Int,String] = Map(1 -> aa, 2 -> bb, 3 -> cc)

--------------------------------------------------------

scala> val list1 = List(1,2,3)
list1: List[Int] = List(1, 2, 3)

scala> val list2 = 1 :: 2 :: 3 :: Nil
list2: List[Int] = List(1, 2, 3)

scala> val list3 = (1 :: (2 :: (3 :: Nil)))
list3: List[Int] = List(1, 2, 3)

scala> val list4 = 0 :: list3
list4: List[Int] = List(0, 1, 2, 3)

scala> val list5 = list4 :: 4
<console>:12: error: value :: is not a member of Int
       val list5 = list4 :: 4
                         ^

scala> val list5 = list4 :: 4 :: Nil
list5: List[Any] = List(List(0, 1, 2, 3), 4)


scala> val list5 = list4 ::: 4 :: Nil
list5: List[Int] = List(0, 1, 2, 3, 4)

--------------------------------------------------------

scala> list1
res2: List[Int] = List(1, 2, 3)

scala> val list2 = list1.map((x : Int) => {x + 1})
list2: List[Int] = List(2, 3, 4)

scala> val list2 = list1.map((x : Int) => x + 1)
list2: List[Int] = List(2, 3, 4)

scala> val list2 = list1.map(x => x + 1)
list2: List[Int] = List(2, 3, 4)

scala> val list2 = list1.map( _ + 1)
list2: List[Int] = List(2, 3, 4)


scala> list1
res3: List[Int] = List(1, 2, 3)

scala> val list2 = list1.filter(x => x > 1)
list2: List[Int] = List(2, 3)

scala> val list2 = list1.filter(_ > 1)
list2: List[Int] = List(2, 3)



scala> val list1 = List(List(1,2,3), List(4,5,6))
list1: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))

scala> val list2 = list1.flatMap(x => x)
list2: List[Int] = List(1, 2, 3, 4, 5, 6)


scala> val list1 = List(List("I am going", "to hyd"), List("I am learning", "hadoop course"))
list1: List[List[String]] = List(List(I am going, to hyd), List(I am learning, hadoop course))

scala> val list2 = list1.flatMap(x => x)
list2: List[String] = List(I am going, to hyd, I am learning, hadoop course)

scala> val list3 = list2.flatMap(x => x)
list3: List[Char] = List(I,  , a, m,  , g, o, i, n, g, t, o,  , h, y, d, I,  , a, m,  , l, e, a, r, n, i, n, g, h, a, d, o, o, p,  , c, o, u, r, s, e)


scala> val list3 = list2.flatMap(x => x.split(""))
list3: List[String] = List(I, " ", a, m, " ", g, o, i, n, g, t, o, " ", h, y, d, I, " ", a, m, " ", l, e, a, r, n, i, n, g, h, a, d, o, o, p, " ", c, o, u, r, s, e)

scala> val list3 = list2.flatMap(x => x.split(" "))
list3: List[String] = List(I, am, going, to, hyd, I, am, learning, hadoop, course)


scala> val list3 = list2.flatMap(x => x.split(" "))
list3: List[String] = List(I, am, going, to, hyd, I, am, learning, hadoop, course)

scala> val map = list3.map(x => (x , 1))
map: List[(String, Int)] = List((I,1), (am,1), (going,1), (to,1), (hyd,1), (I,1), (am,1), (learning,1), (hadoop,1), (course,1))


scala> list3.count(x => true)
res10: Int = 10

scala> list3.count(x => x.length > 2)
res11: Int = 5


scala> val group = list3.groupBy(x => x)
group: scala.collection.immutable.Map[String,List[String]] = Map(course -> List(course), am -> List(am, am), going -> List(going), I -> List(I, I), hyd -> List(hyd), to -> List(to), hadoop -> List(hadoop), learning -> List(learning))



scala> val wordcount = group.map(x => (x._1, x._2))
wordcount: scala.collection.immutable.Map[String,List[String]] = Map(course -> List(course), am -> List(am, am), going -> List(going), I -> List(I, I), hyd -> List(hyd), to -> List(to), hadoop -> List(hadoop), learning -> List(learning))

scala> val wordcount = group.map(x => (x._1, x._2.count(y => true)))
wordcount: scala.collection.immutable.Map[String,Int] = Map(course -> 1, am -> 2, going -> 1, I -> 2, hyd -> 1, to -> 1, hadoop -> 1, learning -> 1)


scala> wordcount.foreach(x => println(x))
(course,1)
(am,2)
(going,1)
(I,2)
(hyd,1)
(to,1)
(hadoop,1)
(learning,1)


scala> wordcount.foreach(println)
(course,1)
(am,2)
(going,1)
(I,2)
(hyd,1)
(to,1)
(hadoop,1)
(learning,1)



scala> wordcount.toList.sorted.foreach(println))
(I,2)
(am,2)
(course,1)
(going,1)
(hadoop,1)
(hyd,1)
(learning,1)
(to,1)



scala> wordcount.toList.sortBy(x => x._1).foreach(println)
(I,2)
(am,2)
(course,1)
(going,1)
(hadoop,1)
(hyd,1)
(learning,1)
(to,1)


scala> wordcount.toList.sortBy(x => x._2).foreach(println)
(course,1)
(going,1)
(hyd,1)
(to,1)
(hadoop,1)
(learning,1)
(am,2)
(I,2)




scala> wordcount.toList.sortWith((x,y) => x._1 > y._1).foreach(println)
(to,1)
(learning,1)
(hyd,1)
(hadoop,1)
(going,1)
(course,1)
(am,2)
(I,2)


scala> wordcount.toList.sortWith((x,y) => x._1 < y._1).foreach(println)
(I,2)
(am,2)
(course,1)
(going,1)
(hadoop,1)
(hyd,1)
(learning,1)
(to,1)


scala> wordcount.toList.sortWith((x,y) => x._2 < y._2).foreach(println)
(course,1)
(going,1)
(hyd,1)
(to,1)
(hadoop,1)
(learning,1)
(am,2)
(I,2)


scala> wordcount.toList.sortWith((x,y) => x._2 > y._2).foreach(println)
(am,2)
(I,2)
(course,1)
(going,1)
(hyd,1)
(to,1)
(hadoop,1)
(learning,1)

Share this article with your friends.

1 comment :

Related Posts Plugin for WordPress, Blogger...