Scala猫。如何写kleisli [f [_],a,b] .ap?
我尝试编写一个Kleisli.ap函数。
final case class Kleisli[F[_], -A, B](run: (A) ⇒ F[B]) extends Product with Serializable
def ap[C, D, AA <: A](f: Kleisli[F, AA, C])(implicit F: Apply[F], ev: As[B, (C) ⇒ D]): Kleisli[F, AA, D]
但
import cats._
import cats.implicits._
import cats.data._
val x: Kleisli[Option,String,Int] = Kleisli(_ => Some(1))
val y: Kleisli[Option,String,Double] = Kleisli(_ => Some(1.0))
val kleisliAp: Kleisli[Option,String,Double] = x.ap(y)
// No implicits found for parameter ev: As[Int, Double => D_]
我看到了这个错误代码,并寻找一种创建 As[A,B] 实例的方法,但找不到。
如果您知道如何解决这个问题,请告诉我。
I tried to write a Kleisli.ap function.
final case class Kleisli[F[_], -A, B](run: (A) ⇒ F[B]) extends Product with Serializable
def ap[C, D, AA <: A](f: Kleisli[F, AA, C])(implicit F: Apply[F], ev: As[B, (C) ⇒ D]): Kleisli[F, AA, D]
but
import cats._
import cats.implicits._
import cats.data._
val x: Kleisli[Option,String,Int] = Kleisli(_ => Some(1))
val y: Kleisli[Option,String,Double] = Kleisli(_ => Some(1.0))
val kleisliAp: Kleisli[Option,String,Double] = x.ap(y)
// No implicits found for parameter ev: As[Int, Double => D_]
I saw this error code and looked for a way to create an instance of As[A,B] but could not find one.
Please let me know if you know how to solve this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ap
是组合两个对象的错误方法。ap
需要一个F[A]
和一个F[A =>; B]
。您有一个F[A]
和一个F[B]
相反,请使用
mapN
(x, y).mapN { ( int,双精度)=> ???此外
,
ap
在 scala FP 中被认为是不好的风格。它的形状确实不适合scala;它之所以存在,是因为它是Apply
基础的一部分,但mapN
是应用地组合对象的惯用方式ap
is the wrong way to combine the two objects you have.ap
requires anF[A]
and anF[A => B]
. You have anF[A]
and anF[B]
Instead, use
mapN
(x, y).mapN { (int, double) => ??? }
Additionally,
ap
is considered bad style in scala FP. The shape of it is really not suited to scala; it's there because it's part of the foundation ofApply
butmapN
is the idiomatic way to compose objects applicatively