tags:

views:

268

answers:

4

Please interpret this code :

public static Func<TInput1, TOutput> Curry<TInput1, TOutput>(this Func<TInput1, TOutput> f)
{
    return x => f(x);
}

OR

Func<Int32, Int32> SubtractOne = x => x - 1;

what is the name of these techniques?

+1  A: 
var result = SubtractOne(5);
Assert.AreEqual(4, result);
Mel Gerats
+3  A: 

This is a new language feature called a lambda expression.

The second one makes a function that takes an Int32 named x and returns an Int32 equal to x - 1, then assigns that function to a variable of a delegate type named Func<Int32, Int32>.

It's equivalent to the following C# 1.0 code:

delegate int MyFunc(int x);
static int SubtractOneFunction(int x) { return x - 1; }

MyFunc SubtractOne = new MyFunc(SubtractOneFunction);
SLaks
Well, in that case it's equivalent... but not in the general case, since lambda expressions (and C# 2.0 anonymous delegates) can capture local variables, which wasn't possible in C# 1.0
Thomas Levesque
+8  A: 

The first sample is effectively a no-op as the input function is already in curried form; it just adds an extra level of indirection to the call. If you're interested in currying then have a look at the information on Wikipedia as a starting point.

The second sample creates a lambda expression named SubtractOne which will subtract one from the argument it is passed.

Greg Beech
+3  A: 

The first snippet would only make sense if accompanied by several similar overloads. It's probably just to complete a set of methods called Curry. This word comes from the name Haskell Curry and refers to the ability in a programming language (or library) to supply a subset of the required parameters to a function in order to obtain another function that accepts the rest of the functions. It's easier to understand with an example:

void Foo(int a, string b, bool c) { ... }

You can call this supplying all three parameters:

Foo(123, "hi", false);

But in currying, you can do this:

var foo123 = Foo(123);

It gives you back another function that takes the remaining two parameters, so we say that it "binds" the first parameter to the value 123. But if you can curry with the original function, you can curry with the new one:

var foo123Hi = foo123("hi");

And finally supply the last argument:

foo123Hi(false);

At last we have all three arguments and only now does our definition of Foo actually execute for real.

In many functional languages (unsurprisingly in the languages Haskell and Curry) this is built into the language. In C# it is not, although you can partly simulate it by providing a set of overloads such as:

Action<string, bool> Foo(int a)
{
    return (b, c) => Foo(a, b, c);
}

Action<bool> Foo(int a, string b)
{
    return c => Foo(a, b, c);
}

But this still isn't quite right, because in the first example the returned Action<string, bool> will not be directly curry-able.

So there are sometimes attempts to provide a currying library that will enable currying on any function.

But with a sufficiently simple lambda syntax it is not actually clear that any library solution will be that helpful. After all, a lambda is a very simple way to bind one parameter, as shown in the above examples. So I'm not sure how widely used a "Curry" method library would be. The => operator is clearer, less clunky, more powerful and readable, and already built-in.

Daniel Earwicker
+1 Thank you for your nice example.
masoud ramezani