简体   繁体   English

使用函子(Scalaz7)提升采用隐式参数的函数

[英]Lifting a function which takes implicit parameter using functor (Scalaz7)

Just started learning Scalaz. 刚开始学习Scalaz。 Here is my code 这是我的代码

trait Monoid[A] {
     def mappend(a1: A, a2: A): A
     def mzero: A
}

object Monoid {
    implicit val IntMonoid: Monoid[Int] = new Monoid[Int] {
        def mappend(a1: Int, a2: Int): Int = a1 + a2
        def mzero: Int = 0
    }

    implicit val StringMonoid: Monoid[String] = new Monoid[String] {
        def mappend(a1: String, a2: String): String = a1 + a2
        def mzero: String = ""
    }
}

trait MonoidOp[A] {
    val F: Monoid[A]
    val value: A
    def |+|(a2: A): A = F.mappend(value, a2)
}

object MonoidOp{
    implicit def toMonoidOp[A: Monoid](a: A): MonoidOp[A] = new MonoidOp[A]{
        val F = implicitly[Monoid[A]]
        val value = a
    }
}

I have defined a function (just for the sake of it) 我已经定义了一个函数(仅出于此目的)

def addXY[A: Monoid](x: A, y: A): A = x |+| y

I want to lift it so that it could be used using Containers like Option, List, etc. But when I do this 我想提起它,以便可以使用诸如Option,List等容器使用。但是当我这样做时

def addXYOptioned = Functor[Option].lift(addXY)

It says error: could not find implicit value for evidence parameter of type scalaz.Monoid[A] def addOptioned = Functor[Option].lift(addXY) 它表示error: could not find implicit value for evidence parameter of type scalaz.Monoid[A] def addOptioned = Functor[Option].lift(addXY)

How to lift such functions? 如何解除这种功能?

Your method addXY needs a Monoid[A] but there is no Monoid[A] in scope when used in addXYOptioned , so you also need to add the Monoid constraint to addXYOptioned . 您的方法addXY需要一个Monoid[A]但是在addXYOptioned使用时,范围内没有Monoid[A] ,因此您还需要向addXYOptioned约束中添加Monoid约束。

The next problem is that Functor.lift only lifts a function A => B , but we can use Apply.lift2 to lift a function (A, B) => C . 下一个问题是Functor.lift仅提升函数A => B ,但是我们可以使用Apply.lift2提升函数(A, B) => C

Using the Monoid from Scalaz itself : 使用Scalaz本身的Monoid

import scalaz._, Scalaz._

def addXY[A: Monoid](x: A, y: A): A = x |+| y

def addXYOptioned[A: Monoid] = Apply[Option].lift2(addXY[A] _)

We could generalize addXYOptioned to make it possible to lift addXY into any type constructor with an Apply instance : 我们可以概括化addXYOptioned以使其有可能通过Apply实例将addXY提升到任何类型的构造函数中:

def addXYApply[F[_]: Apply, A: Monoid] = Apply[F].lift2(addXY[A] _)

addXYApply[List, Int].apply(List(1,2), List(3,4))
// List[Int] = List(4, 5, 5, 6)

addXYApply[Option, Int].apply(1.some, 2.some)
// Option[Int] = Some(3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM