Search code examples
kotlinarrow-kt

Is there any function like ap2, ap3 in arrow-kt?


I saw scala code using cats in this post.

val a = Some(7)
val b = Some(9)
Applicative[Option].ap2(Some(add))(a,b)

And I tried migrating this code to kotlin and arrow like following.

Option.applicative()
        .tupled(Some(7), Some(9))
        .ap(Some(::add))

// works but dirty
fun add(tuple: Tuple2<Int, Int>): Int = tuple.a + tuple.b

// not work, compilation error
// fun add(a: Int, b: Int): Int = a + b

As you noticed, Tuple2 must be specified in the add function signature. I searched the official document of arrow, but there is no apN function like ap2, ap3, ap4.

Is there any way to use the second function which not included Tuple2 type?


Solution

  • Once version 0.10 is available Arrow will have a .tupled() method on function types that handles this, so you will be able to write:

    Option.applicative()
          .tupled(Some(7), Some(9))
          .ap(::add.tupled())
    
    fun add(a: Int, b: Int) = a + b
    

    for functions of up to 22 arguments.