Tennent’s Principle of Correspondence
After watching the incredibly insightful Adventures in Functional Programming I was intrigued by the different refactoring tools he used to arrive at the Y Combinator.
The first of which was Tennent’s Principle of Correspondence. In my research I’ve found two different explanations of it. First from Neal Gafter’s Blog: “The principle dictates that an expression or statement, when wrapped in a closure and then immediately invoked, ought to have the same meaning as it did before being wrapped in a closure.”
Second from Techscursion, “for any parameter mechanism, an analogous definition mechanism is possible, and vice versa.”
What About Performance?
Now I was curious, is there a performance difference between the two implementations? Here’s a jsperf to examine the performance currying and of calling the curried function: Imperative vs Functional.
As of writing this, I ran the test 10 times each in Chrome 20, Firefox 10 and IE 9.
- Imperative Currying was about 8% faster than Functional
- Calling the curried function was nearly identical.
- Overall the performance was more than double the others
- Functional Currying was actually almost 4% faster
- Calling the imperatively curried function was 11% faster though.
- Functional Currying was a whopping 33% faster in IE 9
- Calling the imperatively curried function was, once again, 11% faster.
This and Return