Last active
January 30, 2020 14:08
-
-
Save edersonbadeca/157e0fcff20d34b2f7f1f4a07a807d19 to your computer and use it in GitHub Desktop.
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
// 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 | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can test it with this link
https://pl.kotl.in/SsivPZAgh
tks @marcusvx