Skip to content

Instantly share code, notes, and snippets.

View hhariri's full-sized avatar

Hadi Hariri hhariri

View GitHub Profile
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;
}
fun <T> Collection<T>.filter(predicate: (T) -> Boolean) : List<T>
fun topUSandUK_v4(albums: List<Album>): List<Album> {
return albums.filter {
(it.chartUK == 1 && it.chartUS == 1)
}
}
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;
}
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;
}
fun topUSandUK_v1(albums: List<Album>): List<Album> {
val hits = arrayListOf<Album>()
for (i: Int in 0..albums.count()-1) {
if (albums[i].chartUK == 1 && albums[i].chartUS == 1) {
hits.add(albums[i])
}
}
return hits;
}
data class Album(val title: String, val year: Int, val chartUK: Int, val chartUS: Int)
val albums = listOf(
Album("The Dark Side of the Moon", 1973, 2, 1),
Album("The Wall", 1979, 3, 1),
Album("Wish You Were Here", 1975, 1, 1),
Album("Animals", 1977, 2, 3),
Album("The Piper at the Gates of Dawn", 1967, 6, 131),
Album("The Final Cut", 1983, 1, 6),
Album("Meddle", 1971, 3, 70),
<input type="button" value="Calculate" onclick="alert(Kotlin.modules.basic.org.sample.calculateTax(200.0))">
fun calculateTax(amount: Double): Double {
return amount + amount * 0.21
}
<!DOCTYPE html>
<html>
<head>
<title>This page is manipulated with Kotlin</title>
</head>
<body>
<input type="text" id="email">
<script src="js/lib/kotlin.js"></script>
<script src="js/app/basic.js"></script>
</body>