Scala 用于理解类型检查

Scala 用于理解类型检查,scala,Scala,有人能告诉我在Scala中是否可以使用以下语法吗 @annotation.tailrec def traverse[E,A,B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = { def go(es: List[A], rs: Either[E, List[B]]): Either[E, List[B]] = { es match { case Nil => rs

有人能告诉我在Scala中是否可以使用以下语法吗

  @annotation.tailrec
  def traverse[E,A,B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = {
    def go(es: List[A], rs: Either[E, List[B]]): Either[E, List[B]] = {
      es match {
        case Nil => rs
        case x::xs => for {
          Right(b) <- f(x);
          Right(ls) <- rs
        } yield go(xs, Right(b::ls))
      }
    }

    go(es, Right(List()))
  }
@annotation.tailrec
def遍历[E,A,B](es:List[A])(f:A=>任一[E,B]):任一[E,List[B]={
def go(es:List[A],rs:OR[E,List[B]]:OR[E,List[B]={
es比赛{
案例Nil=>rs
案例x::xs=>用于{

对(b)老实说,我不完全确定函数的目的是什么,但是,猜猜
f
是什么,这里有一些东西可以做你想做的

@annotation.tailrec
  def f[A, B, E](e: A): Either[E, B] = ???

  def go[A, B, E](es: List[A], rs: Either[E, List[B]]): Either[E, List[B]] = {
    es match {
      case Nil => rs
      case x :: xs => (f(x), rs) match {
        case (Right(b), Right(ls)) => go(xs, Right(b :: ls))
      }
    }

    go(es, Right(List()))
  }

尝试在rs上进行左匹配或右匹配?这是我试图实现的,但我得到了例外。我认为编译器不太可能计算出您的函数是tailrecursive。for将被分解为flatmap/Map,并作为函数传递收益率。这要求编译器计算出这是tail调用。h应该怎么做当rs不匹配
右(ls)
,或者
es的第一个元素不匹配
右(b)
?这一点很好@TheArchetypalPaul,似乎没有完全理解
@annotation.tailrec
  def f[A, B, E](e: A): Either[E, B] = ???

  def go[A, B, E](es: List[A], rs: Either[E, List[B]]): Either[E, List[B]] = {
    es match {
      case Nil => rs
      case x :: xs => (f(x), rs) match {
        case (Right(b), Right(ls)) => go(xs, Right(b :: ls))
      }
    }

    go(es, Right(List()))
  }