Passing a function foreach key of an Array

127 views Asked by At

I have an array like that :

val pairs: Array[(Int, ((VertexId, Seq[Int]), Int))] 

which generates this output :

(11,((11,ArraySeq(2, 5, 4, 5)),1))
(11,((12,ArraySeq(7, 7, 8, 2)),1))
(11,((13,ArraySeq(5, 9, 8, 7)),1))
(1,((1,ArraySeq(1, 2, 3, 4)),1))
(1,((4,ArraySeq(1, 5, 1, 1)),1))

I want to build a Graph for each pairs._1. That means for example those who have the same id ( pairs._1 ) will construct a Graph together. I am thinking about passing a function of Graph Construction to every id. How can I do that ?

1

There are 1 answers

6
dcastro On BEST ANSWER

You're looking for the groupBy function followed by mapValues to process each group.

pairs groupBy {_._1} mapValues { groupOfPairs => doSomething(groupOfPairs) }