Observable 发出交易对象。如何将每个发射的操作变成一个结果对象?
交易(交易类型:字符串,利润:BigDecimal)结果(totalProfit:BigDecimal)
例子:
trades
.scan(result: Result, currentTrade: Trade){
result.totalProfit += currentTrade.profit
}
.subscribe(
printLn(it.totalProfit)
)
Observable 发出交易对象。如何将每个发射的操作变成一个结果对象?
交易(交易类型:字符串,利润:BigDecimal)结果(totalProfit:BigDecimal)
例子:
trades
.scan(result: Result, currentTrade: Trade){
result.totalProfit += currentTrade.profit
}
.subscribe(
printLn(it.totalProfit)
)
你有这样的想法吗?下次请提供一个完整的例子。
import io.reactivex.rxjava3.core.Flowable
import org.junit.jupiter.api.Test
import java.math.BigDecimal
class SoScan {
private val seed : Result = Result(BigDecimal(0.0))
internal data class Result(val total : BigDecimal)
internal data class Trade(val profit : Double)
@Test
fun scan() {
Flowable.just(Trade(1.0), Trade(1.0))
.scan(seed) { prev, curr ->
Result(prev.total.plus(BigDecimal(curr.profit)))
}.test()
.assertValues(seed, Result(BigDecimal(1.0)), Result(BigDecimal(2.0)))
}
}