Skip to content

Instantly share code, notes, and snippets.

@edersonbadeca
Last active January 30, 2020 14:08
Show Gist options
  • Save edersonbadeca/157e0fcff20d34b2f7f1f4a07a807d19 to your computer and use it in GitHub Desktop.
Save edersonbadeca/157e0fcff20d34b2f7f1f4a07a807d19 to your computer and use it in GitHub Desktop.
// Example of pipeline process written in kotlin
fun multiplyIt(x:Int): Int {
println(x * 2)
return x * 2
}
fun plusTwo(x: Int): Int {
println(x + 2)
return x + 2
}
fun plusOne(x: Int): Int {
println(x + 1)
return x + 1
}
fun justPutAnError(x: Int): Int {
throw Exception("Deu ruim")
}
fun <T> T.tasks(vararg functions: (T) -> T) {
functions.fold(this) { value, f ->
try {
f(value)
} catch (e: Exception) {
throw e
}
}
}
fun main() {
val x = 10
x.tasks(
::plusOne,
::plusTwo,
::multiplyIt,
::justPutAnError
)
}
@edersonbadeca
Copy link
Author

You can test it with this link
https://pl.kotl.in/SsivPZAgh
tks @marcusvx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment