gregd
12/02/2018, 10:53 AMreduce
function?
I wan’t to reduce an infinite sequence of numbers, but only until some condition is met (based on the result of the reduce).
I know Clojure has this neat reduced
function for this. In Haskell we could use scan
. But do we have anything similar in Kotlin’s stdlib?Andreas Sinz
12/02/2018, 11:43 AMfun reduced(list: Iterable<Int>): Int {
return list.reduce { acc, item -> if(acc < 100) acc + item else return@reduced item }
}
Uzi Landsmann
12/02/2018, 2:55 PMpakoito
12/02/2018, 10:07 PMreduce
uses recursion, or at least the ones in arrow do. For those that are not stack-safe we have tailrecM, which are a bit more involved