Last active
December 24, 2017 13:12
-
-
Save felipecsl/2aabea743edeaac56cf50b9d5531e8b7 to your computer and use it in GitHub Desktop.
Kotlin unless like Ruby just for fun
This file contains hidden or 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
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