‘takeIf’ and ‘takeUnless’ : Kotlin

This post was originally posted at https://agrawalsuneet.github.io/blogs/takeif-and-takeunless-kotlin/ and reposted on Medium on 10th Mar 2019.
fun getLatency():Int {
return (0..30).random()
}
val networkLatency = getLatency()
println(networkLatency)
if (networkLatency < 20){
// join the game
println("can join the game with latency $networkLatency")
}
getLatency()?.takeIf{ it < 20}
?.let{
println("can join the game with latency $it")
}

Please continue reading at https://agrawalsuneet.github.io/blogs/takeif-and-takeunless-kotlin/

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store