Last active
March 28, 2022 19:23
-
-
Save uriberma/1914eb5b59fc0e3afe61fa0d01c8ee64 to your computer and use it in GitHub Desktop.
Scope Functions in Kotlin
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
import java.time.LocalDate | |
import java.time.Period | |
import kotlin.system.exitProcess | |
fun main() { | |
println("Please insert your birthday in this format: yyyy-MM-dd") | |
readLine().takeUnless { | |
it.isNullOrBlank() || it.isEmpty() | |
}?.let { | |
LocalDate.parse(it) | |
}.apply { | |
if (this == null) { | |
println("The date is not valid") | |
exitProcess(1) | |
} | |
}?.also { | |
println("You wrote $it") | |
}.run { | |
with(Period.between(this, LocalDate.now())) { | |
println("The difference between the date you wrote and today is ${this.years}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment