A => (B, C) 到 A => (C, B) 的惯用函数转换

Idiomatic Function Transformation of A => (B, C) to A => (C, B)

是否有转换函数的惯用方法

val x: A => (B, C) = ...

val y: A => (C, B) = ...
  • 您可以使用 Tuple2.swap(B, C) 更改为 (C, B)
  • 然后可以使用 .andThen(_.swap)A => (B, C) 变成 A => (C, B)

例如:

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)