devboy

learn haXe: partial function application

learnhaxe

This is the first post in my series: Learn haXe – the hard way where I will shed some light on various topics around haXe. It’s called “the hard way” because that is the route I am taking myself to learn haXe: Diving extensively into every little feature, disassembling and reassembling it until I fully understand how it works and what its use cases are.

Let’s just dive into the first topic: partial function application! It might be useful to read up some background information on functions in haXe in a previous post.

Partial function application

In case you have never heard of partial (function) application, let’s have a look at what Wikipedia has to say about it:

In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Given a function , we might fix (or ‘bind’) the first argument, producing a function of type . Evaluation of this function might be represented as fpartial(2,3). Note that the result of partial function application in this case is a function that takes two arguments.

This simply states that a partial function application is a subset of a regular function which has been partially applied, and I can guarantee that you are already using partial application, in some form, in your code today. Does the following look familiar?

function add( x: Float, y: Float ) {
return x + y;
}

function addOne( x: Float ) {
return add( 1, x );
}

The function “addOne” is actually a partial application of the function “add”.  It takes a parameter less than “add” and then applies 1 and the given parameter to “add”. Therefore “add” has been partially applied with 1.

In haXe there is no need to write these kind of functions by hand, a helper method called “callback” will generate them for you. Let’s redo the previous example, but this time with the help of “callback”.

function add( x: Float, y: Float ) {
return x + y;
}

var addOne = callback( add, 1 );

addOne( 5 ); // returns 6

As you can see, this will save you from writing some code and at the same time it will also preserve type-safety. The “callback” method will also allow you to partially apply multiple parameters of your functions, it will apply them in the order they are defined in the function (read: left to right):

function sumOfFloats( x: Float, y: Float, z: Float ) {
return x + y + z;
}

var addFloatToOneAndTwo = callback( sumOfFloats, 1, 2 );

addFloatToOneAndTwo( 5 ); // returns 8

Use cases

Partial function application can be very useful in a variety of cases. In functional programming they can actually be used to hold a state in situations where you don’t really need to create a class or value object to hold that state.

It will also be very useful when you need to pass a closure with only a subset of the parameters to a function, instead of doing the following:

function doMath( x: Float, y: Float, operation: Float -> Float -> Float ) {
return operation( x, y );
}

function swapDivide( swap: Bool, x: Float, y: Float ) {
return swap ? y / x : x / y;
}

doMath( 10, 2, function(x, y) { return swapDivide(true, x, y); } ); // returns 0.2
view raw gistfile1.as This Gist brought to you by GitHub.

you can just replace the last line with this one:

doMath( 10, 2, callback( swapDivide, true ) ); // return 0.2
view raw gistfile1.as This Gist brought to you by GitHub.

Notes

Please keep in mind that “partial application” gets mixed up a lot with “function currying”, also on the haXe mailing-list, but they are not the same. If you want to know what currying is you can read it up on Wikipedia.

I am wondering why the method to create a partially applied function is called “callback”, which implies that it is kind of an async process or function like a promise or future. Shouldn’t it instead be called “papply”?

One Response to learn haXe: partial function application

  1. Mike Cann says:

    Nice! I didnt even know HaXe had that as part of its syntax! I must admit tho I have never needed to do it, but I guess I have been trained in the Java / AS3 world rather than a functional one :P

Leave a Reply

devboy

Connect

Follow me on Twitter
Connect with me on Linked In