是否存在转换函数的惯用方法
val x: A => (B, C) = ...
到
val y: A => (C, B) = ...
答案 0 :(得分:15)
Tuple2.swap
将(B, C)
更改为(C, B)
。A => (B, C)
将A => (C, B)
转换为.andThen(_.swap)
。例如:
scala> val a = (i: Int) => (s"$i", i.toDouble)
a: Int => (String, Double) = <function1>
scala> val b = a.andThen(_.swap)
b: Int => (Double, String) = <function1>
scala> b(5)
res1: (Double, String) = (5.0,5)