11

是否可以在 Scala 中并行分配元组成员。如果没有,还有其他技术可以完成类似的事情吗?

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)
4

1 回答 1

20
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. :-)

于 2010-02-05T13:43:41.990 回答