Created
November 16, 2023 14:09
-
-
Save mierzejk/e912bfba10b5698d688ec30900d7c6b2 to your computer and use it in GitHub Desktop.
Kotlin pipe forward operator implemented with an infix function, featuring classic functional paradigms like partial functions and function currying.
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
typealias id<T> = (T) -> T | |
typealias rec_bin<T> = T.(T) -> T | |
inline infix fun<T> T.`|`(fnc: id<T>) = this.let(fnc) | |
inline infix fun<T> id<T>.`|`(crossinline fnc: id<T>): id<T> = { this(it) `|` fnc } | |
fun<T> partial(op: rec_bin<T>, operand: T): id<T> = { it.op(operand) } | |
val addCinque = partial(Int::plus, 5) | |
val negative = partial(Int::times, -1) | |
fun main() { | |
val pipe = addCinque `|` negative | |
println(4 `|` negative `|` pipe) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment