斯卡拉(Scala)和猫:隐式转换为身份单子 [英] Scala and cats: Implicit conversion to identity monad

查看:102
本文介绍了斯卡拉(Scala)和猫:隐式转换为身份单子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计算数字类型平方和的函数,如下所示.

I have a function that computes sum of squares for numeric types as shown below.

import cats.syntax.functor._
import cats.syntax.applicative._
import cats.{Id, Monad}

import scala.language.higherKinds

object PowerOfMonads {
       /**
        * Ultimate sum of squares method
        *
        * @param x First value in context
        * @param y Second value in context
        * @tparam F Monadic context
        * @tparam T Type parameter in the Monad
        * @return Sum of squares of first and second values in the Monadic context
        */
    def sumOfSquares[F[_]: Monad, A, T >: A](x: F[A], y: F[A])(implicit num: Numeric[T]) : F[T] = {
        def square(value:T): T = num.times(value, value)
        def sum(first:T, second:T): T = num.plus(first, second)

        for {
            first <- x
            second <- y
        } yield sum(square(first), square(second))
    }
}

从客户端代码中,我想利用如下所示的功能

From the client code, I would like to utilise the function as shown below

import cats.Id
import cats.instances.future._
import cats.instances.list._
import cats.instances.option._
import cats.syntax.applicative._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

println(s"Sum of squares for list ${PowerOfMonads.sumOfSquares(  List(1,2,3), List(1,2,3) )}")
println(s"Sum of squares for options ${PowerOfMonads.sumOfSquares(  Option(1), 2.pure[Option] )}")
println(s"Sum of squares for future ${PowerOfMonads.sumOfSquares(  1.pure[Future], Future(2) ).value}")
println(s"Sum of squares for id ${PowerOfMonads.sumOfSquares(1.pure[Id], 2.pure[Id])}")


现在,我想使用从数字类型T到Id [T]的隐式转换来调用函数sumOfSquares,如下所示


Now, I would like to use implicit conversion from a numeric type T to Id[T] to invoke the function sumOfSquares as shown below

println(s"Sum of squares for int ${PowerOfMonads.sumOfSquares(1, 2)}")

使用如下所示的功能

import cats.syntax.applicative._
import scala.language.implicitConversions
   /**
    * Implicit conversion for any numeric type T to Id[T]
    * @param value value with type T
    * @tparam T numeric type
    * @return numeric type wrapped in the context of an Identity Monad
    */
implicit def anyNum2Id[T](value:T)(implicit num: Numeric[T]):Id[T] = value.pure[Id]


但是,在执行程序时,出现以下错误


However, when executing the program, I get the following errors

找不到类型为证据的隐式值 cats.Monad [[A] Any] println(s"int的平方和

could not find implicit value for evidence parameter of type cats.Monad[[A]Any] println(s"Sum of squares for int

$ {PowerOfMonads.sumOfSquares(1,2)})没有足够的参数作为方法 sumOfSquares :(隐式证据$ 1:cats.Monad [[A] Any],隐式num: 数值[T]).未指定的值参数证据$ 1,数字. println(s"int $ {PowerOfMonads.sumOfSquares(1, 2)})

${PowerOfMonads.sumOfSquares(1, 2)}") not enough arguments for method sumOfSquares: (implicit evidence$1: cats.Monad[[A]Any], implicit num: Numeric[T])Any. Unspecified value parameters evidence$1, num. println(s"Sum of squares for int ${PowerOfMonads.sumOfSquares(1, 2)}")

请帮助我解决该错误.

推荐答案

更改导入:

  import cats.syntax.flatMap._
  import cats.syntax.functor._
  import cats.Monad

  import scala.language.higherKinds

  object PowerOfMonads {
  ...


您可以帮助编译器推断类型:


You can help compiler to infer types:

PowerOfMonads.sumOfSquares(1: Id[Int], 2: Id[Int])

PowerOfMonads.sumOfSquares[Id, Int, Int](1, 2)

这篇关于斯卡拉(Scala)和猫:隐式转换为身份单子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆