F#: My Understanding of Partial Application

Intro

Partial Application enables functional programmers the ability to write a consolidated expression such that they can run elements of a collection into a function that requires not just the element but other arguments as well.

Function Signatures

I have struggled with the Partial Application concept. Specifically, I just did not understand the context where it could be useful. I then stumbled upon the F# Gazette. In the artcle, Scott Wlaschin addressed my confusion. In short, I learned that Partial Application can be especially useful when applying elements of a collection to a function.

For example, let’s take the following function:

let isDivisible divisor input = input % divisor = 0

The function above is a typical function that could be deemed useful when crunching numbers.

Now what if we wanted to take a list of numbers and filter out only the numbers that are divisible by two?

Well, in F# we could do this:

let isDivisibleBy2 = isDivisible 2

The expression above represents a partial application. Specifically, we provide a function one or more of the arguments that we’re going to use. So in this example, we happen to declare a new function that references another function with some of the inputs already provided.

In order for this to work, the compiler takes our initial function signature:

let isDivisible divisor input = input % divisor = 0

and rewrites it as the following function signature:

let isDivisible divisor = fun input -> input % divisor = 0

The rewritten function signature above says the following:
IsDivisible will now return a value that takes the form of a function that will require its own argument (i.e. input) in addition to the argument that it required initially (i.e. divisor).

So we can take the compiler’s rewritten function signature:

let isDivisible divisor = fun input -> input % divisor = 0

And thus declare a new function that uses the rewritten function signature like this:

let isDivisibleBy2 = isDivisible 2

The function isDivisibleBy2 will now return a function that requires just one argument for the value that we want to divide 2 by.

We can now take advantage of the Partial Application feature by applying a partially applied function to a list of values:

List.filter isDivisibleBy2 [1..10]

Conclusion

In conclusion, Partial Application enables functional programmers the ability to write a consolidated expression such that they can run elements of a collection into a function that requires not just the element but other arguments as well.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: