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?
Ask HN: Any examples of currying that's not toy stuff?
1–10 of 10 posts
Re: Ask HN: Any examples of currying that's not toy stuff?
#2Re: Ask HN: Any examples of currying that's not toy stuff?
#3Currying 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)Re: Ask HN: Any examples of currying that's not toy stuff?
#4 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."Re: Ask HN: Any examples of currying that's not toy stuff?
#5http://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.Re: Ask HN: Any examples of currying that's not toy stuff?
#6Dependency injection using currying would be some real-world example.
Re: Ask HN: Any examples of currying that's not toy stuff?
#7This 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!Re: Ask HN: Any examples of currying that's not toy stuff?
#8Dependency injection using currying would be some real-world example.
Can you link to an example? I've never heard of this technique and I'd like to learn more.
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://stackoverflow.com/questions/14178889/reader-monad-pur...
Re: Ask HN: Any examples of currying that's not toy stuff?
#9http://colinkingswood.blogspot.com.es/2012/04/currying-to-dy...
Re: Ask HN: Any examples of currying that's not toy stuff?
#10Earlier quoted context omitted.
Can you link to an example? I've never heard of this technique and I'd like to learn more.
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/monadre…
http://www.cakesolutions.net/teamblogs/2011/12/19/cake-patte...