Fun with Generics - Currying - Jon Galloway

Fun with Generics - Currying

Sriram writes about aninteresting use of C# 2.0 Generics to implement "Currying," a technique which is normally reserved for functional programming languages.

Currying is the use of virtual functions which fix an function argument to a value and remove the argument:

  In computer science, currying is the technique of transforming a function taking multiple arguments into a function that takes a single argument (the first of the arguments to the original function) and returns a new function that takes the remainder of the arguments and returns the result. The technique was named by Christopher Strachey after logician Haskell Curry, though it was invented by Moses Schönfinkel and Gottlob Frege.

Intuitively, currying says "if you fix some arguments, you get a function of the remaining arguments". So if you take the function in two variables yx, and fix y = 2, then you get the function in one variable 2x.

[Wikipedia]

It seems a lot more academic than practical to me, but it's interesting to see what the kind of thing that C# Generics will enable.

Published Friday, September 09, 2005 5:38 AM by Jon Galloway
Filed under:

Comments

# re: Fun with Generics - Currying

It'll take me awhile to grok that I think. :) Thanks!

Friday, September 09, 2005 3:08 AM by Chris Martin

# re: Fun with Generics - Currying

pffft....

Been there, done that (in JavaScript no less). http://www.svendtofte.com/code/curried_javascript/

A practical use of currying: http://www-igm.univ-mlv.fr/~lecroq/string/node18.html

Friday, September 09, 2005 8:25 AM by Charles Chen

# re: Fun with Generics - Currying

Charles -
Thanks for the example. The Wikipedia article called Javascript as one of the languages which would support Currying since it treats functions as first class objects.

Javascript is pretty powerful language. A lot of people write it off as "just a scripting language," which is too bad.

Friday, September 09, 2005 10:54 AM by Jon Galloway

# re: Fun with Generics - Currying

Just for educational purposes...show me an example in javascript, and how it couldn't be done in C#...having a hard time understanding the concept :-).

Sunday, September 11, 2005 5:05 AM by jayson knight

# re: Fun with Generics - Currying

Jayson -
Check out the article Charles linked to so you can see the Javascript example:
http://www.svendtofte.com/code/curried_javascript/

Excerpt:
alert(myFunc(2,2)); // alerts 4
var adds4 = myFunc(4); // adds4, is now a function,
// which adds 4, to it's argument.
alert(adds4(5)); // alerts 9.

So in the above example, we're creating a virtual function inline - adds4. adds4(x) is really just an alias for MyFunc(4,x). We could have done that in a loop from 1 to 1000, creating 1000 virtual functions which all just call the original function with their fixed parameter.

You couldn't do that in C# without resorting to something like Reflection.Emit, whereas Javascript could handle it natively.

Sunday, September 11, 2005 2:17 PM by Jon Galloway

Leave a Comment

(required) 
(required) 
(optional)
(required)