Marsellus Wallace
Marsellus Wallace

Reputation: 18601

Scalding flatMap tuple containing list

I have the following input tuple that I'd like to flatMap: (String, List[String])

E.G. Input:

("a", ["1", "2"])
("b", ["3", "4"])

Needed output:

("a", "1")
("a", "2")
("b", "3")
("b", "4")

Is there an elegant way to do this in Scalding/Scala?

Upvotes: 1

Views: 351

Answers (2)

Rafael Saraiva
Rafael Saraiva

Reputation: 948

May not be the best solution but cascading maps and then flatting the result was the best i can come up with.

Awsome thing about FP is that you can do the same thing in very very different ways. Hope someone posts a different solution

val input = List( ("a", List("1", "2")), ("b", List("3","4")))

val output = input.map(it => it._2.map { (it._1, _)}).flatten

As indicated the output can change to

input.flatMap(tuple => tuple._2.map { (tuple._1 , _) })

Upvotes: 1

Dan Getz
Dan Getz

Reputation: 9152

You can do this with a multi-line for ... yield expression:

for {
  (a, bs) <- tupleList
  b <- bs
} yield (a, b)

Upvotes: 4

Related Questions