Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created May 7, 2021 04:40
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 CheolhoJeon/ebb476fe543a4f86ac0a19d0ac067e7b to your computer and use it in GitHub Desktop.
Save CheolhoJeon/ebb476fe543a4f86ac0a19d0ac067e7b to your computer and use it in GitHub Desktop.
package chap4.FolddingLists
import atomictest.eq
fun main() {
val list = listOf('a', 'b', 'c', 'd')
list.fold("*") { acc, elem ->
"($acc) + $elem"
} eq "((((*) + a) + b) + c) + d"
list.foldRight("*") { elem, acc ->
"$elem + ($acc)"
} eq "a + (b + (c + (d + (*))))"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment