scala> // /: or foldLeft() #361, 363 scala> def sum(xs: List[Int]): Int = { | (7 /: xs) (_ + _) | } sum: (xs: List[Int])Int scala> scala> val data = List(1, 2, 3) data: List[Int] = List(1, 2, 3) scala> sum(data) res39: Int = 13 scala> scala> val data = List(1, 2, 3) data: List[Int] = List(1, 2, 3) scala> data.foldLeft(7)(_ + _) res40: Int = 13 scala> scala> // :\ or foldRight() #361, 363 scala> def sum(xs: List[Int]): Int = { | (xs :\ 7) (_ + _) | } sum: (xs: List[Int])Int scala> scala> val data = List(1, 2, 3) data: List[Int] = List(1, 2, 3) scala> sum(data) res41: Int = 13 scala> scala> val data = List(1, 2, 3) data: List[Int] = List(1, 2, 3) scala> data.foldRight(7)(_ + _) res42: Int = 13 scala> scala> scala> // Note/Beware : foldLeft() & foldRight() has difference scala> // in terms of efficiency #364 scala> // Best Practice : Is intuitive to use foldLeft() instead of scala> // Symbol /:
Showing posts with label zzzzz| others. Show all posts
Showing posts with label zzzzz| others. Show all posts
Thursday, October 13, 2016
Folding lists : foldLeft() & foldRight()
Thursday, October 6, 2016
List Methods : :::, length, init & last : Example
// First order Methods do not take function as arguments #345 scala> /////////////////////////////////////////////////////////// scala> // Concatenating Multiple List using ::: #345 scala> /////////////////////////////////////////////////////////// scala> val l1 = List(1, 2, 3) l1: List[Int] = List(1, 2, 3) scala> val l2 = List(4, 5) l2: List[Int] = List(4, 5) scala> val l3 = l1 ::: l2 l3: List[Int] = List(1, 2, 3, 4, 5) scala> scala> /////////////////////////////////////////////////////////// scala> // length scala> // Is an expensive operation scala> // ***Need to traverse the whole list to find the scala> // length #347 scala> // Use xs.isEmpty if possible scala> /////////////////////////////////////////////////////////// scala> val l1 = List(1, 2, 3) l1: List[Int] = List(1, 2, 3) scala> l1.length res1: Int = 3 scala> scala> /////////////////////////////////////////////////////////// scala> // init & last scala> // *** Need to traverse the whole list to get scala> // init & last scala> // Best Practice : Organize the list to use scala> // head & tail instead scala> /////////////////////////////////////////////////////////// scala> val l1 = List(1, 2, 3, 4, 5, 6) l1: List[Int] = List(1, 2, 3, 4, 5, 6) scala> val l2 = l1.init l2: List[Int] = List(1, 2, 3, 4, 5) scala> val l3 = l1.last l3: Int = 6
Sunday, September 18, 2016
Nested / Local Function : Example...
scala> // Nested / Local Function #182 scala> // Helps to avoid namespace pollution scala> // Recommended : Helper functions can be created as a scala> // Nested function scala> def getMarks(subList: List[String]): List[Int] = { | | def getMark(subject: String):Int = { | subject match { | case "history" => 75 | case "math" => 80 | case _ => 70 | } | } | | for (sub <- subList) yield { | //Note : It is also possible to access the Function | //Parameter | println("Checking subject List " + subList.length) | getMark(sub) | } | } getMarks: (subList: List[String])List[Int] scala> scala> val subList = List("history", "math", "science") subList: List[String] = List(history, math, science) scala> getMarks(subList) Checking subject List 3 Checking subject List 3 Checking subject List 3 res82: List[Int] = List(75, 80, 70)
Friday, September 16, 2016
do/while Loop : Example
scala> //do/while loop is not an Expression #157 scala> //so..they will always have Side effects scala> //Note : An alternative for do/while might scala> //be recursive functions #159 scala> var n1 = 5 n1: Int = 5 scala> do { | n1 = n1 + 2 | println("n1 -> ", n1) | }while(n1 < 15) (n1 -> ,7) (n1 -> ,9) (n1 -> ,11) (n1 -> ,13) (n1 -> ,15)
Subscribe to:
Posts (Atom)