【发布时间】:2014-02-14 18:35:37
【问题描述】:
我想解压map 中的元组以访问其中的一些值。
在 Python 中我会这样做:
>>> [topic for topic, partition in [('x', 1), ('y', 2)]]
['x', 'y']
这里元组('x', 1), ('y', 2)被解包到topic, partition,然后我访问topic。
在 Scala 中,我只是设法做到了这一点:
List(('x', 1), ('y', 2)).map(_._1)
但我希望将显式解包到元组中作为一个中间步骤(即使它引入了一些开销),因为我认为它使代码更具可读性。
我想实现类似的目标
List(('x', 1), ('y', 2)).map((topic, partition) => topic)
不幸的是它没有工作:
> <console>:9: error: wrong number of parameters; expected = 1
List(('x', 1), ('y', 2)).map((topic, partition) => topic)
其他尝试(例如List(('x', 1), ('y', 2)).map(Tuple2(topic, partition) => topic))也失败了。
有什么方法可以实现类似于我在 Python 中的功能吗?
【问题讨论】:
标签: scala