Skip to content

Instantly share code, notes, and snippets.

@bastman
Created August 11, 2018 08:07
Show Gist options
  • Save bastman/ef7a6a02d266dfc79b27f58b61a60b65 to your computer and use it in GitHub Desktop.
Save bastman/ef7a6a02d266dfc79b27f58b61a60b65 to your computer and use it in GitHub Desktop.
FP forwardCompose: andThen, andAlso
/**
* 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