Scala + 对柯里化(Currying)函数的无形抽象

标签 scala shapeless currying

我正在尝试弄清楚如何对柯里化(Currying)函数进行抽象。 我可以通过以下方式抽象非柯里化(Currying)函数:

def liftAU[F, P <: Product, L <: HList, R, A[_]](f: F)
(implicit
 fp: FnToProduct.Aux[F, L => R],
 gen: Generic.Aux[P, L],
 ap: Applicative[A]
): A[P] => A[R] = p => p.map(gen.to).map(f.toProduct)

这将采用 (Int, Int) => Int 之类的函数,并将其转换为 Option[(Int, Int)] => Option[Int] 之类的东西。它适用于任何数量的功能。

我想创建柯里化(Currying)版本,它将采用类似 Int => Int => Int 的函数并将其转换为 Option[Int] => Option[Int] => Option[Int]。

它也应该适用于任何数量的柯里化(Currying)函数。

由于 FnToProduct 仅适用于第一个参数列表,因此在这里没有帮助,我还尝试在类型级别编写一些递归定义,但在定义类型时遇到问题。

不太确定是否可能,但很想知道其他人是否尝试过这样的事情。

最佳答案

Dmytro 的答案实际上对我不起作用,除非我更改其中一个对象中的实例名称,即使如此,它也不适用于像 Int => Int => Int => Int< 这样的函数,而且我发现使用 Poly 值确实很烦人,所以我不会调试前面的答案,而是编写自己的答案。

实际上,您可以使用 100% 无形状自由类型类很好地编写此操作:

import cats.Applicative

trait LiftCurried[F[_], I, O] {
  type Out
  def apply(f: F[I => O]): F[I] => Out
}

object LiftCurried extends LowPriorityLiftCurried {
  implicit def liftCurried1[F[_]: Applicative, I, I2, O2](implicit
    lc: LiftCurried[F, I2, O2]
  ): Aux[F, I, I2 => O2, F[I2] => lc.Out] = new LiftCurried[F, I, I2 => O2] {
    type Out = F[I2] => lc.Out
    def apply(f: F[I => I2 => O2]): F[I] => F[I2] => lc.Out =
      (Applicative[F].ap(f) _).andThen(lc(_))
  }
}

trait LowPriorityLiftCurried {
  type Aux[F[_], I, O, Out0] = LiftCurried[F, I, O] { type Out = Out0 }

  implicit def liftCurried0[F[_]: Applicative, I, O]: Aux[F, I, O, F[O]] =
    new LiftCurried[F, I, O] {
      type Out = F[O]
      def apply(f: F[I => O]): F[I] => F[O] = Applicative[F].ap(f) _
    }
}

可能可以使其更清晰一些,但我发现它的可读性是合理的。

您可能想要像这样具体的东西:

def liftCurriedIntoOption[I, O](f: I => O)(implicit
  lc: LiftCurried[Option, I, O]
): Option[I] => lc.Out = lc(Some(f))

然后我们可以演示它可以与如下所示的一些函数一起使用:

val f: Int => Int => Int = x => y => x + y
val g: Int => Int => Int => Int = x => y => z => x + y * z
val h: Int => Int => Int => String => String = x => y => z => _ * (x + y * z)

然后:

scala> import cats.instances.option._
import cats.instances.option._

scala> val ff = liftCurriedIntoOption(f)
ff: Option[Int] => (Option[Int] => Option[Int]) = scala.Function1$$Lambda$1744/350671260@73d06630

scala> val gg = liftCurriedIntoOption(g)
gg: Option[Int] => (Option[Int] => (Option[Int] => Option[Int])) = scala.Function1$$Lambda$1744/350671260@2bb9b82c

scala> val hh = liftCurriedIntoOption(h)
hh: Option[Int] => (Option[Int] => (Option[Int] => (Option[String] => Option[String]))) = scala.Function1$$Lambda$1744/350671260@45eec9c6

我们还可以多次应用它,只是为了它的 hell :

scala> val hhhh = liftCurriedIntoOption(liftCurriedIntoOption(hh))
hhh: Option[Option[Option[Int]]] => (Option[Option[Option[Int]]] => (Option[Option[Option[Int]]] => (Option[Option[Option[String]]] => Option[Option[Option[String]]]))) = scala.Function1$$Lambda$1744/350671260@592593bd

所以类型看起来不错,对于值来说......

scala> ff(Some(1))(Some(2))
res0: Option[Int] = Some(3)

scala> ff(Some(1))(None)
res1: Option[Int] = None

scala> hh(Some(1))(None)(None)(None)
res2: Option[String] = None

scala> hh(Some(1))(Some(2))(Some(3))(Some("a"))
res3: Option[String] = Some(aaaaaaa)

...我认为这就是您的目标。

关于Scala + 对柯里化(Currying)函数的无形抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47007733/

相关文章:

scala - 案例类 - 通过转换复制多个字段

scala - 使用 "Prolog in Scala"查找可用的类型类实例

scala - 从一种类型的 Monoids HList 派生 HList of zeroes

haskell - 实现 curry 功能

haskell - 为什么在 Haskell 中为 Curry 类考虑局部小笛卡尔封闭类别是公平的?

mysql - "Hello World"使用 MySQL 的 Slick 2.0 示例

scala - sbt 中命令的更好语法可能吗?

scala - 在 Scala 3 中,为什么有时可以进行谓式类型赋值?

scala - storeAs ...triggeredBy 如何在 simple-build-tool 中工作?

f# - 高阶函数和柯里化(Currying)函数之间的区别