在什么情况下应该liftIO使用?当我使用时ErrorT String IO,该lift功能可以解除IO操作ErrorT,因此liftIO看起来多余.
Java 6 API问题.并呼吁LockSupport.unpark(thread)有之前发生,从收益的关系LockSupport.park,在刚刚出车线程?我强烈怀疑答案是肯定的,但Javadoc似乎没有明确提及它.
我正在尝试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 …Run Code Online (Sandbox Code Playgroud) 使用Scala 2.8.1,编译:
val t = (40, 2)
println(for ((i, j) <- List(t)) yield i + j)
val e: Either[String, (Int, Int)] = Right(t)
println(e.right.map {
case (i, j) => i + j
})
println(for ((i, j) <- e.right) yield i + j)
Run Code Online (Sandbox Code Playgroud)
给出这个:
test.scala:9: error: constructor cannot be instantiated to expected type;
found : (T1, T2)
required: Either[Nothing,(Int, Int)]
println(for ((i, j) <- e.right) yield i + j)
Run Code Online (Sandbox Code Playgroud)
根据Scala中的Programming,for表达式应该等同于map/case表达式,但只有后者才能编译.我做错了什么,我应该怎么做?