This file contains hidden or 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, C: MutableCollection<in R>> Iterable<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : C |
This file contains hidden or 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 albumAndTrackLowerThanGivenSeconds_v2(durationInSeconds: Int, albums: List<Album>): List<Pair<String, String>> { | |
return albums.flatMap { | |
val album = it.title | |
it.tracks.filter { | |
it.durationInSeconds <= durationInSeconds | |
}.map { | |
Pair(album, it.title) | |
} | |
} | |
} |
This file contains hidden or 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> Iterable<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> |
This file contains hidden or 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 albumAndTrackLowerThanGivenSeconds_v1(durationInSeconds: Int, albums: List<Album>): List<Pair<String, String>> { | |
val list = arrayListOf<Pair<String, String>>() | |
albums.forEach { | |
val album = it.title | |
it.tracks.filter { | |
it.durationInSeconds <= durationInSeconds | |
}.map { | |
list.add(Pair(album, it.title)) | |
} |
This file contains hidden or 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_v2(albums: List<Album>): List<Pair<String, Int>> { | |
return albums.map { | |
Pair(it.title, it.tracks.map { it.durationInSeconds }.reduce { x, y -> x +y }) | |
} | |
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 | |
} | |
} |