alex.hart
08/10/2017, 3:11 PMfun <T> List<T>.scan(fn: (T, T) -> T): List<T> {
return if (this.isEmpty()) this
else this.drop(1).fold(listOf(this.first()), { acc, item ->
acc.plus(fn(acc.last(), item))
})
}
println((1..5).toList().scan { x, y -> x + y })
I got one solution with fold, but you might be able to figure out a better approach.