是否有一种通用的方法来反转curried函数的参数顺序?

时间:2013-06-12 14:44:53

标签: scala

我想知道你是否可以编写一个带有curried函数然后反转参数的泛型函数,如下所示:

def foo(a: String)(b: Boolean)(c: Int): String
val bar = invert(foo _)
foo("baz")(false)(12) must be equalTo(bar(12)(false)("baz"))

只要为要解决的特定情况添加隐式逆变器,以下操作就会起作用。但是我对更一般的案例更感兴趣 - 也就是说,处理任何数量的讨论论点的情况。

trait Inverter[V, W] {
  def invert(v: V): W
}

implicit def function2Inverter[X, Y, Z] = 
  new Inverter[(X, Y) => Z, (Y, X) => Z] {
    def invert(v: (X, Y) => Z) = {
      def inverted(y: Y, x: X) = v(x, y)
      inverted _
    }
  }

implicit def curried2Inverter[X, Y, Z] = 
  new Inverter[X => Y => Z, Y => X => Z] {
    def invert(v: (X) => (Y) => Z) = {
      def inverted(y: Y)(x: X) = v(x)(y)
      inverted _
    }
  }

def invert[V, W](v: V)(implicit inverter: Inverter[V, W]): W = 
  inverter.invert(v)

哦,我希望有一个适用于Scala 2.9的解决方案。

2 个答案:

答案 0 :(得分:5)

TL; DR:使用this gist。解释如下:

首先,定义一个类型类(和个案)以部分应用具有最后一个参数的函数:

trait PopLast[A, Last, Rem] {
  def pop(f: A, v: Last): Rem
}

trait LowPrioPopLast {
  implicit def popEnd[A,B] = new PopLast[A => B, A, B] {
    def pop(f: A => B, v: A) = f(v)
  }  
}

object PopLast extends LowPrioPopLast {
  implicit def popOne[A, B, C, Last, IRem](
    implicit iPop: PopLast[B => C, Last, IRem]) = 
      new PopLast[A => B => C, Last, A => IRem] {
        def pop(f: A => B => C, v: Last) = { a: A => iPop.pop(f(a), v) }
      }
}

然后,创建逆变器类型类:递归地执行最后一个参数的部分应用并反转结果。

trait Inverter[A] {
  type Out
  def invert(f: A): Out 
}

trait LowPrioInverter {
  implicit def invertEnd[A,B] = new Inverter[A => B] {
    type Out = A => B
    def invert(f: A => B) = f
  }
}

object Inverter extends LowPrioInverter {
  implicit def invertStep[A, Last, Rem](implicit pop: PopLast[A, Last, Rem],
    inv: Inverter[Rem]) = new Inverter[A] {
      type Out = Last => inv.Out
      def invert(f: A) = { a: Last => inv.invert(pop.pop(f, a)) }
  }
}

最后,封装成一个函数:

def invert[A](f: A)(implicit inv: Inverter[A]) = inv.invert(f)

我们走了:

def foo(a: String)(b: Boolean)(c: Int): String = "bar"
val bar = invert(foo _)
// bar: Int => Boolean => String => String

bar(1)(true)("foo")

答案 1 :(得分:2)

1)从您的函数中将类型信息提取到HList(来自shapelessTs并输入参数Res

Int => String => Long => String TsInt :: String :: Long :: HNilResString

import shapeless.{HList, HNil, ::}

trait TypeExtractorAux[A, R, Ts <: HList, Res]

trait LowPriorityTypeExtractorAux{
  implicit def apply1[A, R]: TypeExtractorAux[A, R, A :: HNil, R] = new TypeExtractorAux[A, R, A :: HNil, R]{}
}

object TypeExtractorAux extends LowPriorityTypeExtractorAux{
  implicit def applyN[A1, A2, R, TsNext <: HList, Res](implicit te: TypeExtractorAux[A2, R, TsNext, Res]): TypeExtractorAux[A1, A2 => R, A1 :: TsNext, Res] =
    new TypeExtractorAux[A1, A2 => R, A1 :: TsNext, Res]{}
}

2)将curried函数转换为Ts => Res

trait FunctionConverterAux[Ts <: HList, Res, A, R]{
  def apply(f: A => R): Ts => Res
}

object FunctionConverterAux{
  implicit def apply1[L, Res]: FunctionConverterAux[L :: HNil, Res, L, Res] =
    new FunctionConverterAux[L :: HNil, Res, L, Res]{
      def apply(f: L => Res): L :: HNil => Res = hl => f(hl.head)
    }

  implicit def applyN[L1, L2, Rt <: HList, Res, R](implicit fc: FunctionConverterAux[L2 :: Rt, Res, L2, R]): FunctionConverterAux[L1 :: L2 :: Rt, Res, L1, L2 => R] =
    new FunctionConverterAux[L1 :: L2 :: Rt, Res, L1, L2 => R]{
      def apply(f: L1 => L2 => R): L1 :: L2 :: Rt => Res = hl => fc(f(hl.head))(hl.tail)
    }
}

Int => String => Long => String结果为Int :: String :: Long :: HNil => String

3)使用Ts反转shapeless.HList.ReverseAux并收集HList的所有参数。你会得到Ts。然后将Ts => Res函数应用于HList个参数:

trait ArgumentsGetterAux[Prev <: HList, Rest <: HList, NR, Ts, Res]{
  def apply(p: Prev, f: Ts => Res): NR
}

object ArgumentsGetterAux{
  implicit def applyHNil[Prev <: HList, L, Res]: ArgumentsGetterAux[Prev, L :: HNil, L => Res, L :: Prev, Res] =
    new ArgumentsGetterAux[Prev, L :: HNil, L => Res, L :: Prev, Res]{
      def apply(p: Prev, f: L :: Prev => Res): L => Res = l => f(l :: p)
    }

  implicit def applyHList[Prev <: HList, L1, R <: HList, NR, Ts, Res](implicit aga: ArgumentsGetterAux[L1 :: Prev, R, NR, Ts, Res]): ArgumentsGetterAux[Prev, L1 :: R, L1 => NR, Ts, Res] =
    new ArgumentsGetterAux[Prev, L1 :: R, L1 => NR, Ts, Res]{
      def apply(p: Prev, f: Ts => Res): L1 => NR = l => aga(l :: p, f)
    }
}

invert方法:

import shapeless.HList.ReverseAux

def invert[A, R, Ts <: HList, Res, RevTs <: HList, NR](f: A => R)(
    implicit te: TypeExtractorAux[A, R, Ts, Res],
             r: ReverseAux[Ts, RevTs],
             ag: ArgumentsGetterAux[HNil, RevTs, NR, Ts, Res],
             fc: FunctionConverterAux[Ts, Res, A, R]
  ): NR = ag(HNil, fc(f))

用法:

scala> invert((i: Int) => (s: String) => (l: Long) => s"i: $i; s: $s; l: $l")
res0: Long => (String => (Int => String)) = <function1>

scala> res0(666L)("str")(1)
res1: String = i: 1; s: str; l: 666