elm*_*elm 3 scala tuples scala-collections
在一次调用中map我们构造一个元组集合,例如像这样,
val a = (1 to 5).map { x => (x, x*10) }
a: Vector((1,10), (2,20), (3,30), (4,40), (5,50))
Run Code Online (Sandbox Code Playgroud)
然后我们将第一个和第二个元素提取到两个独立的,不可变的集合中
val b1 = a.map {_._1}
b1: Vector(1, 2, 3, 4, 5)
val b2 = a.map {_._2}
b2: Vector(10, 20, 30, 40, 50)
Run Code Online (Sandbox Code Playgroud)
如何获取b1并b2通过迭代初始集合一次,
val (b1,b2) = (1 to 5).map { x => /* ??? */ }
Run Code Online (Sandbox Code Playgroud)
Ion*_*tan 11
用途unzip:
scala> (1 to 5).unzip { x => x -> x * 3 }
res0: (Vector(1, 2, 3, 4, 5),Vector(3, 6, 9, 12, 15))
Run Code Online (Sandbox Code Playgroud)