Last active
May 5, 2016 16:17
-
-
Save yashsriv/1ed9dad1b25ac606036de7c324bf61ea to your computer and use it in GitHub Desktop.
Scala Collections framework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.toIntmight be more scala-ish than.asInstanceOf[Int].