开发者

Scala: Parallel assignment of Tuples

开发者 https://www.devze.com 2022-12-19 01:34 出处:网络
Is it possible to assign tuple members in parallel in Scala. if not is there another technique to accomplish something similar?

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 H开发者_Python百科oopii", 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)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消