Created
August 11, 2018 08:07
-
-
Save bastman/ef7a6a02d266dfc79b27f58b61a60b65 to your computer and use it in GitHub Desktop.
FP forwardCompose: andThen, andAlso
This file contains 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: forward compose | |
* | |
* see: | |
* - https://discuss.kotlinlang.org/t/pipe-forward-operator/2098/43 | |
* - https://hackernoon.com/funktionale-function-composition-for-kotlin-1f6e3e3c3a83 | |
* | |
* val composition = ::sqrt thenAlso ::println then ::sqrt thenAlso ::println | |
* val result:Double = composition(16.0) | |
*/ | |
infix fun <A, B, C> ((A) -> B).then(other: (B) -> C): (A) -> C = { other.invoke(this.invoke(it)) } | |
infix fun <A, B> ((A) -> B).thenAlso(other: (B) -> Unit): (A) -> B = this.then { it.also(other) } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment