Skip to content

Instantly share code, notes, and snippets.

@mierzejk
Created November 16, 2023 14:09
Show Gist options
  • Save mierzejk/e912bfba10b5698d688ec30900d7c6b2 to your computer and use it in GitHub Desktop.
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.
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