Collections: List and Arrays


object CollectionsDay2 {
  println("Welcome to the Scala worksheet")       //> Welcome to the Scala worksheet
  
  val l = List(1,2,3,4,5,6,7,8,9,10)              //> l  : List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 
 	def fn[T](u: T) = u                       //> fn: [T](u: T)T
 	
 	fn(l)                                     //> res0: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 	
 	val a = Array(1,2,3,4,5,6,7,8,9,10)       //> a  : Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 	
 	l(3)                                      //> res1: Int = 4
 	a(3)                                      //> res2: Int = 4
 	
 	a(3) = 15
 	a                                         //> res3: Array[Int] = Array(1, 2, 3, 15, 5, 6, 7, 8, 9, 10)
 	
 	//l(3) = 15
 	
 	val s = "Vijay"                           //> s  : String = Vijay
 	val s1 = s.replace('V','A')               //> s1  : String = Aijay
 	s                                         //> res4: String = Vijay
 	
 	val l1 = l.map(n => n*n)                  //> l1  : List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
 	l                                         //> res5: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 	l1                                        //> res6: List[Int] = List(1, 4, 9, 16, 25, 36, 49, 64, 81, 100)
 	
 	val a1 = a.map(n => n.toDouble/10.0)      //> a1  : Array[Double] = Array(0.1, 0.2, 0.3, 1.5, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0
                                                  //| )
 	a                                         //> res7: Array[Int] = Array(1, 2, 3, 15, 5, 6, 7, 8, 9, 10)
 	val a2 = a.filter(x => x % 2 == 0)        //> a2  : Array[Int] = Array(2, 6, 8, 10)
 	
 	val l2 = l.partition(_%2 == 0)            //> l2  : (List[Int], List[Int]) = (List(2, 4, 6, 8, 10),List(1, 3, 5, 7, 9))
 	
 	val i = ("Vijay",38)                      //> i  : (String, Int) = (Vijay,38)
 	i._1                                      //> res8: String = Vijay
 	i._2                                      //> res9: Int = 38
 
  val (name,age) = i                              //> name  : String = Vijay
                                                  //| age  : Int = 38
  
  l.reduce(_+_)                                   //> res10: Int = 55