<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx</link><description>A recent comment asked why Haskell programmers sometimes write C# lambdas in this style: Func&amp;lt;int, Func&amp;lt;int, int&amp;gt;&amp;gt; add = x=&amp;gt;y=&amp;gt;x+y; which is then invoked as sum = add(2)(3); because of course the first invocation returns a function that</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9803718</link><pubDate>Thu, 25 Jun 2009 18:22:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9803718</guid><dc:creator>Douglas McClean</dc:creator><description>&lt;P&gt;...except when T in your last example is a value type, in which case it fails with an Argument Exception: "Error binding to target method," as discussed at &lt;A href="http://stackoverflow.com/questions/1016033/extension-methods-defined-on-value-types-cannot-be-used-to-create-delegates-why" target=_new rel=nofollow&gt;http://stackoverflow.com/questions/1016033/extension-methods-defined-on-value-types-cannot-be-used-to-create-delegates-why&lt;/A&gt;.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Excellent point; I had forgotten about that. I've updated the text. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9804274</link><pubDate>Fri, 26 Jun 2009 00:12:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804274</guid><dc:creator>Olmo</dc:creator><description>&lt;p&gt;Really nice trick, now with extension methods it makes sense to reuse delegate _target field for &amp;nbsp;the first parameter, pretending &amp;nbsp;it's the target object of an instance method. &lt;/p&gt;
&lt;p&gt;But the really amazing thing is that you release this in 2004/5, when extension methods didn't exist. Looks like you have an advicer from the future telling you what to do next :)&lt;/p&gt;
&lt;p&gt;Nice job&lt;/p&gt;
</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9804304</link><pubDate>Fri, 26 Jun 2009 00:29:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804304</guid><dc:creator>Scott</dc:creator><description>&lt;P&gt;Apparently currying was discovered by Moses Schoenfinkel in 1924, according to &lt;A href="http://c2.com/cgi/wiki?CurryingSchonfinkelling" target=_new rel=nofollow&gt;http://c2.com/cgi/wiki?CurryingSchonfinkelling&lt;/A&gt;&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Indeed. It often happens in mathematics that the first discovery or invention of a concept is unappreciated and unknown, and only upon the second discovery does the idea gain currency. There are many examples of this; for example, Benford's Law (which I discussed some time ago in this blog) was originally discovered by Newcombe decades earlier. -- Eric&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9804310</link><pubDate>Fri, 26 Jun 2009 00:33:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804310</guid><dc:creator>Jon Skeet</dc:creator><description>&lt;P&gt;I thought that partial application and currying actually had different meanings, and that most people meant "partial application" when they said "currying". Is this not the case after all then?&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;Indeed, I am speaking informally and imprecisely in this article. "Partial application" is the act of providing too-few arguments to a function so as to somehow get as the result a function of fewer parameters which, when called with the missing arguments, gives the result of calling the original function with all the given arguments. "Currying" is the operation of taking a method of multiple arguments and reorganizing it into several nested methods of a single argument each, so that partial application "starting from the left" can happen easily. The two operations are so naturally a part of the same process that it is easy to conflate them when speaking informally.&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9804402</link><pubDate>Fri, 26 Jun 2009 01:27:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804402</guid><dc:creator>Strilanc</dc:creator><description>&lt;p&gt;Before lambda expressions came around I had a library for currying functions. &amp;nbsp;When you called Curry(someFunc, someVar) it created an instance of a class, and returned a delegate to the class' run method, which had one less argument. I also had special curry functions which would do things like take delegates instead of values and only evaluate them at call time.&lt;/p&gt;
&lt;p&gt;I was very, very happy when I finally got my hands on VB9. Lambda expressions nuked that entire boring library (90% of the content was copy-paste coding to deal with different numbers of arguments).&lt;/p&gt;
&lt;p&gt;I used the library to implement type-safe futures, another very useful abstraction. The futures themselves are relatively boring, the cool part are functions like FutureSub.Call which take normal functions and change them into functions which take and return future arguments.&lt;/p&gt;
</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9804962</link><pubDate>Fri, 26 Jun 2009 11:02:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9804962</guid><dc:creator>Marcel Popescu</dc:creator><description>&lt;p&gt;Hmm... I still don't see the &amp;quot;why&amp;quot; part :) Why do this at all? Maybe a more realistic example than &amp;quot;add two numbers in a really convoluted way&amp;quot; is needed :)&lt;/p&gt;
</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9805088</link><pubDate>Fri, 26 Jun 2009 11:37:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805088</guid><dc:creator>Masklinn</dc:creator><description>&lt;P&gt;&amp;gt; In C#, to curry away the first parameter you could simply say&lt;/P&gt;
&lt;P&gt;This is not currying, it's partial application. While related concepts they're not the same thing (and your initial link even made the difference between them, though it called partial application the more general concept when clearly it's the other way around).&lt;/P&gt;
&lt;P&gt;Currying is the action of turning a function of arity `n` into a sequence of `n` functions of arity 1. Partial application is the action of turning a function of arity `n` into a function of arity `n-k` (with k&amp;gt;0 and commonly 1) by fixing some of its parameters (usually from the front, but not necessarily). You don't curry parameters, you curry functions. So "curry away the first parameters" just doesn't make any sense, while "partially apply" does.&lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;See my reply to Jon's comment above. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9805241</link><pubDate>Fri, 26 Jun 2009 13:58:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805241</guid><dc:creator>Rene</dc:creator><description>&lt;p&gt;Probabably variation on the same theme are my &amp;quot;adapters&amp;quot; created for &amp;quot;fun&amp;quot; - some adapters are adopted from the C++ (Bind1st...) , adapter ToPredicate converts Func&amp;lt;T, bool&amp;gt; to Predicate etc.&lt;/p&gt;
&lt;p&gt;Automatic translaton of my article (original is only in czech). &amp;nbsp;&lt;a rel="nofollow" target="_new" href="http://tinyurl.com/mgxlwb"&gt;http://tinyurl.com/mgxlwb&lt;/a&gt; (warning: possible syntax &amp;nbsp;errors ( both C# and english) in the article are caused by the translation).&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Collections.Generic;&lt;/p&gt;
&lt;p&gt;using System.Linq;&lt;/p&gt;
&lt;p&gt;using System.Text;&lt;/p&gt;
&lt;p&gt;namespace FunctionExtensions&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static class FuncExtension&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T1, R&amp;gt; Bind1St&amp;lt;T0, T1, R&amp;gt;(T0 bindValue, Func&amp;lt;T0, T1, R&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (arg =&amp;gt; originalFunc(bindValue, arg));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, R&amp;gt; Bind2nd&amp;lt;T0, T1, R&amp;gt;(T1 bindValue, Func&amp;lt;T0, T1, R&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (arg =&amp;gt; originalFunc(arg, bindValue));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, bool&amp;gt; Not&amp;lt;T0&amp;gt;(Func&amp;lt;T0, bool&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (arg =&amp;gt; !originalFunc(arg));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, T1, bool&amp;gt; Not&amp;lt;T0, T1&amp;gt;(Func&amp;lt;T0, T1, bool&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ((arg1, arg2) =&amp;gt; !originalFunc(arg1, arg2));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, T1, T2, bool&amp;gt; Not&amp;lt;T0, T1, T2&amp;gt;(Func&amp;lt;T0, T1, T2, bool&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ((arg1, arg2, arg3) =&amp;gt; !originalFunc(arg1, arg2, arg3));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, T1, bool&amp;gt; And&amp;lt;T0, T1&amp;gt;(Func&amp;lt;T0, T1, bool&amp;gt; originalFunc, Func&amp;lt;T0, T1, bool&amp;gt; originalFunc2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ((arg1, arg2) =&amp;gt; originalFunc(arg1, arg2) &amp;amp;&amp;amp; originalFunc(arg1, arg2));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, bool&amp;gt; And&amp;lt;T0&amp;gt;(Func&amp;lt;T0, bool&amp;gt; originalFunc, Func&amp;lt;T0, bool&amp;gt; originalFunc2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (arg1 &amp;nbsp;=&amp;gt; originalFunc(arg1) &amp;amp;&amp;amp; originalFunc2(arg1));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, T1, bool&amp;gt; Or&amp;lt;T0, T1&amp;gt;(Func&amp;lt;T0, T1, bool&amp;gt; originalFunc, Func&amp;lt;T0, T1, bool&amp;gt; originalFunc2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ((arg1, arg2) =&amp;gt; originalFunc(arg1, arg2) &amp;nbsp;|| originalFunc(arg1, arg2));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Func&amp;lt;T0, bool&amp;gt; Or&amp;lt;T0&amp;gt;(Func&amp;lt;T0, bool&amp;gt; originalFunc, Func&amp;lt;T0, bool&amp;gt; originalFunc2)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return (arg1 =&amp;gt; originalFunc(arg1) || originalFunc2(arg1));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static Predicate&amp;lt;T&amp;gt; ToPredicate&amp;lt;T&amp;gt;(Func&amp;lt;T, bool&amp;gt; originalFunc)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return arg =&amp;gt; originalFunc(arg);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item><item><title>re: Mmm, Curry</title><link>http://blogs.msdn.com/ericlippert/archive/2009/06/25/mmm-curry.aspx#9805992</link><pubDate>Sat, 27 Jun 2009 00:21:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9805992</guid><dc:creator>Olmo</dc:creator><description>&lt;P&gt;Hi Scott,&lt;/P&gt;
&lt;P&gt;My point wasn't that they inveted currification on 2004/5, in fact i think that, when they disigned delegates they weren't thinking in curryfication at all (an academic thing that didn't make a lot of sense in a pre-Linq practical C# language): &lt;/P&gt;
&lt;P&gt;Instead they thought that an instance method shouldn't have a different signature than a static one, even if theres an extra parameter (the instance to call), so they hide it and now you can do:&lt;/P&gt;
&lt;P&gt;Func&amp;lt;int, int&amp;gt; a = myObject.Method; &lt;/P&gt;
&lt;P&gt;Func&amp;lt;int,int&amp;gt; b = MyClass.Method; &lt;/P&gt;
&lt;P&gt;This is ok.&lt;/P&gt;
&lt;P&gt;The amazing thing though is that they realize that hiding the first parameter could be interesting EVEN it this is not the instance parameter. And then, a few years later Extension methods appear, just a compiler trick to use the first parameter pretending is an instance, and now the feature makes sense. &lt;/P&gt;
&lt;P&gt;How C#/CLR guys know that this will happend? &lt;/P&gt;
&lt;DIV class=yellowbox&gt;
&lt;P&gt;The CLR guys knew that people wanted to implement functional languages on the .NET framework. Consider the problem of using existing methods in a language that emphasizes partial application everywhere. If you are writing new functions, of course you can always curry them to make partial application easier. But if you want to partially apply existing framework methods, then it is helpful to compiler writers to have a way to do that via delegates. It's even nicer if the technique is "baked in" to the framework. That this happened to mesh very nicely with extension methods was a happy accident; my understanding is that their intention was to make it easier for third-party compiler writers. -- Eric&lt;/P&gt;&lt;/DIV&gt;</description></item></channel></rss>