Skip to content

Instantly share code, notes, and snippets.

@oxbowlakes
Created February 20, 2012 14:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save oxbowlakes/1869377 to your computer and use it in GitHub Desktop.
Save oxbowlakes/1869377 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))
//Sorting
scala> res1.max
res38: Int = 20
scala> res1.min
res39: Int = 1
scala> res1.sorted
res40: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
scala> res1.sortWith(_ > _)
res42: List[Int] = List(20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
scala> res1.sortBy(_.toString)
res43: List[Int] = List(1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 3, 4, 5, 6, 7, 8, 9)
scala> res1.sorted(implicitly[Ordering[Int]].reverse)
res44: List[Int] = List(20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
//Accumulation and processing
scala> res1.sum
res45: Int = 210
scala> res1.foldLeft("FOO: ") { (s, i) => s + " Z" + i }
res4: java.lang.String = FOO: Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Z19 Z20
scala> ("FOO: " /: res1) { (s, i) => s + " Z" + i }
res47: java.lang.String = FOO: Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Z19 Z20
scala> ("FOO: " /: res1) (_ + " Z" + _)
res48: java.lang.String = FOO: Z1 Z2 Z3 Z4 Z5 Z6 Z7 Z8 Z9 Z10 Z11 Z12 Z13 Z14 Z15 Z16 Z17 Z18 Z19 Z20
scala> res1.reduceLeft(_ * _)
res2: Int = -2102132736
scala> res1.scanLeft(0)(_ + _)
res3: List[Int] = List(0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210)
//Conversions
scala> (_ : Int) % 5
res4: Int => Int = <function1>
scala> res1 map res4
res5: List[Int] = List(1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0)
scala> (res1 map res4).toArray
res6: Array[Int] = Array(1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0)
scala> (res1 map res4).toSet
res7: scala.collection.immutable.Set[Int] = Set(0, 1, 2, 3, 4)
scala> (res1 map res4).toSeq
res8: scala.collection.immutable.Seq[Int] = List(1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0)
scala> (res1 map res4).toIndexedSeq
res9: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0)
scala> (res1 map res4).toList
res10: List[Int] = List(1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0)
scala> (res1 map res4).toStream
res11: scala.collection.immutable.Stream[Int] = Stream(1, ?)
scala> (res1 map res4).iterator
res12: Iterator[Int] = non-empty iterator
scala> (res1 map (i => (i.toString + "Z") -> (i % 5))).toMap
res13: scala.collection.immutable.Map[java.lang.String,Int] = Map(3Z -> 3, 2Z -> 2, 20Z -> 0, 11Z -> 1, 8Z -> 3, 1Z -> 1, 13Z -> 3, 17Z -> 2, 10Z -> 0, 9Z -> 4, 6Z -> 1, 16Z -> 1, 7Z -> 2, 5Z -> 0, 19Z -> 4, 15Z -> 0, 4Z -> 4, 18Z -> 3, 14Z -> 4, 12Z -> 2)
scala> res1.view
res14: java.lang.Object with scala.collection.SeqView[Int,List[Int]] = SeqView(...)
scala> res1.par
res15: scala.collection.parallel.immutable.ParSeq[Int] = ParVector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
scala> res1.seq
res16: scala.collection.immutable.Seq[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment