2

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 ?

Community
  • 1
  • 1
Michael
  • 41,026
  • 70
  • 193
  • 341

1 Answers1

6

Scalaz has various ops e.g. suml, assuming there's a Foldable typeclass instance for Stream.

lmm
  • 17,386
  • 3
  • 26
  • 37