Skip to content

Instantly share code, notes, and snippets.

@aldajo92
Last active June 17, 2017 00:10
Show Gist options
  • Save aldajo92/d5d3e4cd2e47b01df6fc834fd2ffbcc8 to your computer and use it in GitHub Desktop.
Save aldajo92/d5d3e4cd2e47b01df6fc834fd2ffbcc8 to your computer and use it in GitHub Desktop.
/**
* https://antonioleiva.com/collection-operations-kotlin/
*/
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6)
println(list.any { it % 2 == 0 })
println(list.map { it + 1 })
val listPair = listOf(2, 4, 6, 8, 10)
println(listPair.all { it % 2 == 0})
val listRepeat = listOf(2, 5, 2, 5, 8, 9)
println(listRepeat.count { it == 2})
val listAccumulate = listOf(2, 2, 3, 4, 1)
println(listAccumulate.fold(0) { total, next -> total + next})
println(listAccumulate.foldRight(0) {total, next -> total + next})
println("----------------------")
val listWords = listOf("hello", "hola", "hallo")
listWords.forEach{ println(it) }
listWords.forEachIndexed{ index, value -> println("position $index contains a $value") }
val listMax = listOf(9, 9, 9, 111, 0, 0)
val maxValue = listMax.max()
println("max value is $maxValue")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment