Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
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
1
fun <T> negate(f: (T) -> Boolean): (T) -> Boolean = TODO()
Enter to Rename, Shift+Enter to Preview
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 ..."
1
2
3
4
val countries = mapOf("France" to "Paris", "Germany" to "Berlin", "Spain" to "Madrid", "Italy" to "Rome")
fun capitals(): Unit = TODO()
Enter to Rename, Shift+Enter to Preview
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
1
2
3
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Generate the first ten powers of two and print them
1
2
3
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Print the first power of 2 greater than 10000
1
2
3
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Fun with data
Print the birth countries of the musicians, without duplicate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"),
Musician("Mozart", "Wolfgang", "1756-01-27", "Salzbourg", "Allemagne", "1791-12-05", "Vienne", "Autriche"),
Musician("Dvořák", "Antonin", "1841-09-08", "Prague", "République Tchèque", "1904-05-01", "Prague", "République Tchèque"),
Musician("Rossini", "Gioachino", "1792-02-29", "Pesaro", "Italie", "1868-11-13", "Paris", "France"),
Musician("Haydn", "Joseph", "1732-03-31", "Rohrau", "Autriche", "1809-05-31", "Vienne", "Autriche"),
Musician("Mendelssohn", "Félix", "1809-02-03", "Hambourg", "Allemagne", "1847-11-04", "Leipzig", "Allemagne"),
Musician("Tchaïkovski", "Piotr", "1840-05-07", "Votkinsk", "Russie", "1893-11-06", "Saint-Pétersbourg", "Russie"),
Musician("Liszt", "Franz", "1811-10-22", "Raiding", "Autriche", "1886-07-31", "Bayreuth", "Allemagne"),
Musician("Salieri", "Antonio", "1750-08-18", "Vérone", "Italie", "1825-05-07", "Vienne", "Autriche"),
Musician("Schumann", "Robert", "1810-06-08", "Zwickau", "Allemagne", "1856-07-29", "Bonn", "Allemagne"),
Musician("Schubert", "Franz", "1797-01-31", "Vienne", "Autriche", "1828-11-19", "Vienne", "Autriche"),
Musician("Debussy", "Claude", "1862-08-22", "Saint-Germain-en-Laye", "France", "1918-03-25", "Paris", "France"),
Musician("Wagner", "Richard", "1813-05-22", "Leipzig", "Allemagne", "1883-02-13", "Venise", "Italie"),
Musician("Van Beethoven", "Ludwig", "1770-12-16", "Bonn", "Allemagne", "1827-03-29", "Vienne", "Autriche"),
Musician("Strauss", "Richard", "1864-06-11", "Munich", "Allemagne", "1949-09-08", "Garmisch-Partenkirchen", "Allemagne"),
Musician("Strauss", "Johann", "1825-10-25", "Vienne", "Autriche", "1899-06-03", "Vienne", "Autriche"),
Musician("Vivaldi", "Antonio", "1678-03-04", "Venise", "Italie", "1741-07-28", "Vienne", "Autriche"),
Musician("Verdi", "Giuseppe", "1813-10-10", "Roncole", "Italie", "1901-01-27", "Milan", "Italie"),
Musician("Bach", "Jean-Sébastien", "1685-03-31", "Eisenach", "Allemagne", "1750-07-28", "Leipzig", "Allemagne"))
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Generate a string containing the last names of the musicians, sorted in ascending order and separated by a comma
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"),
Musician("Mozart", "Wolfgang", "1756-01-27", "Salzbourg", "Allemagne", "1791-12-05", "Vienne", "Autriche"),
Musician("Dvořák", "Antonin", "1841-09-08", "Prague", "République Tchèque", "1904-05-01", "Prague", "République Tchèque"),
Musician("Rossini", "Gioachino", "1792-02-29", "Pesaro", "Italie", "1868-11-13", "Paris", "France"),
Musician("Haydn", "Joseph", "1732-03-31", "Rohrau", "Autriche", "1809-05-31", "Vienne", "Autriche"),
Musician("Mendelssohn", "Félix", "1809-02-03", "Hambourg", "Allemagne", "1847-11-04", "Leipzig", "Allemagne"),
Musician("Tchaïkovski", "Piotr", "1840-05-07", "Votkinsk", "Russie", "1893-11-06", "Saint-Pétersbourg", "Russie"),
Musician("Liszt", "Franz", "1811-10-22", "Raiding", "Autriche", "1886-07-31", "Bayreuth", "Allemagne"),
Musician("Salieri", "Antonio", "1750-08-18", "Vérone", "Italie", "1825-05-07", "Vienne", "Autriche"),
Musician("Schumann", "Robert", "1810-06-08", "Zwickau", "Allemagne", "1856-07-29", "Bonn", "Allemagne"),
Musician("Schubert", "Franz", "1797-01-31", "Vienne", "Autriche", "1828-11-19", "Vienne", "Autriche"),
Musician("Debussy", "Claude", "1862-08-22", "Saint-Germain-en-Laye", "France", "1918-03-25", "Paris", "France"),
Musician("Wagner", "Richard", "1813-05-22", "Leipzig", "Allemagne", "1883-02-13", "Venise", "Italie"),
Musician("Van Beethoven", "Ludwig", "1770-12-16", "Bonn", "Allemagne", "1827-03-29", "Vienne", "Autriche"),
Musician("Strauss", "Richard", "1864-06-11", "Munich", "Allemagne", "1949-09-08", "Garmisch-Partenkirchen", "Allemagne"),
Musician("Strauss", "Johann", "1825-10-25", "Vienne", "Autriche", "1899-06-03", "Vienne", "Autriche"),
Musician("Vivaldi", "Antonio", "1678-03-04", "Venise", "Italie", "1741-07-28", "Vienne", "Autriche"),
Musician("Verdi", "Giuseppe", "1813-10-10", "Roncole", "Italie", "1901-01-27", "Milan", "Italie"),
Musician("Bach", "Jean-Sébastien", "1685-03-31", "Eisenach", "Allemagne", "1750-07-28", "Leipzig", "Allemagne"))
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Count the number of musicians having their last name containing the letter B, no matter the case
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"),
Musician("Mozart", "Wolfgang", "1756-01-27", "Salzbourg", "Allemagne", "1791-12-05", "Vienne", "Autriche"),
Musician("Dvořák", "Antonin", "1841-09-08", "Prague", "République Tchèque", "1904-05-01", "Prague", "République Tchèque"),
Musician("Rossini", "Gioachino", "1792-02-29", "Pesaro", "Italie", "1868-11-13", "Paris", "France"),
Musician("Haydn", "Joseph", "1732-03-31", "Rohrau", "Autriche", "1809-05-31", "Vienne", "Autriche"),
Musician("Mendelssohn", "Félix", "1809-02-03", "Hambourg", "Allemagne", "1847-11-04", "Leipzig", "Allemagne"),
Musician("Tchaïkovski", "Piotr", "1840-05-07", "Votkinsk", "Russie", "1893-11-06", "Saint-Pétersbourg", "Russie"),
Musician("Liszt", "Franz", "1811-10-22", "Raiding", "Autriche", "1886-07-31", "Bayreuth", "Allemagne"),
Musician("Salieri", "Antonio", "1750-08-18", "Vérone", "Italie", "1825-05-07", "Vienne", "Autriche"),
Musician("Schumann", "Robert", "1810-06-08", "Zwickau", "Allemagne", "1856-07-29", "Bonn", "Allemagne"),
Musician("Schubert", "Franz", "1797-01-31", "Vienne", "Autriche", "1828-11-19", "Vienne", "Autriche"),
Musician("Debussy", "Claude", "1862-08-22", "Saint-Germain-en-Laye", "France", "1918-03-25", "Paris", "France"),
Musician("Wagner", "Richard", "1813-05-22", "Leipzig", "Allemagne", "1883-02-13", "Venise", "Italie"),
Musician("Van Beethoven", "Ludwig", "1770-12-16", "Bonn", "Allemagne", "1827-03-29", "Vienne", "Autriche"),
Musician("Strauss", "Richard", "1864-06-11", "Munich", "Allemagne", "1949-09-08", "Garmisch-Partenkirchen", "Allemagne"),
Musician("Strauss", "Johann", "1825-10-25", "Vienne", "Autriche", "1899-06-03", "Vienne", "Autriche"),
Musician("Vivaldi", "Antonio", "1678-03-04", "Venise", "Italie", "1741-07-28", "Vienne", "Autriche"),
Musician("Verdi", "Giuseppe", "1813-10-10", "Roncole", "Italie", "1901-01-27", "Milan", "Italie"),
Musician("Bach", "Jean-Sébastien", "1685-03-31", "Eisenach", "Allemagne", "1750-07-28", "Leipzig", "Allemagne"))
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Print the list of death countries and the number of deceased musicians
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"),
Musician("Mozart", "Wolfgang", "1756-01-27", "Salzbourg", "Allemagne", "1791-12-05", "Vienne", "Autriche"),
Musician("Dvořák", "Antonin", "1841-09-08", "Prague", "République Tchèque", "1904-05-01", "Prague", "République Tchèque"),
Musician("Rossini", "Gioachino", "1792-02-29", "Pesaro", "Italie", "1868-11-13", "Paris", "France"),
Musician("Haydn", "Joseph", "1732-03-31", "Rohrau", "Autriche", "1809-05-31", "Vienne", "Autriche"),
Musician("Mendelssohn", "Félix", "1809-02-03", "Hambourg", "Allemagne", "1847-11-04", "Leipzig", "Allemagne"),
Musician("Tchaïkovski", "Piotr", "1840-05-07", "Votkinsk", "Russie", "1893-11-06", "Saint-Pétersbourg", "Russie"),
Musician("Liszt", "Franz", "1811-10-22", "Raiding", "Autriche", "1886-07-31", "Bayreuth", "Allemagne"),
Musician("Salieri", "Antonio", "1750-08-18", "Vérone", "Italie", "1825-05-07", "Vienne", "Autriche"),
Musician("Schumann", "Robert", "1810-06-08", "Zwickau", "Allemagne", "1856-07-29", "Bonn", "Allemagne"),
Musician("Schubert", "Franz", "1797-01-31", "Vienne", "Autriche", "1828-11-19", "Vienne", "Autriche"),
Musician("Debussy", "Claude", "1862-08-22", "Saint-Germain-en-Laye", "France", "1918-03-25", "Paris", "France"),
Musician("Wagner", "Richard", "1813-05-22", "Leipzig", "Allemagne", "1883-02-13", "Venise", "Italie"),
Musician("Van Beethoven", "Ludwig", "1770-12-16", "Bonn", "Allemagne", "1827-03-29", "Vienne", "Autriche"),
Musician("Strauss", "Richard", "1864-06-11", "Munich", "Allemagne", "1949-09-08", "Garmisch-Partenkirchen", "Allemagne"),
Musician("Strauss", "Johann", "1825-10-25", "Vienne", "Autriche", "1899-06-03", "Vienne", "Autriche"),
Musician("Vivaldi", "Antonio", "1678-03-04", "Venise", "Italie", "1741-07-28", "Vienne", "Autriche"),
Musician("Verdi", "Giuseppe", "1813-10-10", "Roncole", "Italie", "1901-01-27", "Milan", "Italie"),
Musician("Bach", "Jean-Sébastien", "1685-03-31", "Eisenach", "Allemagne", "1750-07-28", "Leipzig", "Allemagne"))
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Print the musicians ordered by their death age. Who was the youngest and who was the oldest ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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"),
Musician("Mozart", "Wolfgang", "1756-01-27", "Salzbourg", "Allemagne", "1791-12-05", "Vienne", "Autriche"),
Musician("Dvořák", "Antonin", "1841-09-08", "Prague", "République Tchèque", "1904-05-01", "Prague", "République Tchèque"),
Musician("Rossini", "Gioachino", "1792-02-29", "Pesaro", "Italie", "1868-11-13", "Paris", "France"),
Musician("Haydn", "Joseph", "1732-03-31", "Rohrau", "Autriche", "1809-05-31", "Vienne", "Autriche"),
Musician("Mendelssohn", "Félix", "1809-02-03", "Hambourg", "Allemagne", "1847-11-04", "Leipzig", "Allemagne"),
Musician("Tchaïkovski", "Piotr", "1840-05-07", "Votkinsk", "Russie", "1893-11-06", "Saint-Pétersbourg", "Russie"),
Musician("Liszt", "Franz", "1811-10-22", "Raiding", "Autriche", "1886-07-31", "Bayreuth", "Allemagne"),
Musician("Salieri", "Antonio", "1750-08-18", "Vérone", "Italie", "1825-05-07", "Vienne", "Autriche"),
Musician("Schumann", "Robert", "1810-06-08", "Zwickau", "Allemagne", "1856-07-29", "Bonn", "Allemagne"),
Musician("Schubert", "Franz", "1797-01-31", "Vienne", "Autriche", "1828-11-19", "Vienne", "Autriche"),
Musician("Debussy", "Claude", "1862-08-22", "Saint-Germain-en-Laye", "France", "1918-03-25", "Paris", "France"),
Musician("Wagner", "Richard", "1813-05-22", "Leipzig", "Allemagne", "1883-02-13", "Venise", "Italie"),
Musician("Van Beethoven", "Ludwig", "1770-12-16", "Bonn", "Allemagne", "1827-03-29", "Vienne", "Autriche"),
Musician("Strauss", "Richard", "1864-06-11", "Munich", "Allemagne", "1949-09-08", "Garmisch-Partenkirchen", "Allemagne"),
Musician("Strauss", "Johann", "1825-10-25", "Vienne", "Autriche", "1899-06-03", "Vienne", "Autriche"),
Musician("Vivaldi", "Antonio", "1678-03-04", "Venise", "Italie", "1741-07-28", "Vienne", "Autriche"),
Musician("Verdi", "Giuseppe", "1813-10-10", "Roncole", "Italie", "1901-01-27", "Milan", "Italie"),
Musician("Bach", "Jean-Sébastien", "1685-03-31", "Eisenach", "Allemagne", "1750-07-28", "Leipzig", "Allemagne"))
fun main(args: Array<String>) {
TODO()
}
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content