Skip to content

Instantly share code, notes, and snippets.

@fgoinai
Created May 31, 2017 13:53
Show Gist options
  • Save fgoinai/21b4088a7f9883c22097f33abe08a5b8 to your computer and use it in GitHub Desktop.
Save fgoinai/21b4088a7f9883c22097f33abe08a5b8 to your computer and use it in GitHub Desktop.
fun <T: Comparable<T>> eWhen(target: T, tester: Tester<T>.() -> Unit) {
val test = Tester(target)
test.tester()
test.funFiltered?.invoke() ?: return
}
class Tester<T: Comparable<T>>(val it: T) {
var funFiltered: (() -> Unit)? = null
infix fun Boolean.then(block: () -> Unit) {
if (this && funFiltered == null)
funFiltered = block
}
fun lt(arg: T) = it < arg
fun gt(arg: T) = it > arg
fun ge(arg: T) = it >= arg
fun le(arg: T) = it <= arg
fun eq(arg: T) = it == arg
fun ne(arg: T) = it != arg
fun ct(arg: Collection<T>) = it in arg
fun ct(arg: String) = it as String in arg
fun nc(arg: Collection<T>) = it !in arg
fun nc(arg: String) = it as String !in arg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment