Permalink
Browse files

scala collections vocabulary

  • Loading branch information...
1 parent 092da8d commit f51619ea5b843baff905379b788fcb40a637c5d2 @Srirangan committed Jan 24, 2013
Showing with 163 additions and 0 deletions.
  1. +6 −0 app/views/index.scala.html
  2. +157 −0 public/posts/2013-01-scala-collections-vocabulary.html
@@ -3,6 +3,12 @@
<div class='span9'>
<ul class="unstyled">
<li>
+ <a href="/2013-01-scala-collections-vocabulary">
+ Scala Collections Vocabulary
+ </a>
+ <small>2013-01-24</small>
+ </li>
+ <li>
<a href="/2013-01-controlling-flow-with-scala-futures">
Controlling flow with Scala Futures
</a>
@@ -0,0 +1,157 @@
+<h2>Scala Collections Vocabulary</h2>
+
+<div class="entry-content">
+ <p>Scala provides collections with powerful methods to perform simple and complex operations.</p>
+ <p>It's a jargon that may already be familiar.</p>
+ <p>Operations on the left, results on the right:</p>
+ <div class="row">
+ <span style="width:10px" class="span1"></span>
+ <pre style="width: 270px;" class="span1">
+(1 to 10)
+
+// Multiply each by 3
+(1 to 10).map(_ * 3)
+
+// flatMap flattens the result
+(1 to 10).map(n => List(n, n * 2))
+
+
+(1 to 10).flatMap(n => List(n, n * 2))
+
+// Filter out odds
+(1 to 10).filter(_ % 2 == 0)
+
+// Collect multiples of 3 and 4
+(1 to 10).collect({
+ case n if n % 3 == 0 => n
+ case n if n % 5 == 0 => n
+})
+
+// Add two collections
+(1 to 5) ++ (6 to 10)
+
+// Size
+List().isEmpty
+(1 to 3).nonEmpty
+List(1, 2, 3, 4, 5).size
+List().hasDefiniteSize
+
+// Element retrieval
+(1 to 10).head
+(1 to 10).tail
+(1 to 10).headOption
+(1 to 10).lastOption
+(1 to 25).find(_ == 12)
+
+//Sub-collection retrieval
+(1 to 10).tail
+(1 to 10).init
+(1 to 10).slice(3, 7)
+(1 to 10).take(5)
+(1 to 10).drop(5)
+(1 to 10).filter(_ % 2 == 0)
+(1 to 10).filterNot(_ % 2 == 0)
+(1 to 10).withFilter(_ % 2 == 0)
+
+
+// Sub-division
+(1 to 10).splitAt(7)
+
+(1 to 10).span(_ % 2 == 0)
+
+(1 to 10).partition(_ > 7)
+
+
+(1 to 10).groupBy[String](n => n match {
+ case n if n % 2 == 0 => "even"
+ case _ => "odd"
+})
+
+
+// Element tests
+(1 to 25).exists(_ == 24)
+(1 to 25).forall(_ > -1)
+(1 to 25).count(_ > 20)
+
+// Simple reductions
+(1 to 10).sum
+(1 to 5).product
+List(5, 3, 1, 7).max
+List(5, 3, 1, 7).min
+
+
+ </pre>
+ <pre style="width: 540px;" class="span1">
+> res0: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8,
+ 9, 10)
+
+> res1: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 6, 9, 12, 15, 18, 2
+ 1, 24, 27, 30)
+
+> res2: scala.collection.immutable.IndexedSeq[List[Int]] = Vector(List(1, 2), List
+ (2, 4), List(3, 6), List(4, 8), List(5, 10), List(6, 12), List(7, 14), List(8,
+ 16), List(9, 18), List(10, 20))
+> res3: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 2, 4, 3, 6, 4, 8
+ , 5, 10, 6, 12, 7, 14, 8, 16, 9, 18, 10, 20)
+
+> res4: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
+
+
+
+
+> res5: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 6, 9, 10)
+
+
+> res6: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8
+ , 9, 10)
+
+
+> res7: Boolean = true
+> res8: Boolean = true
+> res9: Int = 5
+> res10: Boolean = true
+
+> res11: Int = 1
+> res12: scala.collection.immutable.Range = Range(2, 3, 4, 5, 6, 7, 8, 9, 10)
+> res13: Option[Int] = Some(1)
+> res14: Option[Int] = Some(10)
+> res15: Option[Int] = Some(12)
+
+> res16: scala.collection.immutable.Range = Range(2, 3, 4, 5, 6, 7, 8, 9, 10)
+> res17: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9)
+> res18: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 5, 6, 7)
+> res19: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5)
+> res20: scala.collection.immutable.Range = Range(6, 7, 8, 9, 10)
+> res21: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)
+> res22: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 3, 5, 7, 9)
+> res23: scala.collection.generic.FilterMonadic[Int,scala.collection.immutable.Ind
+ exedSeq[Int]] = scala.collection.TraversableLike$WithFilter@385533b0
+
+
+> res24: (scala.collection.immutable.Range, scala.collection.immutable.Range) = (R
+ ange(1, 2, 3, 4, 5, 6, 7),Range(8, 9, 10))
+> res25: (scala.collection.immutable.Range, scala.collection.immutable.Range) = (R
+ ange(),Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
+> res26: (scala.collection.immutable.IndexedSeq[Int], scala.collection.immutable.I
+ ndexedSeq[Int]) = (Vector(8, 9, 10),Vector(1, 2, 3, 4, 5, 6, 7))
+
+
+
+
+
+> res27: scala.collection.immutable.Map[String,scala.collection.immutable.IndexedS
+ eq[Int]] = Map(odd -> Vector(1, 3, 5, 7, 9), even -> Vector(2, 4, 6, 8, 10))
+
+
+> res28: Boolean = true
+> res29: Boolean = true
+> res30: Int = 5
+
+> res31: Int = 55
+> res32: Int = 120
+> res33: Int = 7
+> res34: Int = 1
+
+ </pre>
+ </div>
+</div>

0 comments on commit f51619e

Please sign in to comment.