Skip to content

Instantly share code, notes, and snippets.

@felipecsl
Last active December 24, 2017 13:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipecsl/2aabea743edeaac56cf50b9d5531e8b7 to your computer and use it in GitHub Desktop.
Save felipecsl/2aabea743edeaac56cf50b9d5531e8b7 to your computer and use it in GitHub Desktop.
Kotlin unless like Ruby just for fun
inline fun <T> unless(stmt: () -> Boolean, block: () -> T, noinline elseBlock: (() -> T)? = null) =
if (!stmt.invoke()) {
block()
} else {
elseBlock?.invoke()
}
// Use like:
unless({ foo == bar }, {
doSomething()
}, {
doSomethingElse()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment