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 computeSqrt(number: Double): Double { | |
if(number >= 0) { | |
return Math.sqrt(number) | |
} else { | |
throw RuntimeException("No negative please") | |
} | |
} |
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
var countUsersYoungerThan30WithSubscriptions = 0 | |
for (user in users) { | |
if (user.isYoungerThan30WithSubscriptions) { | |
countUsersYoungerThan30WithSubscriptions++; | |
} | |
} |
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
for (user in users) { | |
if(user.subscriptions != null) { | |
if (user.subscriptions.size > 0) { | |
var isYoungerThan30 = user.isYoungerThan30() | |
if (isYoungerThan30) { | |
countUsers++ | |
} | |
} | |
} | |
} |
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 parseProduct(response: Response?) = when (response?.code()){ | |
null -> throw ClientException("Response is null") | |
200, 201 -> mapToDTO(response.body()) | |
in 400..499 -> throw ClientException("Invalid request") | |
in 500..599 -> throw ClientException("Server error") | |
else -> throw ClientException("Error ${response.code()}") | |
} |
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 parseProduct(response: Response?): Product? { | |
if (response == null) { | |
throw ClientException("Response is null") | |
} | |
val code: Int = response.code() | |
if (code == 200 || code == 201) { | |
return mapToDTO(response.body()) | |
} | |
if (code >= 400 && code <= 499) { | |
throw ClientException("Invalid request") |
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 GetFile(val d: String, val n: String) | |
val pattern = Regex("(.+)/([^/]*)") | |
fun files(ph: String): PathParts { | |
val match = pattern.matchEntire(ph) | |
?: return PathParts("", ph) | |
return PathParts(match.groupValues[1], | |
match.groupValues[2]) |
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 PathParts(val directory: String, val fileName: String) | |
fun splitPath(path: String) = | |
PathParts( | |
path.substringBeforeLast('/', ""), | |
path.substringAfterLast('/')) |
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
class Book(val title: String?, val publishYear: Int?) | |
fun displayBookDetails(book: Book) { | |
val title = book.title | |
if (title == null) | |
throw IllegalArgumentException("Title required") | |
val publishYear = book.publishYear | |
if (publishYear == null) return | |
println("$title: $publishYear") |
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
class Book(val title: String?, val publishYear: Int?) | |
fun displayBookDetails(book: Book) { | |
val title = book.title ?: | |
throw IllegalArgumentException("Title required") | |
val publishYear = book.publishYear ?: return | |
println("$title: $publishYear") | |
} |
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
users.filter{ user -> user.job == Job.Developer } | |
.map{ developer -> developer.birthDate.dayOfMonth } | |
.filter { birthDay -> birthDay <= 10 } | |
.min() |