Skip to content

Instantly share code, notes, and snippets.

@mbektimirov
Forked from oxbowlakes/gist:1869377
Created May 11, 2012 15:47
Show Gist options
  • Save mbektimirov/2660553 to your computer and use it in GitHub Desktop.
Save mbektimirov/2660553 to your computer and use it in GitHub Desktop.
Simple scala collection examples
scala> (1 to 20).toList
res1: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
//Simple side effects
scala> res1 take 3 foreach (i => println(i))
1
2
3
scala> res1 take 3 foreach println
1
2
3
//String representation
scala> (res1 take 3).toString
res9: String = List(1, 2, 3)
scala> (res1 take 3).mkString
res10: String = 123
scala> (res1 take 3).mkString(", ")
res11: String = 1, 2, 3
scala> (res1 take 3).mkString("[", ", ", "]")
res12: String = [1, 2, 3]
//Searching
scala> res1 find (_ % 7 == 3)
res13: Option[Int] = Some(3)
scala> res1 find (_ > 20)
res14: Option[Int] = None
//Quantification
scala> res1 exists (_ > 20)
res15: Boolean = false
scala> res1.forall(_ < 25)
res16: Boolean = true
//2 separate syntaxes for filtering
scala> res1 filter (_ % 2 == 0)
res2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
scala> res1 filter (i => i % 2 == 0)
res3: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
//Filtering and mapping 2 ways
scala> res1 filter (_ % 2 == 0) map (_.toString + "Z")
res5: List[java.lang.String] = List(2Z, 4Z, 6Z, 8Z, 10Z, 12Z, 14Z, 16Z, 18Z, 20Z)
scala> res1 collect { case i if i % 2 == 0 => i.toString + "Z" }
res6: List[java.lang.String] = List(2Z, 4Z, 6Z, 8Z, 10Z, 12Z, 14Z, 16Z, 18Z, 20Z)
//Flattening and mapping. Um, flatMap!
scala> res1 take 5 map (Some(_))
res19: List[Some[Int]] = List(Some(1), Some(2), Some(3), Some(4), Some(5))
scala> res19.flatten
res20: List[Int] = List(1, 2, 3, 4, 5)
scala> res1 take 4 flatMap (i => Some(i + "Z"))
res21: List[String] = List(1Z, 2Z, 3Z, 4Z)
//Organizing: spans and partitions
scala> res1 takeWhile (_ % 5 != 0)
res28: List[Int] = List(1, 2, 3, 4)
scala> res1 dropWhile (_ % 5 != 0)
res29: List[Int] = List(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
scala> res1 span (_ % 5 != 0)
res30: (List[Int], List[Int]) = (List(1, 2, 3, 4),List(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))
scala> res1 partition (_ % 5 != 0)
res31: (List[Int], List[Int]) = (List(1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19),List(5, 10, 15, 20))
//Zipping
scala> res1 take 5 zipWithIndex
res32: List[(Int, Int)] = List((1,0), (2,1), (3,2), (4,3), (5,4))
scala> res32.unzip
res0: (List[Int], List[Int]) = (List(1, 2, 3, 4, 5),List(0, 1, 2, 3, 4))
//Grouping/Chunking
scala> res1 groupBy (_ % 5)
res35: scala.collection.immutable.Map[Int,List[Int]] = Map(0 -> List(5, 10, 15, 20), 1 -> List(1, 6, 11, 16), 2 -> List(2, 7, 12, 17), 3 -> List(3, 8, 13, 18), 4 -> List(4, 9, 14, 19))
scala> res1 sliding 3
res33: Iterator[List[Int]] = non-empty iterator
scala> res33 toList
res34: List[List[Int]] = List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6), List(5, 6, 7), List(6, 7, 8), List(7, 8, 9), List(8, 9, 10), List(9, 10, 11), List(10, 11, 12), List(11, 12, 13), List(12, 13, 14), List(13, 14, 15), List(14, 15, 16), List(15, 16, 17), List(16, 17, 18), List(17, 18, 19), List(18, 19, 20))
scala> res1 grouped 4
res36: Iterator[List[Int]] = non-empty iterator
scala> res36.toList
res37: List[List[Int]] = List(List(1, 2, 3, 4), List(5, 6, 7, 8), List(9, 10, 11, 12), List(13, 14, 15, 16), List(17, 18, 19, 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment