Read my book

I wrote books about Webpack and React. Check them out!

Friday, May 4, 2012

Partial Application is to FP as Inheritance is to OOP

While developing suite.js, a testing tool of mine, a had an epiphany of sorts. Partial application is actually more or less equivalent of the concept of inheritance in object oriented programming. In other words you go from general to specific. This little insight made me rethink the way I construct my functions.

If you don't know how partial application works yet, don't worry. It is simply a mean to construct new functions that perform some specific task. The classic example goes something like this. Suppose you have a function "add(a, b)". Using partial application you could construct a specific one such as this: "var addTen = partial(add, 10)". Now only parameter b remains free (ie. addTen(12) yields 22).

As suite.js relies on partial application quite heavily, this means that in order to test some function I'll need to make sure the parameters have been ordered from general to specific. I simply lock the generic bit and test the specific one using a suite. I know it's a small thing. Still, it's cool to have small epiphanies like these every once in a while.