Created
August 10, 2017 15:13
-
-
Save exallium/c71f3dbd7c48d43fe52defd9b83e7f51 to your computer and use it in GitHub Desktop.
Quick and Dirty Scan function in Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <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 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment