Skip to content

Instantly share code, notes, and snippets.

@a-dminator
Created February 7, 2017 22:12
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 a-dminator/da218f37161c369aae6fb51ea2aa26f0 to your computer and use it in GitHub Desktop.
Save a-dminator/da218f37161c369aae6fb51ea2aa26f0 to your computer and use it in GitHub Desktop.
val validators = mapOf(
String::class to { s: String -> s != "123" })
inline fun <vararg T, R> validate(vars: tuple<T>, action: (tuple<T>) -> R): R? =
if (vars.all { validators(it::class)(it) })

action(vars)
else
null
get("/methor") { request, _ ->
validate(
request.queryParams("key1"),
request.queryParams("key2")) { (v1, v2) ->
// do something
}
}
inline fun <vararg T, R> guard(vars: tuple<T>, action: (tuple<T>) -> R): R? =
if (vars.all { it != null })
action(vars)
else
null
val sum = guard(1, 2, null) { (a, b, c) ->
a+b+c
} ?: return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment