Skip to content

Instantly share code, notes, and snippets.

@andrewharmellaw
Created March 12, 2020 14:59
Show Gist options
  • Save andrewharmellaw/b7d8bb217d9a3edb74008122d1cff127 to your computer and use it in GitHub Desktop.
Save andrewharmellaw/b7d8bb217d9a3edb74008122d1cff127 to your computer and use it in GitHub Desktop.
Folding in Kotlin (Tests)
@Test
fun `foldLeft on a very large list is stack-safe`() {
val seed = 1;
val massiveList = mutableListOf<Int>()
for (i in 1..10000) {
massiveList.add(i)
}
assertEquals(500501, foldLeft(massiveList, seed, ::sum))
}
@Test
fun `foldRight on a very large list isn't stack safe`() {
val seed = 1;
val massiveList = mutableListOf<Int>()
for (i in 1..10000) {
massiveList.add(i)
}
assertEquals(500501, foldRightOverflows(massiveList, seed, ::product))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment