Skip to content

Instantly share code, notes, and snippets.

@exallium
Created August 10, 2017 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exallium/c71f3dbd7c48d43fe52defd9b83e7f51 to your computer and use it in GitHub Desktop.
Save exallium/c71f3dbd7c48d43fe52defd9b83e7f51 to your computer and use it in GitHub Desktop.
Quick and Dirty Scan function in Kotlin
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