Interpret this code

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 methods?

+4
source share
4 answers

The first fragment will only make sense if it is accompanied by several similar overloads. 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 provide a subset of the required parameters for a function to get another function that takes the rest of the functions. This is easier to understand with an example:

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

You can call this by specifying all three parameters:

 Foo(123, "hi", false); 

But in currying you can do this:

 var foo123 = Foo(123); 

It returns another function that takes the remaining two parameters, so we say that it β€œbinds” the first parameter to 123. But if you can curry with the original function, you can curry with the new one:

 var foo123Hi = foo123("hi"); 

And finally, put the last argument:

 foo123Hi(false); 

Finally, we have all three arguments, and only now our definition of Foo for real is really satisfied.

In many functional languages ​​(not surprisingly in Haskell and Curry), this is built into the language. This is not the case in C #, although you can partially imitate 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 is still not entirely correct, because in the first example, the returned Action<string, bool> will not work directly with curry.

Thus, sometimes attempts are made to provide a currying library that will allow currying of any function.

But with a fairly simple lambda syntax, it’s actually not clear that any library solution would be useful. After all, lambda is a very simple way to bind a single parameter, as shown in the examples above. Therefore, I am not sure how widely used the Curry method library. The operator => is clearer, less awkward, more powerful and readable and already built-in.

+3
source

The first pattern is actually no-op, since the input function is already in curry; it just adds an extra level of indirectness to the call. If you're interested in currying, look at the Wikipedia information as a starting point.

The second example creates a lambda expression named SubtractOne , which subtracts one of the passed argument.

+8
source

This is a new language feature called lambda expression .

The second makes a function that takes Int32 with the name x and returns Int32 equal to x - 1 , then assigns this function a delegate type variable named Func<Int32, Int32> .

This is 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); 
+3
source
 var result = SubtractOne(5); Assert.AreEqual(4, result); 
+1
source

Source: https://habr.com/ru/post/1303459/


All Articles