Languages/C/Samples

From UIT
(Difference between revisions)
Jump to: navigation, search
Pim (Talk | contribs)
(Created page with "{{public}} {{TOC right}} == Counting matching elements in collection == <source lang="scala"> object test { val x = Seq("foo", "bar", "bar", "baz", "foo") //> x : Seq...")

Latest revision as of 09:56, 18 August 2014

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