Skip to content

Instantly share code, notes, and snippets.

@marrek13
Created April 22, 2022 16:35
Show Gist options
  • Save marrek13/3780b947153526aa0c5a63bbb6981293 to your computer and use it in GitHub Desktop.
Save marrek13/3780b947153526aa0c5a63bbb6981293 to your computer and use it in GitHub Desktop.
Fold Kotlin
fun <T, R> Collection<T>.fold(
initial: R,
combine: (acc: R, nextElement: T) -> R
): R {
var accumulator: R = initial
for (element: T in this) {
accumulator = combine(accumulator, element)
}
return accumulator
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment