views:

233

answers:

1

Is it possible to assign tuple members in parallel in Scala. if not is there another technique to accomplish something similar?

val players = List(
    new Player("Django Reinhardt", 42), 
    new Player("Sol Hoopii", 57),
    new Player("Marc Ribot", 64)
)

val winners, losers = players.partition(p => p.score > 50)

// winners = List(Player name:Sol Hoopii score: 57, Player name:Marc Ribot score: 64)
// losers = List(Player name:Django Reinhardt score: 42)
+12  A: 
val winners, losers = players.partition(p => p.score > 50)

Assignes the (List, List) tuple to two variables. If you want to unpack the tuple you have to use

val (winners, losers) = players.partition(p => p.score > 50)

Which does exactly what you want. :-)

Malax
Excellent thanks Malax ;)
Brian Heylin
+1 for pointing out what the syntax without parenthesis means. Or, rather, if I _could_ award one additional vote, I would. :-)
Daniel
I suggest, though, that you also point out the tuple assignment works because it is actually a pattern matching.
Daniel
I would be certainly be interested in the pattern matching example
Brian Heylin