Skip to content

Instantly share code, notes, and snippets.

@uriberma
Last active March 28, 2022 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uriberma/1914eb5b59fc0e3afe61fa0d01c8ee64 to your computer and use it in GitHub Desktop.
Save uriberma/1914eb5b59fc0e3afe61fa0d01c8ee64 to your computer and use it in GitHub Desktop.
Scope Functions in Kotlin
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