简体   繁体   中英

How to “sum” stream of monoid values in Scala?

Suppose I am using the Rng library for simple Monte Carlo simulation (as in that post ).

val d      : Rng[Double] = double
val point  : Rng[(Double, Double)] = pair(d, d)
val points = point.stream(1000)
val tests  = points.map(point => if (insideCircle(point)) 1.0 else 0.0)

Now I need to sum all items of tests

tests[0] |+| tests[1] |+| tests[2] ... // Rng[Double] is a monoid

I can do it with fold but I would like to use some "shortcut" (smth. like sum: Seq[M[A]] => M[A] , where M is a monoid), instead. Do scala or scalaz have such a function ?

Scalaz拥有各种操作,例如suml ,假设Stream有一个Foldable typeclass实例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM