我试图在Scala.js中使用Promises和Futures.承诺是有效的,一旦涉及期货,我会得到警告和错误.
尝试:
val p1 = Promise[Int]
val f1: Future[Int] = p1.future
val p2 = Promise[Int]
val f2: Future[Int] = p2.future
val res1 = for {
v1 <- f1
v2 <- f2
} yield v1 + v2
val res2 = f1.flatMap(x => f2.map(y => x + y))
res1 onSuccess {
case x: Int => g.console.log(x);
}
res2 onSuccess {
case x: Int => g.console.log(x);
}
// callback in dom, using ScalaTags
// div(`class` := "btn btn-default", `type` := "button", onclick := …Run Code Online (Sandbox Code Playgroud) 以下代码改编自论文(RO Bjarnason,Stackless Scala With Free Monads).
本文的标题总体上指出了所提出的数据结构的目的 - 即在常量堆栈空间中提供递归处理,并让用户以清晰的方式表达递归.
具体来说,我的目标是建立一个monadic结构,在升序时基于恒定堆栈空间中的简单模式匹配,提供不可变树对(二叉树)或列表(n-ary-tree)的结构重写.
sealed trait Free[S[+_], +A]{
private case class FlatMap[S[+_], A, +B](
a: Free[S, A],
f: A => Free[S, B]
) extends Free[S, B]
def map[B](f: A => B): Free[S, B] = this.flatMap((a:A) => Done[S, B](f(a)))
def flatMap[B](f: A => Free[S, B]): Free[S, B] = this match {
case FlatMap(a, g) => FlatMap(a, (x: Any) => g(x).flatMap(f))
case x => FlatMap(x, f)
}
@tailrec
final def resume(implicit S: Functor[S]): Either[S[Free[S, A]], A] …Run Code Online (Sandbox Code Playgroud)