<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2782951084319807024</id><updated>2012-02-16T10:38:55.415-08:00</updated><category term='Ximura'/><category term='C#'/><category term='F#'/><category term='.NET Framework'/><category term='programming'/><category term='Currying'/><title type='text'>Ximura</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ximura.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2782951084319807024/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ximura.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Paul Stancer</name><uri>http://www.blogger.com/profile/04928962650612000685</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2782951084319807024.post-6267491065324280299</id><published>2009-04-30T21:28:00.000-07:00</published><updated>2010-04-07T00:17:38.962-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Currying'/><category scheme='http://www.blogger.com/atom/ns#' term='.NET Framework'/><category scheme='http://www.blogger.com/atom/ns#' term='F#'/><category scheme='http://www.blogger.com/atom/ns#' term='Ximura'/><category scheme='http://www.blogger.com/atom/ns#' term='programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>Stopping off for a Curry.</title><content type='html'>&lt;p&gt;One of the positives of the global downturn in the IT industry (and probably the only one), is that I now have more time on my hands to wander off (programmatically speaking) to investigate the more esoteric areas of the C# language. &lt;/p&gt;&lt;p&gt;Yesterday, while writing &lt;a href="http://en.wikipedia.org/wiki/Unit_testing"&gt;unit-test code&lt;/a&gt; for the new Ximura lock-free collection classes, I embarked on one such walk-about in to the realms of functional programming.&lt;/p&gt;&lt;h2&gt;Currying while Hurrying&lt;/h2&gt;&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Currying"&gt;Currying&lt;/a&gt;, or as it is affectionately known – &lt;i&gt;&lt;a href="http://www.cynic.net/tsac.html"&gt;Schönfinkelisation&lt;/a&gt; (&lt;/i&gt;I wonder why that name didn’t catch on?) is at it’s simplest, the process of reducing a function with n parameters, to n-1 parameters, by wrapping one of the parameters in a holding function.&lt;/p&gt;&lt;p&gt;Complicated? Well a little, but it does allow for a lot of reuse of your functions if you do it right, and it’s a nice tool to have on your coding tool belt.&lt;/p&gt;&lt;h2&gt;Functional Iterators&lt;/h2&gt;&lt;p&gt;First off, a little diversion to look at functional iterators. Below is a brief code snippet that add 5 integers to a HashSet [note: you’re going to need Visual Studio 2008 to run these samples].&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;   &lt;pre&gt;HashSet&amp;lt;int&amp;gt; coll = new HashSet&amp;lt;int&amp;gt;&lt;int&gt;();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;int[] data = {42,46,77,345,-12};&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;foreach (var local in data)&lt;br /&gt;&lt;br /&gt;   coll.Add(local);&lt;/int&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;A more functional way to write the last statement would be like this. &lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;data.ForEach(i =&amp;gt; coll.Add(i));&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;Where the ForEach &lt;a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx"&gt;extension method&lt;/a&gt; is a the method below:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;public static void ForEach&amp;lt;T&amp;gt;(this IEnumerable&amp;lt;T&amp;gt; items, Action&amp;lt;T&amp;gt; action)&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;   foreach (var item in items)&lt;br /&gt;&lt;br /&gt;       action(item);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;You can see another example of how I used this code &lt;a href="http://99-bottles-of-beer.net/language-csharp-2041.html"&gt;here&lt;/a&gt; (the full set of these Ximura extension method can be found &lt;a href="http://code.google.com/p/ximura/source/browse/trunk/Ximura/Helper/Linq/Linq.cs"&gt;here&lt;/a&gt;).&lt;/p&gt;&lt;p&gt;Now that we have got the basics down, let’s have some curry.&lt;/p&gt;&lt;h2&gt;The Curry Monster&lt;/h2&gt;&lt;p&gt;So let’s create a simple function to print out a range of numbers, starting from an initial seed number. The traditional way to do this would be like this:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;for (int i = 0; i &amp;lt; 40; i++)&lt;br /&gt;&lt;br /&gt;   Console.WriteLine(i);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;However in out brave new functional world we would write it like this:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;Enumerable.Range(0, 40)&lt;br /&gt;&lt;br /&gt;   .ForEach(i =&amp;gt; Console.WriteLine(i));&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;Not the most exciting use of functional programming, but let’s take this further and use it to show Currying in action. First we create a function:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;Action&amp;lt;int,int&amp;gt; fnOutput = (total, start) =&amp;gt;&lt;br /&gt;&lt;br /&gt;   Enumerable.Range(start, total)&lt;br /&gt;&lt;br /&gt;       .ForEach(i =&amp;gt; Console.WriteLine(i));&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;The eagle-eyed amongst you might have noticed that I have switched the &lt;em&gt;start&lt;/em&gt; and &lt;em&gt;total &lt;/em&gt;parameters, well more on that later. Anyway,  now that we have our function we can just call it to output our list of numbers, so for instance to print out 20 to 59, and 45 to 84, we would call the following:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;fnOutput(40,20);&lt;br /&gt;&lt;br /&gt;fnOutput(40,45);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;The total parameter (value 40) is used twice, but if we wanted, we could curry this out of the way, so that it is passed automatically. To do this we would create a new function, let’s call it fnOutput40:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;Action&amp;lt;int&amp;gt; fnOutput40 = fnOutput.Curry()(40);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;Then we could just call this new function with just the start parameter, as follows, and get the same output as the total parameter of 40 is passed automatically:&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;fnOutput40(20);&lt;br /&gt;&lt;br /&gt;fnOutput40(45);&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;p&gt;So, Currying, in the right circumstances can simplify your code when dealing with complex functions. For this example, I have used a integer parameter, but any value-type or class parameter can be Curried. &lt;/p&gt;&lt;p&gt;Just taking a look at the curry function itself, you can see that it takes a function of type &amp;lt;T1,T2,TResult&amp;gt; and returns a function of type &amp;lt;T2, TResult&amp;gt; essentially wrapping the first parameter in the function so that it can be passed around within the new function.&lt;/p&gt;&lt;div style="border-bottom: black 1px solid; border-left: black 1px solid; padding-bottom: 10px; line-height: 12px; background-color: #eeeeee; margin: 0px; padding-left: 10px; width: 370px; padding-right: 10px; display: block; font-family: arial; color: red; font-size: 12px; overflow: auto; border-top: black 1px solid; font-weight: bold; border-right: black 1px solid; padding-top: 10px"&gt;&lt;br /&gt; &lt;pre&gt;public static Func&amp;lt;T1, Func&amp;lt;T2, TResult&amp;gt;&amp;gt;&lt;br /&gt;Curry&amp;lt;T1, T2, TResult&amp;gt;(this Func&amp;lt;T1, T2, TResult&amp;gt; f)&lt;br /&gt;{&lt;br /&gt;     return p1 =&amp;gt; p2 =&amp;gt; f(p1, p2);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;h2&gt;That's all folks.&lt;/h2&gt;&lt;p&gt;The full set of Ximura currying functions can be found &lt;a href="http://code.google.com/p/ximura/source/browse/trunk/Ximura/Helper/Linq/Linq_Currying.cs"&gt;here&lt;/a&gt;, they have been expanded for both functions and actions for up to 4 parameters.&lt;/p&gt;&lt;p&gt;To use them in your project you can either copy the code directly, or add the Ximura Base project as a reference and add the Ximura.Helper namespace to your class. &lt;/p&gt;&lt;p&gt;The ForEach and Currying method will be automatically suggested by &lt;a href="http://en.wikipedia.org/wiki/IntelliSense"&gt;Intellisense&lt;/a&gt; when you are coding.&lt;/p&gt;&lt;p&gt;Hope you enjoy them,&lt;/p&gt;&lt;p&gt;Paul Stancer&lt;/p&gt;&lt;p&gt;May 1st 2009&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2782951084319807024-6267491065324280299?l=ximura.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ximura.blogspot.com/feeds/6267491065324280299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ximura.blogspot.com/2009/04/stopping-off-for-curry.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2782951084319807024/posts/default/6267491065324280299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2782951084319807024/posts/default/6267491065324280299'/><link rel='alternate' type='text/html' href='http://ximura.blogspot.com/2009/04/stopping-off-for-curry.html' title='Stopping off for a Curry.'/><author><name>Paul Stancer</name><uri>http://www.blogger.com/profile/04928962650612000685</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2782951084319807024.post-849339040208029796</id><published>2009-04-16T09:07:00.001-07:00</published><updated>2009-04-16T09:49:56.303-07:00</updated><title type='text'>Work in progress / work is progressing.</title><content type='html'>&lt;p&gt;Well, work progresses on getting the full Ximura project up and running. Version 3.0 becomes more of a rewrite from version 2.0 every day (I’m sure there is some code somewhere that hasn’t been changed), but I am adding significant new functionality, better security, and implementing major performance improvements.&lt;/p&gt;  &lt;p&gt;Still, we still need a working system at some point. &lt;/p&gt;  &lt;p&gt;I’ll be posting updates regarding the various parts of the system architecture over the next few months. For those of you interested in Ximura and how it works, and the design decisions that have gone in to it, then this blog should hopefully answer most of those questions.&lt;/p&gt;  &lt;p&gt;Currently the code is compilable, but will not start due to major changes to the threading and job scheduling engines (more on that in my next post). However the code can be accessed through Subversion at &lt;a title="http://code.google.com/p/ximura/" href="http://code.google.com/p/ximura/"&gt;http://code.google.com/p/ximura/&lt;/a&gt;. Also the project has been evaluated by Ohloh &lt;a title="https://www.ohloh.net/p/ximura" href="https://www.ohloh.net/p/ximura"&gt;(https://www.ohloh.net/p/ximura&lt;/a&gt;) which has estimated our effort so far at USD $1.6 million. Obviously this means absolutely nothing, as an open source project is only valued on how useful it is to the community, but it leaves a warm fuzzy feeling that there is something there of value, even if it is based on Mickey Mouse money.&lt;/p&gt;  &lt;p&gt;In code,&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Paul Stancer.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2782951084319807024-849339040208029796?l=ximura.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ximura.blogspot.com/feeds/849339040208029796/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ximura.blogspot.com/2009/04/work-in-progress-work-is-progressing.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2782951084319807024/posts/default/849339040208029796'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2782951084319807024/posts/default/849339040208029796'/><link rel='alternate' type='text/html' href='http://ximura.blogspot.com/2009/04/work-in-progress-work-is-progressing.html' title='Work in progress / work is progressing.'/><author><name>Paul Stancer</name><uri>http://www.blogger.com/profile/04928962650612000685</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
