Home > Mobile >  How to map (String, Array[String]) to (String, String)
How to map (String, Array[String]) to (String, String)

Time:11-02

I got a tuple ("banana", ("green", "yellow", brown")), but I want to have ("banana","yellow"), ("banana","green"), ("banana","brown"). How can I do that?

CodePudding user response:

Assuming that the question description is actually accurate.

val srcTup = ("banana", ("green", "yellow", "brown"))

val (resA
    ,resB
    ,resC) = ((srcTup._1, srcTup._2._1)
             ,(srcTup._1, srcTup._2._2)
             ,(srcTup._1, srcTup._2._3))

If, on the other hand, the question title is more correct:

val srcTup = ("banana", Array("green", "yellow", "brown"))

val res: Array[(String,String)] = srcTup._2.map(srcTup._1 -> _) 

CodePudding user response:

You could try something like

source match {
   case (banana:String, fruits:List[String]) => fruits.map( (banana,_) )
}

CodePudding user response:

val (fruit, colors) = ("banana", Array("green", "yellow", "brown"))
colors.map((fruit, _))
  • Related