This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sumOfInts(list: List<Int>): Int { | |
return list.reduce { (x, y) -> x + y} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun nameAndTotalTime_v1(albums: List<Album>): List<Pair<String, Int>> { | |
return albums.map { | |
var total = 0 | |
it.tracks.forEach { | |
total += it.durationInSeconds | |
} | |
Pair(it.title,total) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Track(val title: String, val durationInSeconds: Int) | |
val pinkFloyd = listOf( | |
Album("The Dark Side of the Moon", 1973, 2, 1, | |
listOf(Track("Speak to Me", 90), | |
Track("Breathe", 163), | |
Track("On he Run", 216), | |
Track("Time", 421), | |
Track("The Great Gig in the Sky", 276), | |
Track("Money", 382), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun topUSandUK_hits_years_v2(albums: List<Album>): List<Int> { | |
return albums.filter { | |
(it.chartUK == 1 && it.chartUS == 1) | |
}.map { | |
it.year | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun topUSandUK_hits_years_v1(albums: List<Album>): List<Int> { | |
val hits = albums.filter { | |
(it.chartUK == 1 && it.chartUS == 1) | |
} | |
val years = ArrayList<Int>() | |
hits.forEach { | |
years.add(it.year) | |
} | |
return years; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Collection<T>.filter(predicate: (T) -> Boolean) : List<T> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun topUSandUK_v4(albums: List<Album>): List<Album> { | |
return albums.filter { | |
(it.chartUK == 1 && it.chartUS == 1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun topUSandUK_v3(albums: List<Album>): List<Album> { | |
val hits = arrayListOf<Album>() | |
albums.forEach { | |
if (it.chartUK == 1 && it.chartUS == 1) { | |
hits.add(it) | |
} | |
} | |
return hits; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun topUSandUK_v2(albums: List<Album>): List<Album> { | |
val hits = arrayListOf<Album>() | |
for (album in albums) { | |
if (album.chartUK == 1 && album.chartUS == 1) { | |
hits.add(album) | |
} | |
} | |
return hits; | |
} |