【问题标题】:What methods are needed to create custom Applicative Functor to use with scalaz |@|需要哪些方法来创建自定义 Applicative Functor 以与 scalaz 一起使用 |@|
【发布时间】:2017-08-15 14:16:36
【问题描述】:

我希望能够使用 scalaz 的 |@|在我自己的应用函子上。

示例:
val spread: Source[Yield] = (y2 |@| y1)(_ - _)

这是我的课

sealed abstract class Source[+A] {
  def map[B](f: A => B): Source[B] 
  def unit[A](a: A): Source[A]
  def pureList[A](la: List[A]): Source[A]
  def zero[A]: Source[A]
  def map2[A, B, C](as: Source[A], bs: Source[B], f: (A, B) => C): Source[C]
}

我确定我必须实现 map,因为它是一个函子。
应用程序可以通过多种方式实现:例如使用apply()unit()map2()unit()

我还需要appure 吗?

如您所见,我不确定需要什么。

【问题讨论】:

  • 请注意,对于您的示例,您只需要 Apply 而不是 Applicative
  • @LukaJacobowitz 我不明白;你的意思是我不需要方法apply 但我需要方法applicative?或者你的意思是我应该称它为 Apply 函子而不是 Applicative 函子

标签: scala functional-programming scalaz applicative


【解决方案1】:
implicit val mya = new Applicative[Source] {}

让编译器为你回答这个问题:

object creation impossible, since:
it has 2 unimplemented members.
/** As seen from <$anon: scalaz.Applicative[Source]>, the missing signatures are as follows.
 *  For convenience, these are usable as stub implementations.
 */
  // Members declared in scalaz.Applicative
  def point[A](a: => A): Source[A] = ???

  // Members declared in scalaz.Apply
  def ap[A, B](fa: => Source[A])(f: => Source[A => B]): Source[B] = ???

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-22
    • 2013-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多