val newFunction: (a: Int, b: Int) -> Unit = ::sum andThen ::format andThen ::printResult
infix fun <A, B, C, D> ((A, B) -> C).andThen(f: (C) -> D): (A, B) -> D = { a, b ->
f(this(a, b))
}
j
jtonic
04/06/2018, 9:28 AM
Thank you Simon for the example.
I tried it but I have a compilation error in the newFunction declaration, first andThen
Error:(35, 59) Kotlin: Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline infix fun <IP, R> (() -> @ParameterName Int).andThen(crossinline f: (a: Int) -> String): () -> String defined in arrow.syntax.function
public inline infix fun <P1, IP, R> ((???) -> @ParameterName Int).andThen(crossinline f: (a: Int) -> String): (???) -> String defined in arrow.syntax.function
s
simon.vergauwen
04/06/2018, 9:35 AM
Did you add
andThen
from the snippet?
This works fine for me.
Copy code
fun sum(a: Int, b: Int) = a + b
fun format(a: Int) = "Result: $a"
fun printResult(result: String) = println(result)
infix fun <A, B, C, D> ((A, B) -> C).andThen(f: (C) -> D): (A, B) -> D = { a, b ->
f(this(a, b))
}
fun main(args: Array<String>) {
(::sum andThen ::format andThen ::printResult)(4, 3)
}
👍 1
j
jtonic
04/06/2018, 9:42 AM
Thank you, Simon.
Yes. It works. And it looks amazing compared to pipe2, in terms of readability.
I am wondering if it makes sense to have at least a few variants of this in arrow library.