Scala猫。如何写kleisli [f [_],a,b] .ap?

发布于 2025-01-17 10:52:09 字数 678 浏览 5 评论 0原文

我尝试编写一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烏雲後面有陽光 2025-01-24 10:52:09

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 an F[A] and an F[A => B]. You have an F[A] and an F[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 of Apply but mapN is the idiomatic way to compose objects applicatively

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文