Back
Close

Kotlin Tour

joow
9,848 views

Lambdas and Collections

Higher-order functions

https://kotlinlang.org/docs/reference/lambdas.html#higher-order-functions

A higher-order function is a function that either :

- takes at least one function as argument
- returns a function

Lambda expressions

https://kotlinlang.org/docs/reference/lambdas.html#lambda-expressions-and-anonymous-functions

The formal syntax for lambda expression is :

val sum = { x: Int, y: Int -> x + y }

We can also rewrite the previous expression like this :

val sum: (Int, Int) -> Int = { x, y -> x + y }

If the compiler can infer the signature you can leave out the signature :

ints.filter { it > 0 }
Declare a function negate that takes a predicate and returns its opposite
fun <T> negate(f: (T) -> Boolean): (T) -> Boolean = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Destructuring Lambdas

https://kotlinlang.org/docs/reference/multi-declarations.html#destructuring-in-lambdas-since-11

Since Kotlin 1.1 it is possible to destruture lambdas.

Given a map of countries and their capital, create a function capital printing for each entry "The capital of ... is ..."
val countries = mapOf("France" to "Paris", "Germany" to "Berlin", "Spain" to "Madrid", "Italy" to "Rome")
fun capitals(): Unit = TODO()
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Collections

https://kotlinlang.org/docs/reference/collections.html

Kotlin has immutable and mutable collections :

// List
val mutableList = mutableListOf("Java", "Kotlin", "JavaScript")
mutableList.add("Go")
val immutableList = listOf("Java", "Kotlin", "JavaScript")
immutableList.plus("Go") // returns a new list
// Set
val mutableSet = mutableSetOf(1, 3, 5, 7, 9)
mutableSet.add(11)
val immutableSet = setOf(0, 2, 4, 6, 8)
immutableSet.plus(10) // returns a new set
// Map    
val mutableMap = mutableMapOf("1" to 1, "2" to 2, "3" to 3)
mutableMap.put("4", 4)
val immutableMap = mapOf("1" to 1, "2" to 2, "3" to 3)
immutableMap.plus("4" to 4) // returns a new map

Fun with numbers

Generate the ten first numbers and print them
fun main(args: Array<String>) {
TODO()
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Generate the first ten powers of two and print them
fun main(args: Array<String>) {
TODO()
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Print the first power of 2 greater than 10000
fun main(args: Array<String>) {
TODO()
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Fun with data

Print the birth countries of the musicians, without duplicate
package musician
data class Musician(val lastName: String, val firstName: String,
val birthDate: String, val birthCity: String, val birthCountry: String,
val deathDate: String, val deathCity: String, val deathCountry: String)
val musicians = listOf(
Musician("Haendel", "Georg", "1685-02-23", "Halle", "Allemagne", "1759-04-14", "Londres", "Royaume-Uni"),
Musician("Brahms", "Johannes", "1833-05-07", "Hambourg", "Allemagne", "1897-04-03", "Vienne", "Autriche"),
Musician("Chopin", "Frédéric", "1810-03-01", "Żelazowa Wola", "Pologne", "1849-10-17", "Paris", "France"),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Generate a string containing the last names of the musicians, sorted in ascending order and separated by a comma
package musician
data class Musician(val lastName: String, val firstName: String,
val birthDate: String, val birthCity: String, val birthCountry: String,
val deathDate: String, val deathCity: String, val deathCountry: String)
val musicians = listOf(
Musician("Haendel", "Georg", "1685-02-23", "Halle", "Allemagne", "1759-04-14", "Londres", "Royaume-Uni"),
Musician("Brahms", "Johannes", "1833-05-07", "Hambourg", "Allemagne", "1897-04-03", "Vienne", "Autriche"),
Musician("Chopin", "Frédéric", "1810-03-01", "Żelazowa Wola", "Pologne", "1849-10-17", "Paris", "France"),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Count the number of musicians having their last name containing the letter B, no matter the case
package musician
data class Musician(val lastName: String, val firstName: String,
val birthDate: String, val birthCity: String, val birthCountry: String,
val deathDate: String, val deathCity: String, val deathCountry: String)
val musicians = listOf(
Musician("Haendel", "Georg", "1685-02-23", "Halle", "Allemagne", "1759-04-14", "Londres", "Royaume-Uni"),
Musician("Brahms", "Johannes", "1833-05-07", "Hambourg", "Allemagne", "1897-04-03", "Vienne", "Autriche"),
Musician("Chopin", "Frédéric", "1810-03-01", "Żelazowa Wola", "Pologne", "1849-10-17", "Paris", "France"),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Print the list of death countries and the number of deceased musicians
package musician
data class Musician(val lastName: String, val firstName: String,
val birthDate: String, val birthCity: String, val birthCountry: String,
val deathDate: String, val deathCity: String, val deathCountry: String)
val musicians = listOf(
Musician("Haendel", "Georg", "1685-02-23", "Halle", "Allemagne", "1759-04-14", "Londres", "Royaume-Uni"),
Musician("Brahms", "Johannes", "1833-05-07", "Hambourg", "Allemagne", "1897-04-03", "Vienne", "Autriche"),
Musician("Chopin", "Frédéric", "1810-03-01", "Żelazowa Wola", "Pologne", "1849-10-17", "Paris", "France"),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Print the musicians ordered by their death age. Who was the youngest and who was the oldest ?
package musician
data class Musician(val lastName: String, val firstName: String,
val birthDate: String, val birthCity: String, val birthCountry: String,
val deathDate: String, val deathCity: String, val deathCountry: String)
val musicians = listOf(
Musician("Haendel", "Georg", "1685-02-23", "Halle", "Allemagne", "1759-04-14", "Londres", "Royaume-Uni"),
Musician("Brahms", "Johannes", "1833-05-07", "Hambourg", "Allemagne", "1897-04-03", "Vienne", "Autriche"),
Musician("Chopin", "Frédéric", "1810-03-01", "Żelazowa Wola", "Pologne", "1849-10-17", "Paris", "France"),
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants