Differences between the currying and partial application

There is some confusion between currying and partial application. Some authors treat them as synonymous, but they are different:

import arrow.syntax.function.curried
import arrow.syntax.function.invoke

fun main(args: Array<String>) {
val strong: (String, String, String) -> String = { body, id, style -> "<strong id="$id" style="$style">$body</strong>" }

println(strong.curried()("Batman Begins")("trilogy1")("color:black")) // Curried

println(strong("The Dark Knight")("trilogy2")("color:black")) // Fake curried, just partial application

println(strong(p2 = "trilogy3")(p2 = "color:black")("The Dark Knight rises")) // partial application
}

The differences are significant and they can help us to decide when to use one or the other:

Currying

Partial application

Return value

When a function of arity N gets curried, it returns a chain of functions of N size, (curried form).

When a function or arity N gets partially applied, it returns a function of arity N - 1.

Parameter application

After curried, only the first parameter of the chain can be applied.

Any parameter can be applied in any order.

Reverting

It is possible to take a function on the curried form and revert it to a multi-parameter function.

As partial application doesn't change the function form, reverting is not applicable.

 

Partial application can be more flexible, but some functional styles tend to favor currying. The important thing to grasp is that both styles are different and both are supported by Arrow.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.86.18