Day 2: Scala and Math
So today I went on with Martin Odersky’s course and just completed week 5.
The functional equivalent of operator overloading is awesome and I believe
it allows more verbose and consistent definition of classes which will behave as expected
if I plug in normal operators. The definition of functions is now more and more
tending towards how we define them in maths : f: [R, R] => R.
I also read about the object oriented implementation of functions and I believe have understood functional programming paradigm to a great degree. Scala’s elegant combination of both fp and oop allows us to implement programs in a better way. Scala’s pattern matching is also good enough to allow us to implement recursive algorithms in a better way.
I have also compiled a program highlighting the collections in scala:
| object collection { | |
| def main(args: Array[String]):Unit = { | |
| // Different types of sequences | |
| // Base class Seq whose base class is Iterable | |
| // List | |
| val l: List[Int] = List(1, 2, 3, 4) | |
| /** Head access faster | |
| * Useful for recursive algorithms | |
| * accessing any element takes time | |
| * maps and filters need to be applied step by step | |
| * Apending an element x to list xs = x :: xs || xs :: x | |
| * Appending two lists xs and ys = xs ++ ys || ys ++ xs | |
| * Immutable | |
| **/ | |
| // Vector | |
| val v: Vector[Int] = Vector(1, 2, 3, 4) | |
| /** Any random element access faster | |
| * maps and filters applied in chunks of 32 which is faster | |
| * Appending an element x to vector xs = x +: xs || xs :+ x | |
| * */ | |
| //Arrays and strings | |
| val a: Array[Int] = Array(1, 2, 3, 4) | |
| val s = "Hello World" | |
| /** Not exactly a subclass of Seq but supports functions | |
| * Normal linear access | |
| **/ | |
| // Range | |
| val r: Range = 1 to 10 by 2 | |
| /** does not support that many Seq functions because its simpler | |
| * */ | |
| // Sequences functions | |
| // drop n | |
| // drops first n elements | |
| a drop 2 // Array(3, 4) | |
| // take n | |
| // takes first n elements | |
| a take 2 // Array(1, 2) | |
| // dropWhile and takeWhile | |
| // takes or drops from beginning as long as a condition is being satisfied | |
| a takeWhile (_ < 3) // Array(1, 2) | |
| // span | |
| // Applies dropWhile and takeWhile simultaneously | |
| a span (_ < 3) // a pair (Array[Int], Array[Int]) = (Array(1, 2), Array(3, 4)) | |
| // Map | |
| a map (x => x * x) // Square all elements of a | |
| v map (_ * 2) // Double all elements of v and so on | |
| // Filter | |
| s filter (_.isUpper) // Return only uppercase | |
| a filter (_ < 3) // Return an array of all less than 3 | |
| // Fold left | |
| // function passed is of the form f: (T,T) => T | |
| (l foldLeft 0) (_ + _) // Sum of all elements | |
| (l foldLeft 1) (_ * _) // Product of all elements | |
| // exists(p) p is of the form T => Boolean | |
| a exists (_ > 4) // False | |
| s exists (_.isUpper) // True | |
| // forall p | |
| a forall (_ < 5) // True | |
| s forall (_.isLower) // False | |
| // zip | |
| l zip s // Output is List[(Int, Char)] = List((1, 'H'),...,(4, 'l')) | |
| // unzip is reverse of that, returns pair of lists | |
| // sum | |
| a.sum // 10 | |
| // max | |
| a.max // 4 | |
| // prime check | |
| def isPrime(n: Int) = (2 until Math.sqrt(n).asInstanceOf[Int]) forall (n % _ != 0) | |
| } | |
| } |