加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

是否可以改进Scala中部分应用类型的类型推断?

发布时间:2020-12-16 08:55:40 所属栏目:安全 来源:网络整理
导读:我正在尝试在下面的代码中改进traverse_函数的类型推断: import scala.language.higherKindstrait Applicative[AF[_]] { def ap[A,B](a: AF[A])(f: AF[A = B]): AF[B] def pure[A](a: A): AF[A] def fmap[A,B](a: AF[A])(f: A = B): AF[B]}def traverse_[AP
我正在尝试在下面的代码中改进traverse_函数的类型推断:

import scala.language.higherKinds

trait Applicative[AF[_]] {

  def ap[A,B](a: AF[A])(f: AF[A => B]): AF[B]

  def pure[A](a: A): AF[A]

  def fmap[A,B](a: AF[A])(f: A => B): AF[B]

}

def traverse_[AP[_]: Applicative,A](xs: Iterable[A])(f: A => AP[Unit]): AP[Unit] = {
  val ap = implicitly[Applicative[AP]]
  (xs : ap.pure(())) { (x,acc) =>
    val apFunc = ap.fmap(f(x))(a => identity[Unit] _)
    ap.ap(acc)(apFunc)
  }
}

implicit def optionAp = new Applicative[Option] {

  def ap[A,B](a: Option[A])(f: Option[A => B]): Option[B] = f flatMap (a map _)

  def pure[A](a: A) = Some(a)

  def fmap[A,B](a: Option[A])(f: A => B) = a map f

}

implicit def eitherAp[L] = new Applicative[({type l[x]=Either[L,x]})#l] {

  def ap[A,B](a: Either[L,A])(f: Either[L,A => B]): Either[L,B] = f.right flatMap (a.right map _)

  def pure[A](a: A) = Right(a)

  def fmap[A,A])(f: A => B) = a.right map f

}

// silly,but compiles
val x = traverse_(1 to 10) {
  case 5 => None
  case _ => Some(())
}
println(x)

// also silly,but does not compile
val y = traverse_(1 to 10) {
  case 5 => Left("x")
  case _ => Right(())
}
println(y)

运行上面给出:

/Users/lodea/tmp/traverse.scala:49: error: no type parameters for method traverse_: (f: Int => AP[Unit])(implicit evidence$1: this.Applicative[AP])AP[Unit] exist so that it can be applied to arguments (Int => Product with Serializable with scala.util.Either[String,Unit])
 --- because ---
argument expression's type is not compatible with formal parameter type;
 found   : Int => Product with Serializable with scala.util.Either[String,Unit]
 required: Int => ?AP

val y = traverse_(1 to 10) {
                 ^
/Users/lodea/tmp/traverse.scala:49: error: type mismatch;
 found   : Int => Product with Serializable with scala.util.Either[String,Unit]
 required: Int => AP[Unit]
val y = traverse_(1 to 10) {
                           ^
two errors found

要使它编译,我必须指定traverse_的类型参数:

val y = traverse_[({type l[x]=Either[String,x]})#l,Int](1 to 10) {
  case 5 => Left("x")
  case _ => Right(())
}

有没有办法可以重构traverse_或代码的任何其他部分,以使类型推断工作?当类型开始变得越来越复杂时,这会变得很烦人.

解决方法

正如Ben James所指出的,你正在寻找Miles Sabin的 Unapply trick. Here它是在scalaz repo中. Here’s traverseU,在其帮助下实施. Here是一些示例用法.这是我对您的特定情况的粗略(希望是正确的)实现(注意:我已将您的Applicative重命名为ApplicativeTest,不会干扰在scalaz中定义的Applicative):

scalaz> core/console
[warn] Credentials file /home/folone/.ivy2/.credentials does not exist
[info] Starting scala interpreter...
[info] 
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM,Java 1.7.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scalaz._

trait ApplicativeTest[AF[_]] {
  def ap[A,B](a: AF[A])(f: AF[A => B]): AF[B]
  def pure[A](a: A): AF[A]
  def fmap[A,B](a: AF[A])(f: A => B): AF[B]
}

def traverse_[AP,A](xs: Iterable[A])(f: A => AP)(implicit G: Unapply[ApplicativeTest,AP]): G.M[Unit] = {
  (xs : G.TC.pure(())) { (x,acc) =>
    val apFunc = G.TC.fmap(G(f(x)))(a => identity[Unit] _)
    G.TC.ap(acc)(apFunc)
  }
}

implicit def optionAp = new ApplicativeTest[Option] {
  def ap[A,B](a: Option[A])(f: Option[A => B]): Option[B] = f flatMap (a map _)
  def pure[A](a: A) = Some(a)
  def fmap[A,B](a: Option[A])(f: A => B) = a map f
}

implicit def eitherAp[L]: ApplicativeTest[({type l[x]=Either[L,x]})#l] =
  new ApplicativeTest[({type l[x]=Either[L,x]})#l] {
    def ap[A,B] = f.right flatMap (a.right map _)
    def pure[A](a: A) = Right(a)
    def fmap[A,A])(f: A => B) = a.right map f
  }

implicit def iterAp = new ApplicativeTest[Iterable] {
  def ap[A,B](a: Iterable[A])(f: Iterable[A ? B]): Iterable[B] = f flatMap(a map _)
  def pure[A](a: A) = Iterable(a)
  def fmap[A,B](a: Iterable[A])(f: A ? B) = a map f
}

// Exiting paste mode,now interpreting.

import scalaz._
defined trait ApplicativeTest
traverse_: [AP,A](xs: Iterable[A])(f: A => AP)(implicit G: scalaz.Unapply[ApplicativeTest,AP])G.M[Unit]
optionAp: java.lang.Object with ApplicativeTest[Option]{def pure[A](a: A): Some[A]}
eitherAp: [L]=> ApplicativeTest[[x]Either[L,x]]
iterAp: java.lang.Object with ApplicativeTest[Iterable]

scala> val x = traverse_(1 to 10) {
     |   case 5 => None
     |   case _ => Some(())
     | }
x: Option[Unit] = None

scala> val y = traverse_(1 to 10) {
     |   case 5 => Left("x"): Either[String,Unit]
     |   case _ => Right(())
     | }
y: Either[String,Unit] = Left(x)

我仍然不知道如何使它推断[String,Unit]而不是带有scizable.util.Either [String,Unit]的Serializable,而不是严格指定类型,就像我在这一行中所做的那样:案例5 =>左(“x”):[String,Unit].

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读