Languages/C/Samples

From UIT
Revision as of 09:56, 18 August 2014 by Pim (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Counting matching elements in collection

object test {
  val x = Seq("foo", "bar", "bar", "baz", "foo")     //> x  : Seq[String] = List(foo, bar, bar, baz, foo)
 
  x.groupBy(identity).map(x => (x._1, x._2.length))  //> res0: scala.collection.immutable.Map[String,Int] = 
                                                     //| Map(baz -> 1, foo -> 2, bar -> 2)
}

groupBy(identity) is a synonym of groupBy(x => x)

Personal tools
Namespaces
Variants
Actions
Navigation
Browse
Toolbox