1

From what I understood, Applicatives are classes which implement the method apply, but I've seen a version with functions too. This is what it should look like:

fun <T, R> List<T>.ap(fab: List<(T) -> R>): List<R> = fab.flatMap { f -> this.map(f) }

And, when I am testing it with:

fun main(){
    val numbers = listOf(75, 454, 7, 45, 45, 56, 75)
    val functions = listOf<(Int) -> Int>({ i -> i * 2 }, { i -> i + 3 })
    val result = numbers.ap(functions).joinToString()
    println(result)
}

The output is:

150, 908, 14, 90, 90, 112, 150, 78, 457, 10, 48, 48, 59, 78

But the expected output is:

153, 911, 17, 93, 93, 115, 153, 81, 460, 13, 51, 51, 62, 81

Basically, I am applying a list of functions to a normal list, that's what it should do. From what I observed, my applicative did his job only for the first function, but for the other, it didn't... How can I get the expected result, using applicatives? I would like to keep my list of functions the way it is already, or at least to keep it similar at most.

RidiX
  • 819
  • 6
  • 9

0 Answers0