

Ask HN: Any examples of currying that's not toy stuff? - coldtea

I mean, we&#x27;ve all seen the &quot;add N to value&quot; kind of examples.<p>Are there also any uses for the real world, or does it only apply to a specific kind of programming and is only relevant there?
======
NickPollard
The problem is that currying is useful for so many things that this is a bit
like asking 'what is addition useful for?'

Currying can always be implemented just by lambdas, so it does not actually
enable any new behaviour, but it makes a lot of things simpler - any time you
want to close over values, it can normally be neatened by using currying.

Let's say you're making a 3D renderer. You have a list of vertices (points) in
object space, and you want to transform them to screenspace by multiplying by
a modelview matrix and then a projection matrix.

Without currying:

    
    
      vectors fmap (\x -> multiply modelview x ) fmap (\x -> multiply projection x)
    

With currying:

    
    
      vectors fmap (multiply modelview) fmap (multiply projection)

------
cgore
I'll use a C example, GUI toolkits. Look at the code example for GTK+ at:

[http://zetcode.com/tutorials/gtktutorial/gtklayoutmanagement...](http://zetcode.com/tutorials/gtktutorial/gtklayoutmanagement/)

All of the calls to gtk_fixed_put are of the form:

    
    
        gtk_fixed_put(GTK_FIXED(fixed), button1, 150, 50);
    

For most applications, the leftmost argument is going to be exactly the same
window/buffer/menu/etc. for several calls in a row. A shortcut version like
this is nice if you are going to add 25 buttons to the same widget:

    
    
        curried_put(button1, 150, 50);
    

You can do this sort of thing with curry, making a shortcut function.

------
bjourne
Partial application is syntactic sugar over lambdas. For example, say you have
a print_with_color function:

    
    
        print_with_color(color.RED, level.INFO, "some message")
    

But it's to long to type, each time you want to print a message, so you define
some wrappers:

    
    
        info=lambda m: print_with_color(color.GRAY, level.INFO, m)
        error=lambda m: print_with_color(color.RED, level.ERROR, m)
        warning=lambda m: print_with_color(color.YELLOW, level.WARNING, m)
        info('hi im an info')
    

Which works, but it is perhaps nicer to use partial:

    
    
        info=partial(print_with_color, color.GRAY, level.INFO)
        error=partial(print_with_color, color.RED, level.ERROR)
        warning=partial(print_with_color, color.YELLOW, level.WARNING)
        info('hi im an info')
    

You could also use it on objects:

    
    
        std=partial(stdlog.log, level.INFO)
        std('hi')
        err=partial(errlog.log, level.ERROR)
        err('bad')
    

Partial application saves you more in languages with syntactic support for
them. There api producers design their functions with partial application in
mind, unlike python where you often wish for an rpartial function because you
want to partially apply from "the other side."

------
chriswarbo
We can think of methods in Object Oriented programming as functions with their
first argument (self/this/etc.) curried.

This is a very limited form of currying though, since a) only the first
argument gets curried and b) in order to access these curried functions
(methods), we have to qualify them with the object they're inside. This pretty
much 'cancels out' any syntactic saving we might have hoped for.

For example, compare these two implementations of the same algorithm:

    
    
        foo = function(this, x) { run(this, x, x); }
        foo(obj, 10);
    
        class Bar {
          function bar(x) { this.run(x, x); }
        }
        obj.bar(10);
    

The "foo" implementation requires us to pass in an explicit object parameter
like "obj", which is then explicitly passed along to the "run" function.
However, the "bar" functions are wrapped in objects, so we must specify which
one we want in order to call it (eg. "obj.bar"). Likewise, inside "bar" we
must look up the "run" function inside "this", so we've not actually gained
anything here; we've just shuffled the tokens around!

------
fakenBisEsRult
Dependency injection using currying would be some real-world example.

~~~
pkinsky
Can you link to an example? I've never heard of this technique and I'd like to
learn more.

~~~
chriswarbo
This has a fancy name: The Reader Monad.

Don't be scared though! Essentially it's a way of composing functions
together, like a(b(c(d(...)))), but it 'skips' the first argument of every
function, leaving a 'hole'. We get back a function which plugs its argument
into all of these holes, ie. dependency injection!

I found these links useful for understanding the technique:

[http://lambdaman.blogspot.co.uk/2007/10/monadreader.html](http://lambdaman.blogspot.co.uk/2007/10/monadreader.html)
[http://stackoverflow.com/questions/14178889/reader-monad-
pur...](http://stackoverflow.com/questions/14178889/reader-monad-purpose)

~~~
pkinsky
Oh, cool! I've seen examples of the Reader monad in Scala, but we use the Cake
pattern for dependency injection (self types and multiple inheritance)

[http://www.cakesolutions.net/teamblogs/2011/12/19/cake-
patte...](http://www.cakesolutions.net/teamblogs/2011/12/19/cake-pattern-in-
depth/)

------
collyw
I had this feeling for a while, then I notice some places it fitted into
Django - for creating actions. I wrote it up.

[http://colinkingswood.blogspot.com.es/2012/04/currying-to-
dy...](http://colinkingswood.blogspot.com.es/2012/04/currying-to-dynamically-
poulate-django.html)

