Created
April 22, 2022 16:35
-
-
Save marrek13/3780b947153526aa0c5a63bbb6981293 to your computer and use it in GitHub Desktop.
Fold Kotlin
This file contains 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, 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