Languages/C/Samples

From UIT
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