Christoph’s Blog

Rants, discoveries and subtleties that my children should never learn about

If only I had studied partially… err… partials

without comments

I have to admit it. Like millions of people everywhere I wasted my best years sitting in dusty lectures learning about 20 year old technology. (The technological elite of this country seriously learned from books where they talked about green CRTs and that color tubes are about to be invented… and that was in the 1990s.) The evilest lecture was called “theoretical basics of programming”. You learned about concepts that you would forget the instant that the semester ended. It was just a pretence to throw out half of the students. At least I had expected to learn something about programming. What did I learn? LISP, Assembler and Modula-2. Those languages are arguably not very useful. Okay, I understood the difference of functional and imperative programming languages. But later in my studies I found myself with no knowledge about object-oriented programming or at least C.

Last week most of my “master of computer science” arrogance suddenly dropped to zero when people in #pylons told me about “currying” and “partials”. I assumed to be an educated programmer but a dozen people flooded my IRC window with supportive comments like “that’s one of the most common paradigms besides recursion - didn’t you know that?“. Neither the Wikipedia page nor Pythons PEP309 really made sense at first. But now I understood the principle and it has already come handy…

A partial is just a function with preset parameters. Imagine that you have a function that takes a certain number of arguments. And you need to call that function time and again in your program. You may discover that one of the parameters always needs to be set to a certain default. How do you do that? Well, usually you pass that parameter along with every function call. With a partial you create a new function from the old function with certain parameters set. Say you have:

  • myfunc(foo=42, a=3, b=51, c=58)
  • myfunc(foo=42, z=12)
  • myfunc(foo=42, j=’hello world’)

You can create a new function with that foo=42 parameter already set:

  • myfunc2 = partial(myfunc, foo=42)

Now you can use that newly function and don’t have to care about foo=42 any longer:

  • myfunc2(a=3, b=51, c=58)
  • myfunc2(z=12)
  • myfunc2(j=’hello world’)

This may look ridiculous. But I already found my first use case for that. I needed to set certain arguments and then passed that newly created function around. Another part of the program would then just execute my function with the parameters I gave it
while adding further parameters.

The word partial is not very descriptive. But the word currying makes a lot more sense. You start with an empty pot on your oven and add ingredients (parameters). No idea if that’s the right metaphor but at least that tastes like I can remember that. :)

Getting back to the topic: my mentors at university tried to persuade me that studying is a good idea because you learn a lot about concepts that come handy for your job later. While I tend to believe that partially… why do I work with studied mathematicians and physicians in a network administration department? And most Ph.D.s I know at my employer create slideshows and emit buzzwords. Wasted time I tell you.

Written by Christoph Haas

February 19th, 2007 at 11:55 pm

Posted in Open-Source, Work

Leave a Reply