Question

I am learning Scala. I am running an example that I want to do.

I want to do a collection of tuples of 3 members.

Let's say that I have a list.

val test= List("How","long","are","we","dog","dog")

The desired result: (Word,Frequency,Position)

Map(("How",1,1), ("long",1,2), ("are",1,3), ("we",1,4) ("dog",2,5))

I couldn't find a way to do this :

   test.collect{ case (key, v, k ) => (key,indexOf(k),freq)}

No correct solution

OTHER TIPS

Not sure what you mean by position, but here's something that might help you:

scala> val test = List("How","long","are","we","dog","dog")
test: List[String] = List(How, long, are, we, dog, dog)

scala> test.groupBy(identity).mapValues(_.size).toSeq.sortBy(_._2)
res0: Seq[(String, Int)] = ArrayBuffer((are,1), (How,1), (long,1), (we,1), (dog,2))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top