- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
andThen
意思是我从中学到了answer是一个函数 Composer 。
比如说
f andThen g andThen h
h(g(f(x)))
h function
将接收来自
g(f(x))
的输入
andThen
在
Future
, 后面的所有闭包 andThen 总是从原来的
Future
接收结果.
Future{
1
}.andThen{ case Success(x) =>
println(x) // print 1
Thread.sleep(2000)
x * 2
}.andThen{ case Success(x) =>
println(x) // print 1
Thread.sleep(2000)
x * 2
}
val func: Function1[Int, Int] = { x: Int =>
x
}.andThen { y =>
println(y) // print 1
y * 2
}.andThen { z =>
println(z) // print 2
z * 2
}
func(1)
最佳答案
scala.concurrent.Future
被设计为两种异步方法的折衷方案:
Future.andThen
's docs :
Applies the side-effecting function to the result of this future, and returns a new future with the result of this future.
andThen
最有可能来自 OOP 世界。获得与
Function1.andThen
类似的结果你可以使用
map
方法 :
Future(1).map {_ * 2}.map {_ * 2}
andThen
不同于
onComplete
用一件小事:
andThen
的 future 仍然返回相同的结果,但会等到提供的观察者返回或抛出一些东西。这就是为什么在文档中写的:
This method allows one to enforce that the callbacks are executed in a specified order.
Note that if one of the chained andThen callbacks throws an exception, that exception is not propagated to the subsequent andThen callbacks. Instead, the subsequent andThen callbacks are given the original value of this future.
Future
完全没有任何作用的结果。甚至不能用它自己的异常(exception)来破坏它。这个
andThen
和
onComplete
只是观察者的顺序和并行绑定(bind)。
关于scala - 为什么 Future 的 andThen 不链接结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30659427/
我遇到了andThen ,但没有正确理解它。 为了进一步了解它,我阅读了 Function1.andThen文档 def andThen[A](g: (R) ⇒ A): (T1) ⇒ A mm是 Mu
让我们来看两个用 Rx 编写的方法: Maybe getFooFromLocal Single getFooFromNetwork 我想在检查本地存储的Foo 时编写一个链。如果我们没有任何 Foo,
使用 Scalaz,一个函数可以映射到另一个函数上。我什么时候想使用 map在 andThen ?使用 map 是否有明显优势? ?谢谢 例如, val f: Int => Int = (a) =>
这里有两个示例函数 def fun1(s: String, x: Int) = x def fun2(x: Int) = x 我想部分应用 fun1 并使用 andThen 将其与 fun2 组合。
我发现为了模式匹配 Future皮草 Success/Failure , 我需要使用 andThen (或 onComplete , onSuccess ...)并且不能使用 map .这是为什么?
我正在按照教程 Pattern matching & functional composition Scala compose 和 andThen 方法。有这样一个例子: scala> def add
我对 ember.js 项目还很陌生,我正在尝试编写第一个验收测试来测试我的根路径是否有效。我正在关注以下tutorial 。我无法从助手中导入“接受模块”,因为它已被弃用。当我运行下面的测试时,我收
我有一堆函数可以清理文本并将它们拆分为单词。最小的例子: val txt = "Mary had a @little \nlamb" val stopwords = Seq("a") def clea
我需要一个 emberJs 验收测试,以便能够滚动到页面中的某个点,然后才对页面断言。 这两个功能, Ember.$('body').scrollTop(1000); window.scroll(0,
我有一个用例,我在 Completable 中初始化了一些全局变量,并在链的下一步中(使用 andThen 运算符)我使用了这些变量。 以下示例详细解释了我的用例 假设你有一个类 User
我有以下 POC 可以使用 Java 8 功能。 我想在接受方法后更新数据库。使用 andThen() 好吗?什么时候调用这个方法?谁叫它? andThen()方法的基本用法是什么?查看文档令人困惑。
我有以下内容: def become(r: ListMap[String, Receive]) { val undefined: PartialFunction[Any, Unit] = Map.
我有“ parent ”A包含“ child ”的类型 B类型。 这是我应用中主要数据结构的简化版本。 A和 B和 A_id和 B_id都是独立的榆树模块。 如果我能让这个简化工作,那么也许更容易解决
为什么andThen只存在于 Scala 中的单参数函数? 以下代码有效: val double = (x: Int) => x * 2 val timesFour = double andThen
andThen意思是我从中学到了answer是一个函数 Composer 。 比如说 f andThen g andThen h 将等于 h(g(f(x))) 这意味着 h function将接收来自
我是 Java 8 的新手,只是想问一下下面两个代码片段在性能上有什么区别。 我知道两者都有效,但我想知道为什么 Java 团队创建 Consumer::andThen当我可以使用正常方法时的方法。
我正在尝试链接两个返回 Completable 的响应式调用在 android 上使用改造: val userRequest = ... val languageRequest = ... retur
我正在尝试使用 Byte Buddy 在方法完成时执行一些额外的逻辑。这是示例代码: T proxyClient = JAXRSClientFactory.fromClient(client,
在我的 Android 应用程序中,我想更新部分数据库条目(使用 Room),然后在更新完成后立即从数据库中读取整个条目。 阅读RxJava Completable的文档后我希望 public fin
我有一个像这样创建的 Completable: public Completable doCalulations() { return Completable.fromCallable(()
我是一名优秀的程序员,十分优秀!