how to perform cumProd() operation over a list?
# getting-started
s
how to perform cumProd() operation over a list?
n
list.forEach { it.foo() }
s
I don’t think that’s what they mean by `cumProd()`…
s
hmm not sure, how it can do cumulative multiplication ?
s
assuming you mean cumulative product, then you probably want something like this
Copy code
list.runningReduce { acc, next -> acc * next }
n
depends also whether you want a single value at the end -- if yes, probably fold/reduce
s
if you’re working with a numeric type, you can shorten that by providing a reference to its
times
method, for example with `Int`:
Copy code
val cumprod = ints.runningReduce(Int::times)
1
s
got it, thx, that's exactly I wanted.
👍 1