<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" gd:etag="W/&quot;C0ADQXw-fip7ImA9WhRVGU8.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280</id><updated>2012-01-18T18:22:50.256-03:00</updated><category term="xml" /><category term="solr" /><category term="activerecord" /><category term="teamcity" /><category term="nhwebconsole" /><category term="opengrok" /><category term="linq" /><category term="gdatadb" /><category term="yahoo pipes" /><category term="quartz" /><category term="continuous integration" /><category term="javascript" /><category term="IoC" /><category term="web" /><category term="ohloh" /><category term="figment" /><category term="GitSharp" /><category term="boo" /><category term="monads" /><category term="F#" /><category term="lenses" /><category term="open source" /><category term="http" /><category term="openx" /><category term="formlets" /><category term="android" /><category term="git" /><category term="vs2010" /><category term="functional programming" /><category term="fsharpx" /><category term="solrnet" /><category term="applicative functors" /><category term="castle" /><category term="asp.net" /><category term="monorail" /><category term="testing" /><category term="asp.net mvc" /><category term="nhibernate" /><category term="google" /><category term="ndepend" /><category term="C# 4.0" /><title>Bug squash</title><subtitle type="html">Squashing bugs all day long</subtitle><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default?start-index=6&amp;max-results=5&amp;redirect=false&amp;v=2" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>127</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>5</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/bugsquash" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="bugsquash" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DUMEQX09cCp7ImA9WhRQEU8.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280.post-3713007892648445005</id><published>2011-12-04T20:57:00.001-03:00</published><updated>2011-12-05T20:23:20.368-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-12-05T20:23:20.368-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="solrnet" /><title>SolrNet 0.4.0 beta 1 released</title><content type="html">&lt;p&gt;&lt;a href="http://code.google.com/p/solrnet/"&gt;SolrNet&lt;/a&gt; is a &lt;a href="http://lucene.apache.org/solr/"&gt;Solr&lt;/a&gt; client for .NET, and I just released version 0.4.0 beta 1.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Core functionality is stable.&lt;/li&gt;
&lt;li&gt;New features are unit-tested, but may not be battle-tested.&lt;/li&gt;
&lt;li&gt;Feature-frozen, no new features will be allowed between this beta and the final release. You can of course still send in pull requests for new features, they will be included in the next release, just not in 0.4.0; and in fact there is a pending pull request implementing sharding that I have to review, work on and eventually merge.&lt;/li&gt;
&lt;li&gt;API-frozen, unless there's some critical flaw or an obvious win with a small change.&lt;/li&gt;
&lt;li&gt;Little to no documentation about new features or changes. Tests are the primary or maybe only source of usage.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;New features&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://wiki.apache.org/solr/MoreLikeThisHandler"&gt;MoreLikeThis handler&lt;/a&gt; queries.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://wiki.apache.org/solr/TermsComponent"&gt;TermsComponent&lt;/a&gt; support.&lt;/li&gt;
&lt;li&gt;&lt;a href="http://wiki.apache.org/solr/ClusteringComponent"&gt;ClusteringComponent&lt;/a&gt; support. &lt;/li&gt;
&lt;li&gt;Optionally disable quoting in SolrQueryByField.&lt;/li&gt;
&lt;li&gt;Ngroups for result grouping.&lt;/li&gt;
&lt;li&gt;Multicore support for Autofac module.&lt;/li&gt;
&lt;li&gt;Friendlier highlighting results.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Bugfixes&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Fixed an intermitent bug in NHibernate integration.&lt;/li&gt;
&lt;li&gt;Fixed bug with LocalParams in date facets.&lt;/li&gt;
&lt;li&gt;Fixed pre/post delimiters for fastVectorHighlighter.&lt;/li&gt;
&lt;li&gt;Fixed exception when using SolrNet in Application_Start under IIS 7+&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Breaking changes&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Removed query result interfaces (ISolrQueryResults). Just replace it with SolrQueryResults in your code.&lt;/li&gt;
&lt;li&gt;Deprecated Add(IEnumerable&amp;lt;T&amp;gt;). Use AddRange() instead.&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Other stuff&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;Upgraded to NHibernate 3.2&lt;/li&gt;
&lt;li&gt;Upgraded to Autofac 2.5.2&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Contributors since 0.4.0 alpha 1&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="https://github.com/ptasz3k"&gt;Radek Krahl&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/AlmostTooCool"&gt;Rich Semenick&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/jerizm"&gt;Jerry Wang&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://paigecsharp.blogspot.com/"&gt;Paige Cook&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/stevencasey"&gt;Steven Casey&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/joneholland"&gt;Jonathan Holland&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mr-road"&gt;James Atherton&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/teun"&gt;Teun Duynstee&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/tpmorley"&gt;Tom Morley&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://plus.google.com/113231434618770588081/about"&gt;Emad Nashed&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/matejskubic"&gt;Matej Skubic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mattbraid"&gt;Matthew Braid&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Huge kudos to them all! Also thanks to &lt;a href="https://twitter.com/stephenpope"&gt;Stephen Pope&lt;/a&gt; and &lt;a href="http://paigecsharp.blogspot.com/"&gt;Paige Cook&lt;/a&gt; for picking up many questions about SolrNet in the mailing list and Stackoverflow.&lt;/p&gt;&lt;h3&gt;Download&lt;/h3&gt;&lt;p&gt;&lt;a href="http://code.google.com/p/solrnet/downloads/list"&gt;Binaries available on Google Code&lt;/a&gt; as usual.&lt;/p&gt;&lt;p&gt;If you're upgrading from 0.3.1, see also the &lt;a href="http://bugsquash.blogspot.com/2011/06/solrnet-040-alpha-1-released.html"&gt;0.4.0 alpha 1 release notes&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8643857899806162280-3713007892648445005?l=bugsquash.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/4_U9c2GA3De3Tu8l_T6rbcLmyPs/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4_U9c2GA3De3Tu8l_T6rbcLmyPs/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/4_U9c2GA3De3Tu8l_T6rbcLmyPs/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/4_U9c2GA3De3Tu8l_T6rbcLmyPs/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bugsquash/~4/OQIUQ0VmNHw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/3713007892648445005/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8643857899806162280&amp;postID=3713007892648445005" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/3713007892648445005?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/3713007892648445005?v=2" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/2011/12/solrnet-040-beta-1-released.html" title="SolrNet 0.4.0 beta 1 released" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;CkABR309eyp7ImA9WhRRFU0.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280.post-5082801826620902256</id><published>2011-11-28T14:17:00.001-03:00</published><updated>2011-11-28T14:19:16.363-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-28T14:19:16.363-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="monads" /><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="lenses" /><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><category scheme="http://www.blogger.com/atom/ns#" term="fsharpx" /><title>A few FSharpx examples</title><content type="html">&lt;p&gt;&lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt; doesn't currently have any proper documentation, and even comments are lacking despite what &lt;a href="https://www.ohloh.net/p/fsharpx/factoids/12898520"&gt;ohloh says&lt;/a&gt;. We will definitely improve the comments, but a reference documentation isn't going to help at all if you don't know the underlying concepts and how things fit together. A good way to introduce these concepts is by using examples that deal with familiar problems. &lt;a href="http://stackoverflow.com/"&gt;Stackoverflow&lt;/a&gt; has proven to be a great source of real-world use cases for many FSharpx features, which also serve to grow it with new features. Here are the F# questions I answered last week using FSharpx I'd like to elaborate. I recommend following the links to the original questions and reading the other answers for comparison.&lt;/p&gt;  &lt;h3&gt;&lt;a href="http://stackoverflow.com/questions/8260728/list-multiplication"&gt;List multiplication&lt;/a&gt;&lt;/h3&gt;  &lt;p&gt;The question is about doing a cartesian product. Of course, in F# this is pretty trivial to achieve thanks to list comprehensions, but FSharpx has a couple of functions to help express this more directly:&lt;/p&gt;  &lt;p&gt;First there's &lt;code&gt;List.lift2&lt;/code&gt;. Its signature is &lt;code class="code"&gt;('a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'c) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a list &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b list &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'c list&lt;/code&gt; .&lt;/p&gt;  &lt;p&gt;If you're familiar with F# you'll notice this is similar to the standard function &lt;a href="http://msdn.microsoft.com/en-us/library/ee340232.aspx"&gt;List.map2&lt;/a&gt;, which has the exact same signature. So what's the difference?&lt;/p&gt;  &lt;p&gt;&lt;code&gt;List.map2&lt;/code&gt; works pairwise. Its result has the exact same number of elements as the parameters. Let's see an example:&lt;/p&gt;  &lt;pre class="code"&gt;&amp;gt; List.map2 (+) [0;1] [0;2];;

[0;3]&lt;/pre&gt;

&lt;p&gt;That is, it adds 1st element in the first list to 1st element in the second list, and 2nd to 2nd.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;List.lift2&lt;/code&gt; does precisely a cartesian product. Let's try it with the same parameters as above:&lt;/p&gt;

&lt;pre class="code"&gt;&amp;gt; List.lift2 (+) [0;1] [0;2];;

[0;2;1;3]&lt;/pre&gt;

&lt;p&gt;It adds 1st to 1st, 1st to 2nd, 2nd to 1st, 2nd to 2nd.&lt;/p&gt;

&lt;p&gt;Same signature, very different semantics.&lt;/p&gt;

&lt;p&gt;The name &lt;code&gt;lift2&lt;/code&gt; comes from lifting a function (a binary function, in this case, hence '2') to a monad (the F# list as a monad in this case). In Haskell this function is generalized to all monads and called &lt;a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html#v:liftM2"&gt;liftM2&lt;/a&gt;. In F# it's also the same as &lt;a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Applicative.html#v:liftA2"&gt;liftA2&lt;/a&gt;, which lifts a function to an applicative functor, because there isn't really a first-class concept of monads or applicatives in F#. As an exercise, try writing the monadic 'bind' and 'return' for an F# list, then lift2 in terms of them. You can also implement lift2 using applicative functors, try it. Both exercises are quite enlightening once they 'click'.&lt;/p&gt;

&lt;p&gt;But the question asked for a result in tuples. So we could say:&lt;/p&gt;

&lt;pre class="code"&gt;List.lift2 (&lt;span style="color: blue"&gt;fun &lt;/span&gt;a b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;a,b) [1..30] [1..30]&lt;/pre&gt;

&lt;p&gt;Tupling elements is a pretty common operation, so FSharpx offers a function &lt;code&gt;tuple2&lt;/code&gt; that does just that (and &lt;code&gt;tuple3&lt;/code&gt; up to &lt;code&gt;tuple6&lt;/code&gt;. If you need more than that you probably shouldn't be using a tuple). To summarize:&lt;/p&gt;

&lt;pre class="code"&gt;List.lift2 tuple2 [1..30] [1..30]&lt;/pre&gt;

&lt;p&gt;With list comprehensions:&lt;/p&gt;

&lt;pre class="code"&gt;[ &lt;span style="color: blue"&gt;for &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;[1..30] &lt;span style="color: blue"&gt;do
    for &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;[1..30] &lt;span style="color: blue"&gt;do
        yield &lt;/span&gt;x,y ]&lt;/pre&gt;

&lt;p&gt;I think &lt;a href="http://www.haskell.org/haskellwiki/Pointfree"&gt;pointfree style&lt;/a&gt; wins here &lt;em&gt;if&lt;/em&gt; you're familiar with monads.&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://stackoverflow.com/questions/8179485/updating-nested-immutable-data-structures"&gt;Updating nested immutable data structures&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;In which the developer has a few nested immutable records and wants to write a mapping function that reaches deep inside this nesting. The record types model a role-playing game:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Monster = { 
    Awake: bool 
}

&lt;span style="color: blue"&gt;type &lt;/span&gt;Room = { 
    Locked: bool 
    Monsters: Monster list 
}

&lt;span style="color: blue"&gt;type &lt;/span&gt;Level = { 
    Illumination: int 
    Rooms: Room list 
}

&lt;span style="color: blue"&gt;type &lt;/span&gt;Dungeon = { 
    Levels: Level list 
}&lt;/pre&gt;

&lt;p&gt;How to write a mapping function that transforms all Monsters in all Rooms within a particular Level of a Dungeon?&lt;/p&gt;

&lt;p&gt;In other words, a function &lt;code class="code"&gt;mapMonstersOnLevel : int &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(Monster &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Monster) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Dungeon &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Dungeon&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;Let's roll up our sleeves!&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;mapMonstersOnLevel nLevel f dungeon =
  &lt;span style="color: blue"&gt;let &lt;/span&gt;levelMap (level: Level) =
    { level &lt;span style="color: blue"&gt;with &lt;/span&gt;Rooms = 
                 List.map (&lt;span style="color: blue"&gt;fun &lt;/span&gt;room &lt;span style="color: blue"&gt;-&amp;gt; 
                            &lt;/span&gt;{ room &lt;span style="color: blue"&gt;with &lt;/span&gt;Monsters = List.map f room.Monsters }) level.Rooms }
  { dungeon &lt;span style="color: blue"&gt;with &lt;/span&gt;Levels = 
                 List.mapi (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i level &lt;span style="color: blue"&gt;-&amp;gt; 
                             if &lt;/span&gt;i = nLevel &lt;span style="color: blue"&gt;then &lt;/span&gt;levelMap level &lt;span style="color: blue"&gt;else &lt;/span&gt;level) dungeon.Levels }&lt;/pre&gt;

&lt;br /&gt;

&lt;p&gt;Using &lt;a href="http://bugsquash.blogspot.com/2011/11/lenses-in-f.html"&gt;lenses&lt;/a&gt; we can express this much more concisely and elegantly. After the dreaded but necessary boilerplate for lenses (each field in each record must have an associated lens explicitly spelled out), we are left with:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;mapMonstersOnLevel nLevel f =
    Dungeon.levels &amp;gt;&amp;gt;| Lens.forList nLevel &amp;gt;&amp;gt;| Level.rooms &amp;gt;&amp;gt;| Lens.listMap Room.monsters
    |&amp;gt; Lens.update (f |&amp;gt; List.map |&amp;gt; List.map)&lt;/pre&gt;

&lt;p&gt;Lenses are quite intuitive, I think it's pretty easy to grasp what's hapenning there, even if you've never seen lenses before. The only difference is that because a lens Update is a Get, followed by the transforming function, followed by a Set, this isn't as effective as the first version. It can be optimized, but I'm not sure it's worth it, updating an element in an immutable list is still O(n), it doesn't matter much if it needs an extra pass.&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://stackoverflow.com/questions/8249744/exception-handling-in-pipeline-sequence"&gt;Exception handling in pipeline sequence&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;An initial state is pipelined along a series of functions that modify this state. The state in question is a point on a plane, but that's not important. Problem is, each function can throw, and when that happens the whole computation must be aborted and an error must be shown.&lt;/p&gt;

&lt;p&gt;Initially, the problem is posed like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;startingPosition = 0. ,0.

&lt;span style="color: blue"&gt;let &lt;/span&gt;moveByLengthAndAngle l a (x,y) = x,y &lt;span style="color: green"&gt;// too lazy to do the math
&lt;/span&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;moveByXandY dx dy (x,y) = x+dx, y+dy
&lt;span style="color: blue"&gt;let &lt;/span&gt;moveByXandAngle dx a (x,y) = x+dx, y

&lt;span style="color: blue"&gt;let &lt;/span&gt;finalPosition =
    startingPosition
    |&amp;gt; moveByLengthAndAngle x1 a1 
    |&amp;gt; moveByXandY x2 y2
    |&amp;gt; moveByXandAngle x3 a3
    |&amp;gt; moveByLengthAndAngle x4 a4
    &lt;span style="color: green"&gt;// etc...

&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Instead of doing this, let's represent the computation as a list of partially applied functions with the associated error messages:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;actions = 
    [
        moveByLengthAndAngle x1 a1, &lt;span style="color: maroon"&gt;&amp;quot;failed first moveByLengthAndAngle&amp;quot;
        &lt;/span&gt;moveByXandY x2 y2, &lt;span style="color: maroon"&gt;&amp;quot;failed moveByXandY&amp;quot;
        &lt;/span&gt;moveByXandY x3 y3, &lt;span style="color: maroon"&gt;&amp;quot;failed moveByXandY&amp;quot;
        &lt;/span&gt;moveByXandAngle x3 a3, &lt;span style="color: maroon"&gt;&amp;quot;failed moveByXandAngle&amp;quot;
        &lt;/span&gt;moveByLengthAndAngle x4 a4, &lt;span style="color: maroon"&gt;&amp;quot;failed second moveByLengthAndAngle&amp;quot;
    &lt;/span&gt;]&lt;/pre&gt;

&lt;p&gt;And then fold over this list of actions, starting with the initial point and accumulating the result of each function. In the folding function we must also catch any potential exception and put the error message instead, and abort the rest of the computation. But the state is a pair of floats and the error message obviously a string, so we model the actual state as a Choice of either the pair of floats or a string (i.e. &lt;code class="code"&gt;Choice&amp;lt;float * float, string&amp;gt;&lt;/code&gt; ):&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;finalPosition = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;evalToChoice (f,message) a =
        &lt;span style="color: blue"&gt;try
            &lt;/span&gt;f a |&amp;gt; Choice1Of2
        &lt;span style="color: blue"&gt;with &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Choice2Of2 message
    &lt;span style="color: blue"&gt;let &lt;/span&gt;folder a f = 
        &lt;span style="color: blue"&gt;match &lt;/span&gt;a &lt;span style="color: blue"&gt;with 
        &lt;/span&gt;| Choice2Of2 a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Choice2Of2 a 
        | Choice1Of2 a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;f a
    actions 
    |&amp;gt; List.map evalToChoice
    |&amp;gt; List.fold folder (Choice1Of2 startingPosition)&lt;/pre&gt;

&lt;p&gt;All that's left to do is pattern match on &lt;code&gt;finalPosition&lt;/code&gt; to see if the computation ended up as error or completed successfully:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;match &lt;/span&gt;finalPosition &lt;span style="color: blue"&gt;with
&lt;/span&gt;| Choice1Of2 (x,y) &lt;span style="color: blue"&gt;-&amp;gt; 
    &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;final position: %f,%f&amp;quot; &lt;/span&gt;x y
| Choice2Of2 error &lt;span style="color: blue"&gt;-&amp;gt; 
    &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;error: %s&amp;quot; &lt;/span&gt;error&lt;/pre&gt;

&lt;p&gt;FSharpx has a couple of abstractions (mostly borrowed from Haskell, as usual) that help reduce the clutter here: &lt;/p&gt;

&lt;p&gt;Firstly, FSharpx defines a monad around Choice1Of2 / Choice2Of2. This is like the Either monad defined in Haskell or Scalaz, except we call it Choice because... well, the F# type is already called Choice. So we have &lt;code&gt;Choice.bind&lt;/code&gt; and &lt;code&gt;returnM&lt;/code&gt; (actually 'return' is just the constructor Choice1Of2). The convention here is that Choice1Of2 carries the successful branch of the computation and Choice2Of2 carries the error.&lt;/p&gt;

&lt;p&gt;FSharpx also defines bifunctor for Choice. In Scalaz and &lt;a href="http://hackage.haskell.org/packages/archive/bifunctors/latest/doc/html/Data-Bifunctor.html"&gt;Haskell this is a typeclass&lt;/a&gt; (of which Either is an instance), but in FSharpx this is simply two functions &lt;code&gt;bimap&lt;/code&gt; and &lt;code&gt;mapSecond&lt;/code&gt;. Bimap takes two mapping functions, and uses the first one to map the Choice1Of2 part of a Choice, and the other one to map the Choice2Of2 part. mapSecond only maps the Choice2Of2 part of a choice. What about mapFirst? It's simply &lt;code&gt;map&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Since Choice/Either is frequently used in pure functional programming to represent errors, and we're working in an impure language, it makes sense to have a library function to convert a function that throws as a means to report an error into a function that returns a Choice, where the Choice2Of2 part is the potential exception. This seems to be &lt;a href="http://stackoverflow.com/q/1194541"&gt;pretty common in Scala&lt;/a&gt;, and it's what &lt;code&gt;Choice.protect&lt;/code&gt; in FSharpx does. The signature is pretty explicit: &lt;code&gt;('a -&amp;gt; 'b) -&amp;gt; 'a -&amp;gt; Choice&amp;lt;'b,exn&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Putting all the pieces together:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;finalPosition = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;folder position (f,message) =
        Choice.bind (Choice.protect f &amp;gt;&amp;gt; Choice.mapSecond (konst message)) position
    List.fold folder (Choice1Of2 startingPosition) actions&lt;/pre&gt;

&lt;p&gt;Haskellers have long ago recognized this folding with a monadic result and abstracted it into a generic function &lt;a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html#v:foldM"&gt;foldM&lt;/a&gt;. As usual, in F# we can't express this generically for &lt;em&gt;any&lt;/em&gt; monad, but we can write it for &lt;em&gt;every&lt;/em&gt; monad. With foldM the code looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;finalPosition = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;folder position (f,message) = 
        Choice.protect f position |&amp;gt; Choice.mapSecond (konst message)
    Choice.foldM folder startingPosition actions&lt;/pre&gt;

&lt;p&gt;A different approach to the problem is to rethrow the message as an exception, instead of using Choices. This is quite concise as well:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;finalPosition() = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;evalRethrow (f,message) a =
        &lt;span style="color: blue"&gt;try &lt;/span&gt;f a &lt;span style="color: blue"&gt;with &lt;/span&gt;_ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;failwith message
    actions
    |&amp;gt; List.map evalRethrow
    |&amp;gt; List.fold (|&amp;gt;) startingPosition&lt;/pre&gt;

&lt;p&gt;Instead of pattern matching over the result, you'd invoke this function in a try..with block:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;try
    let &lt;/span&gt;x,y = finalPosition()
    printfn &lt;span style="color: maroon"&gt;&amp;quot;final position: %f,%f&amp;quot; &lt;/span&gt;x y
&lt;span style="color: blue"&gt;with &lt;/span&gt;e &lt;span style="color: blue"&gt;-&amp;gt; 
    &lt;/span&gt;printfn &lt;span style="color: maroon"&gt;&amp;quot;error: %s&amp;quot; &lt;/span&gt;e.Message&lt;/pre&gt;

&lt;p&gt;The problem with using exceptions is that they're not really part of the type system. The function &lt;code&gt;finalPosition&lt;/code&gt; is of type &lt;code&gt;unit -&amp;gt; float*float&lt;/code&gt; . This doesn't say it might throw an exception, so you can't statically enforce handling the error case. Also, exceptions are actually misused here: having an error in the computation is an expected result, not a programming error. I recommend reading &lt;a href="http://www.haskell.org/haskellwiki/Error_vs._Exception"&gt;Errors vs Exceptions&lt;/a&gt; for more information on this. The terms 'exception' and 'error' may sometimes be used in one way or the other, but it's always useful to make the distinction.&lt;/p&gt;

&lt;p&gt;Also worth noting is how representing the computations as a list of actions with associated data (in this case, the messages) allows us to freely choose how to evaluate it.&lt;/p&gt;

&lt;h3&gt;&lt;a href="http://stackoverflow.com/questions/8238757/using-f-datatypes-in-c-sharp"&gt;Using F# datatypes in C#&lt;/a&gt;&lt;/h3&gt;

&lt;p&gt;FSharpx is not just for F# consumers. I blogged before about &lt;a href="http://bugsquash.blogspot.com/2011/10/10-reasons-to-use-f-runtime-in-your-c.html"&gt;using the F# runtime as a library in C# apps&lt;/a&gt;. FSharpx adds some sugar so you can use F# persistent collections in C# quite comfortably. Here's an example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;.Create(1, 2, 3);
&lt;span style="color: blue"&gt;var &lt;/span&gt;b = a.Cons(0);
b.TryFind(x =&amp;gt; x &amp;gt; 4)
 .Match(v =&amp;gt; Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;I found a value {0}&amp;quot;&lt;/span&gt;, v),
        () =&amp;gt; Console.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;I didn't find anything&amp;quot;&lt;/span&gt;));&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8643857899806162280-5082801826620902256?l=bugsquash.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/-y6YCZyYwD-U-M36IsKdNcisK58/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-y6YCZyYwD-U-M36IsKdNcisK58/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/-y6YCZyYwD-U-M36IsKdNcisK58/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/-y6YCZyYwD-U-M36IsKdNcisK58/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bugsquash/~4/Yr5vTzrHNl0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/5082801826620902256/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8643857899806162280&amp;postID=5082801826620902256" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/5082801826620902256?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/5082801826620902256?v=2" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/2011/11/few-fsharpx-examples.html" title="A few FSharpx examples" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><thr:total>0</thr:total></entry><entry gd:etag="W/&quot;C08NQHg_eyp7ImA9WhRRFU4.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280.post-3279457792413887193</id><published>2011-11-15T22:13:00.001-03:00</published><updated>2011-11-28T22:58:11.643-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-28T22:58:11.643-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><category scheme="http://www.blogger.com/atom/ns#" term="lenses" /><category scheme="http://www.blogger.com/atom/ns#" term="F#" /><category scheme="http://www.blogger.com/atom/ns#" term="fsharpx" /><title>Lenses in F#</title><content type="html">&lt;p&gt;Consider the following record types in F# :&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Car = { 
    Make: string 
    Model: string 
    Mileage: int 
}

&lt;span style="color: blue"&gt;type &lt;/span&gt;Editor = { 
    Name: string 
    Salary: int 
    Car: Car 
}

&lt;span style="color: blue"&gt;type &lt;/span&gt;Book = { 
    Name: string 
    Author: string 
    Editor: Editor 
}&lt;/pre&gt;&lt;p&gt;Given a Book we can trivially retrieve its editor's car mileage:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;mileage = aBook.Editor.Car.Mileage&lt;/pre&gt;&lt;p&gt;Setting the mileage is a bit more problematic though. If this were mutable, we could just do:&lt;/p&gt;&lt;pre class="code"&gt;aBook.Editor.Car.Mileage &amp;lt;- 1000&lt;/pre&gt;&lt;p&gt;But it's not, so we use F# copy-and-update syntax:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;book2 = { aBook &lt;span style="color: blue"&gt;with &lt;/span&gt;Editor = 
                { aBook.Editor &lt;span style="color: blue"&gt;with &lt;/span&gt;Car = 
                    { aBook.Editor.Car &lt;span style="color: blue"&gt;with &lt;/span&gt;Mileage = 1000 } } }&lt;/pre&gt;&lt;p&gt;That's a lot of fun! Not. Can we make this prettier?&lt;/p&gt;&lt;p&gt;Or if we want to &lt;em&gt;modify&lt;/em&gt; a property, for example add 1000 to the mileage, even with mutable properties we have to do:&lt;/p&gt;&lt;pre class="code"&gt;aBook.Editor.Car.Mileage &amp;lt;- aBook.Editor.Car.Mileage + 1000&lt;/pre&gt;&lt;p&gt;If this were C# we could just say:&lt;/p&gt;&lt;pre class="code"&gt;aBook.Editor.Car.Mileage += 1000;&lt;/pre&gt;&lt;p&gt;That's pretty convenient, so how can we implement something similar in F# with immutable records? &lt;/p&gt;&lt;p&gt;In summary, can we gain back some of the convenience of mutable properties?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;The key is to make properties first-class values&lt;/strong&gt;. &lt;/p&gt;&lt;p&gt;First we ask ourselves: what's a property getter? It's a function &lt;code class="code"&gt;'a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b&lt;/code&gt; , where &lt;code&gt;'a&lt;/code&gt; is the record type and &lt;code&gt;'b&lt;/code&gt; is the property type.&lt;/p&gt;&lt;p&gt;A setter for a mutable property is a function &lt;code&gt;'b -&amp;gt; 'a -&amp;gt; unit&lt;/code&gt; . But we're interested in setters for immutable records. Such a setter is a function &lt;code&gt;'b -&amp;gt; 'a -&amp;gt; 'a&lt;/code&gt; , i.e. it takes the new value, the record, and returns the modified record.&lt;/p&gt;&lt;p&gt;So we have a pair of functions:&lt;/p&gt;&lt;pre class="code"&gt;Get : 'a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b

Set : 'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a&lt;/pre&gt;&lt;p&gt;Let's put them together in a record, which we'll call &amp;quot;Lens&amp;quot;:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Lens&amp;lt;'a,'b&amp;gt; = {
    Get: 'a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'b
    Set: 'b &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;'a
}&lt;/pre&gt;&lt;p&gt;Modeling a 'modify' operation on top of this is easy: you get the original value, modify it with some function, then set the modified value back:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;with member &lt;/span&gt;l.Update f a = 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;value = l.Get a 
    &lt;span style="color: blue"&gt;let &lt;/span&gt;newValue = f value 
    l.Set newValue a&lt;/pre&gt;&lt;p&gt;Note that this Update is still purely functional.&lt;/p&gt;&lt;p&gt;Let's create some lenses for the record types we declared above:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;type &lt;/span&gt;Car &lt;span style="color: blue"&gt;with
    static member &lt;/span&gt;mileage = 
        { Get = &lt;span style="color: blue"&gt;fun &lt;/span&gt;(c: Car) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;c.Mileage
          Set = &lt;span style="color: blue"&gt;fun &lt;/span&gt;v (x: Car) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;{ x &lt;span style="color: blue"&gt;with &lt;/span&gt;Mileage = v } }

&lt;span style="color: blue"&gt;type &lt;/span&gt;Editor &lt;span style="color: blue"&gt;with
    static member &lt;/span&gt;car = 
        { Get = &lt;span style="color: blue"&gt;fun &lt;/span&gt;(x: Editor) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;x.Car 
          Set = &lt;span style="color: blue"&gt;fun &lt;/span&gt;v (x: Editor) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;{ x &lt;span style="color: blue"&gt;with &lt;/span&gt;Car = v } }

&lt;span style="color: blue"&gt;type &lt;/span&gt;Book &lt;span style="color: blue"&gt;with
    static member &lt;/span&gt;editor = 
        { Get = &lt;span style="color: blue"&gt;fun &lt;/span&gt;(x: Book) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;x.Editor 
          Set = &lt;span style="color: blue"&gt;fun &lt;/span&gt;v (x: Book) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;{ x &lt;span style="color: blue"&gt;with &lt;/span&gt;Editor = v } }&lt;/pre&gt;&lt;p&gt;What we need now is a way to compose these lenses, so we can go from a book to a mileage and update it:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let inline &lt;/span&gt;(&amp;gt;&amp;gt;|) (l1: Lens&amp;lt;_,_&amp;gt;) (l2: Lens&amp;lt;_,_&amp;gt;) = 
    { Get = l1.Get &amp;gt;&amp;gt; l2.Get 
      Set = l2.Set &amp;gt;&amp;gt; l1.Update }&lt;/pre&gt;&lt;p&gt;Lenses are closed under composition. That is, the sequential composition of any two lenses is a lens.&lt;/p&gt;&lt;p&gt;We can now create a lens that goes from a book instance to the mileage, by composing primitive lenses:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;bookEditorCarMileage = Book.editor &amp;gt;&amp;gt;| Editor.car &amp;gt;&amp;gt;| Car.mileage

&lt;span style="color: blue"&gt;let &lt;/span&gt;mileage = bookEditorCarMileage.Get aBook

&lt;span style="color: blue"&gt;let &lt;/span&gt;book2 = aBook |&amp;gt; bookEditorCarMileage.Set 1000&lt;/pre&gt;&lt;p&gt;We can also implement (+=) generically for any lens focused on a type supporting (+) (e.g. int, float, decimal):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let inline &lt;/span&gt;(+=) (l: Lens&amp;lt;_,_&amp;gt;) v = l.Update ((+) v)

&lt;span style="color: blue"&gt;let &lt;/span&gt;book2 = aBook |&amp;gt; bookEditorCarMileage += 1000&lt;/pre&gt;&lt;p&gt;Thanks to lenses we now have much of the convenience of the mutable properties, while at the same time making properties first-class composable objects and retaining purity.&lt;/p&gt;&lt;h3&gt;Lenses and the State monad&lt;/h3&gt;&lt;p&gt;It's also possible to lift lens operations to the State monad: this gives an even more imperative feel, while still retaining referential transparency. Here's an example (using &lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt;'s State monad):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;promote =
    state {
        &lt;span style="color: blue"&gt;let! &lt;/span&gt;oldSalary = Lens.getState Editor.salary
        &lt;span style="color: blue"&gt;do! &lt;/span&gt;Editor.salary += 1000
        &lt;span style="color: blue"&gt;return &lt;/span&gt;oldSalary
    }
&lt;span style="color: blue"&gt;let &lt;/span&gt;oldSalary, promotedTom = promote tom
printfn &lt;span style="color: maroon"&gt;&amp;quot;Tom used to make %d, after promotion he now makes %d&amp;quot; &lt;/span&gt;oldSalary promotedTom.Salary&lt;/pre&gt;&lt;h3&gt;Conclusions&lt;/h3&gt;&lt;p&gt;I've barely scratched the surface of lenses here. The general concept of lenses is that a lens allows to &lt;em&gt;focus&lt;/em&gt; on a particular element in a data structure, both to view it and to update it. More formally, lenses can be described as &lt;em&gt;well-behaved bidirectional transformations&lt;/em&gt;. Lenses must follow some laws (which I haven't shown here) in order to be well-behaved.&lt;/p&gt;&lt;p&gt;Lenses are being actively researched, and because of their genericity they have been applied in widely different areas, for example &lt;a href="http://www.cis.upenn.edu/~mgree/ugrad/lenses/"&gt;functional reactive AJAX&lt;/a&gt; applications (lenses for &lt;a href="http://www.flapjax-lang.org/"&gt;Flapjax&lt;/a&gt;) and &lt;a href="http://augeas.net/docs/lenses.html"&gt;configuration management&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;There's even a whole research programming language around lenses called &lt;a href="http://www.seas.upenn.edu/~harmony/"&gt;Boomerang&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.exnebula.org/vcc"&gt;Virtual Combat Cards&lt;/a&gt;, a tool to assist Dungeons &amp;amp; Dragons Dungeon masters, written in Scala, makes use of lenses as first-class properties (&lt;a href="http://code.google.com/p/virtual-combat-cards/"&gt;code is here&lt;/a&gt; if you want to check it out).&lt;/p&gt;&lt;p&gt;&lt;a href="http://code.google.com/p/scalaz/"&gt;Scalaz&lt;/a&gt; implements the basic plumbing and definition of lenses for Scala, and this F# implementation draws heavily from that code. Hat tip to the Scalaz devs. Haskell has a similar package &lt;a href="http://hackage.haskell.org/packages/archive/lenses/latest/doc/html/Data-Lenses.html"&gt;Data.Lenses&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;When using lenses for properties as I've shown here, there's the problem of having to define the primitive lenses that wrap the actual .NET properties. Haskell solves that using Template Haskell, and Scala has a &lt;a href="https://github.com/gseitz/Lensed"&gt;compiler plugin&lt;/a&gt; currently under development which automatically generates this boilerplate. It may be possible to write a type provider to do this in F# 3.0, but I haven't looked into this yet.&lt;/p&gt;&lt;p&gt;F# code shown here coming soon to &lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt;.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8643857899806162280-3279457792413887193?l=bugsquash.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/gAgUGRphGwH4_98vAM1rCVbJoEg/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gAgUGRphGwH4_98vAM1rCVbJoEg/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/gAgUGRphGwH4_98vAM1rCVbJoEg/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/gAgUGRphGwH4_98vAM1rCVbJoEg/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bugsquash/~4/pHePMUuPHFw" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/3279457792413887193/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8643857899806162280&amp;postID=3279457792413887193" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/3279457792413887193?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/3279457792413887193?v=2" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/2011/11/lenses-in-f.html" title="Lenses in F#" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;A0EMQX45eCp7ImA9WhdaGEg.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280.post-4384411888718667822</id><published>2011-10-29T00:41:00.001-03:00</published><updated>2011-10-29T00:41:20.020-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-10-29T00:41:20.020-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="functional programming" /><title>Poor man's option type in C#</title><content type="html">&lt;p&gt;I've &lt;a href="http://bugsquash.blogspot.com/2011/04/refactoring-to-monadic-c.html"&gt;blogged&lt;/a&gt; &lt;a href="http://bugsquash.blogspot.com/2011/08/refactoring-to-monadic-c-part-2.html"&gt;before&lt;/a&gt; about the virtues of the Option type. However, it's currently not part of the standard .NET library, so either you have to use F#, code it yourself, or use a library.&lt;/p&gt;  &lt;p&gt;But there is something built-in and almost equivalent: lists! You can use any list-like container (List&amp;lt;T&amp;gt;, T[]) as an option type, and all operations are already built-in. &lt;/p&gt;  &lt;p&gt;Option has None (no value) and Some (a value). This corresponds exactly to an empty list and a singleton list respectively. Let's see how we map operations on Options (using &lt;a href="https://github.com/mausch/fsharpx"&gt;FSharpx&lt;/a&gt;) to operations on arrays using standard .NET 3.5:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;table style="border-collapse: collapse" border="1" cellspacing="0" cellpadding="2" width="100%"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;th&gt;&lt;/th&gt;        &lt;th&gt;Option&lt;/th&gt;        &lt;th&gt;Array / IEnumerable&lt;/th&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td&gt;&lt;b&gt;Constructor: Some&lt;/b&gt;&lt;/td&gt;        &lt;td&gt;         &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Some(5)&lt;/pre&gt;
or: 

        &lt;pre class="code"&gt;5.Some()&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;new&lt;/span&gt;[] { 5 }&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Constructor: None&lt;/b&gt;&lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.None&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;new int&lt;/span&gt;[0]&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Check if the option has a value&lt;/b&gt;&lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;bool &lt;/span&gt;hasValue = o.HasValue();&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;bool &lt;/span&gt;hasValue = o.Any();&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Get the value associated with the option&lt;/b&gt;&lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;int &lt;/span&gt;value = o.Value;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;int &lt;/span&gt;value = o.First();&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td&gt;&lt;b&gt;Pattern matching&lt;/b&gt;&lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;int &lt;/span&gt;b = o.Match(x =&amp;gt; x + 2, () =&amp;gt; 99);&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; o = ...
&lt;span style="color: blue"&gt;int &lt;/span&gt;b = o.Aggregate(99, (_, x) =&amp;gt; x + 2);&lt;/pre&gt;
or lazier: 

        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;int &lt;/span&gt;b = singleVersion.Any() ? singleVersion.First() + 2 : 99;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;And thanks to LINQ, you also get mapping and monadic syntax for free. Remember &lt;a href="http://bugsquash.blogspot.com/2011/04/refactoring-to-monadic-c.html"&gt;that code I refactored to monads&lt;/a&gt; a while ago? Here's the last part of it side-by-side with a translation using Array/IEnumerable:&lt;/p&gt;

&lt;table style="border-collapse: collapse" border="1"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;maxVersion = &lt;span style="color: #2b91af"&gt;L&lt;/span&gt;.F((&lt;span style="color: blue"&gt;string&lt;/span&gt;[] parts) =&amp;gt; {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;p = parts.Length == 2 ? parts[1] : parts[0];
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(p))
        &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Version&lt;/span&gt;&amp;gt;&amp;gt;.Some(&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Version&lt;/span&gt;&amp;gt;.None);
    &lt;span style="color: blue"&gt;return &lt;/span&gt;ParseVersion(p).Select(v =&amp;gt; v.ToOption());
});

&lt;span style="color: blue"&gt;var &lt;/span&gt;singleVersion =
    &lt;span style="color: blue"&gt;from &lt;/span&gt;v &lt;span style="color: blue"&gt;in &lt;/span&gt;ParseVersion(value)
    &lt;span style="color: blue"&gt;select &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IVersionSpec&lt;/span&gt;) &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VersionSpec &lt;/span&gt;{IsMinInclusive = &lt;span style="color: blue"&gt;true&lt;/span&gt;, MinVersion = v};

&lt;span style="color: blue"&gt;var &lt;/span&gt;versionRange = &lt;span style="color: #2b91af"&gt;L&lt;/span&gt;.F(() =&amp;gt; &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;checkLength(value)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;isMin &lt;span style="color: blue"&gt;in &lt;/span&gt;minInclusive(value)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;isMax &lt;span style="color: blue"&gt;in &lt;/span&gt;maxInclusive(value)
                             &lt;span style="color: blue"&gt;let &lt;/span&gt;val = value.Substring(1, value.Length - 2)
                             &lt;span style="color: blue"&gt;let &lt;/span&gt;parts = val.Split(&lt;span style="color: #a31515"&gt;','&lt;/span&gt;)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;checkParts(parts)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;min &lt;span style="color: blue"&gt;in &lt;/span&gt;minVersion(parts)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;max &lt;span style="color: blue"&gt;in &lt;/span&gt;maxVersion(parts)
                             &lt;span style="color: blue"&gt;select &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IVersionSpec&lt;/span&gt;) &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VersionSpec &lt;/span&gt;{
                                 IsMinInclusive = isMin,
                                 MinVersion = min.HasValue() ? min.Value : &lt;span style="color: blue"&gt;null&lt;/span&gt;,
                                 IsMaxInclusive = isMax,
                                 MaxVersion = max.HasValue() ? max.Value : &lt;span style="color: blue"&gt;null&lt;/span&gt;,
                             });

&lt;span style="color: blue"&gt;return &lt;/span&gt;singleVersion.OrElse(versionRange)();&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td&gt;
        &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;maxVersion = &lt;span style="color: #2b91af"&gt;L&lt;/span&gt;.F((&lt;span style="color: blue"&gt;string&lt;/span&gt;[] parts) =&amp;gt; {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;p = parts.Length == 2 ? parts[1] : parts[0];
    &lt;span style="color: blue"&gt;if &lt;/span&gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt;.IsNullOrWhiteSpace(p))
        &lt;span style="color: blue"&gt;return new&lt;/span&gt;[] {&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Version&lt;/span&gt;[0]};
    &lt;span style="color: blue"&gt;return &lt;/span&gt;ParseVersion(p).Select(v =&amp;gt; &lt;span style="color: blue"&gt;new&lt;/span&gt;[] {v});
});

&lt;span style="color: blue"&gt;var &lt;/span&gt;singleVersion =
    &lt;span style="color: blue"&gt;from &lt;/span&gt;v &lt;span style="color: blue"&gt;in &lt;/span&gt;ParseVersion(value)
    &lt;span style="color: blue"&gt;select &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IVersionSpec&lt;/span&gt;) &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VersionSpec &lt;/span&gt;{IsMinInclusive = &lt;span style="color: blue"&gt;true&lt;/span&gt;, MinVersion = v};

&lt;span style="color: blue"&gt;var &lt;/span&gt;versionRange = &lt;span style="color: #2b91af"&gt;L&lt;/span&gt;.F(() =&amp;gt; &lt;span style="color: blue"&gt;from &lt;/span&gt;x &lt;span style="color: blue"&gt;in &lt;/span&gt;checkLength(value)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;isMin &lt;span style="color: blue"&gt;in &lt;/span&gt;minInclusive(value)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;isMax &lt;span style="color: blue"&gt;in &lt;/span&gt;maxInclusive(value)
                             &lt;span style="color: blue"&gt;let &lt;/span&gt;val = value.Substring(1, value.Length - 2)
                             &lt;span style="color: blue"&gt;let &lt;/span&gt;parts = val.Split(&lt;span style="color: #a31515"&gt;','&lt;/span&gt;)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;y &lt;span style="color: blue"&gt;in &lt;/span&gt;checkParts(parts)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;min &lt;span style="color: blue"&gt;in &lt;/span&gt;minVersion(parts)
                             &lt;span style="color: blue"&gt;from &lt;/span&gt;max &lt;span style="color: blue"&gt;in &lt;/span&gt;maxVersion(parts)
                             &lt;span style="color: blue"&gt;select &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;IVersionSpec&lt;/span&gt;) &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;VersionSpec &lt;/span&gt;{
                                 IsMinInclusive = isMin,
                                 MinVersion = min.Any() ? min.First() : &lt;span style="color: blue"&gt;null&lt;/span&gt;,
                                 IsMaxInclusive = isMax,
                                 MaxVersion = max.Any() ? max.First() : &lt;span style="color: blue"&gt;null&lt;/span&gt;,
                             });

&lt;span style="color: blue"&gt;return &lt;/span&gt;singleVersion.Any() ? singleVersion : versionRange();&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;If you want to compare the whole code: &lt;a href="https://github.com/mausch/RefactorToMonadicCSharp/blob/5bd99db3bfb19225bcc3af1dab8eacd015c809f2/RefactorToMonadicCSharp/Functional.cs"&gt;here's the original&lt;/a&gt; (using option) and &lt;a href="https://github.com/mausch/RefactorToMonadicCSharp/blob/5bd99db3bfb19225bcc3af1dab8eacd015c809f2/RefactorToMonadicCSharp/Functional2.cs"&gt;here's the one using arrays&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You've even probably used this already, perhaps without realizing this relation. However, while an option type can either have exactly one or zero value, an array can have any number of values. And if you see a method returning an IEnumerable&amp;lt;T&amp;gt;, you wouldn't think you're supposed to treat it as an option. &lt;/p&gt;

&lt;p&gt;So IEnumerable&amp;lt;T&amp;gt; as a monad (the List monad, that is) is sort of an extension of the option type (i.e. the Maybe monad): instead of just supporting &lt;em&gt;one&lt;/em&gt; successful computation, it supports &lt;em&gt;many&lt;/em&gt;. I think using the List monad as an Option is acceptable &lt;em&gt;locally&lt;/em&gt;, and only if you can't use a proper option type or for some reason don't want to take the dependency on a library. It's a useful hack, but still a hack. They're different things really.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8643857899806162280-4384411888718667822?l=bugsquash.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/OnJ5hyLh_i67rGsmIyYCCNztnpI/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OnJ5hyLh_i67rGsmIyYCCNztnpI/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/OnJ5hyLh_i67rGsmIyYCCNztnpI/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/OnJ5hyLh_i67rGsmIyYCCNztnpI/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bugsquash/~4/uzMMd-xiqXs" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/4384411888718667822/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8643857899806162280&amp;postID=4384411888718667822" title="2 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/4384411888718667822?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/4384411888718667822?v=2" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/2011/10/poor-man-option-type-in-c.html" title="Poor man&amp;#39;s option type in C#" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><thr:total>2</thr:total></entry><entry gd:etag="W/&quot;C0cDQnYyfyp7ImA9WhRVE00.&quot;"><id>tag:blogger.com,1999:blog-8643857899806162280.post-1595648084743450913</id><published>2011-10-18T12:01:00.001-03:00</published><updated>2012-01-11T13:57:53.897-03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-01-11T13:57:53.897-03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="fsharpx" /><title>10 reasons to use the F# runtime in your C# app</title><content type="html">&lt;p&gt;Most people have at least noticed that F# shipped with Visual Studio 2010. I mean, you click File –&amp;gt; New Project and there's the F# project templates, you can't miss them.&lt;/p&gt;&lt;p&gt;What most people probably didn't realize is that even if you don't use the F# language or aren't even interested in it, you can still profit from using the F# &lt;em&gt;runtime &lt;/em&gt;in C# / VB.NET projects. The F# runtime is just a regular DLL named FSharp.Core.dll you can reference just like any other assembly. It's available for .NET 2.0 and 4.0 (separate DLLs). This availability for .NET 2.0 is particularly valuable for projects that for some reason can't be upgraded to newer versions of .NET.&lt;/p&gt;&lt;p&gt;Granted, the library is designed to be used from F#, so sometimes it looks weird in C#, but we'll see how to work around some of the oddities with &lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Let's see some of the things FSharp.Core gives you, in no particular order:&lt;/p&gt;&lt;h3&gt;Tuples&lt;/h3&gt;&lt;p&gt;So you want to use &lt;a href="http://msdn.microsoft.com/en-us/library/system.tuple.aspx"&gt;tuples&lt;/a&gt; but you can't upgrade to .NET 4 because of company policies or some obscure dependency that breaks. No problem, FSharp.Core.dll implements them, so you can use tuples in .NET 2.0 with exactly the same API and namespace as .NET 4 tuples. If you then upgrade you don't have to change anything.&lt;/p&gt;&lt;p&gt;Tuples are simple but not trivial to implement, for example &lt;a href="http://stackoverflow.com/questions/152019/will-a-future-version-of-net-support-tuples-in-c/152026#152026"&gt;some&lt;/a&gt; &lt;a href="http://lambda-the-ultimate.org/node/3584"&gt;forget to&lt;/a&gt; implement equality / hashing so you'd end up with &amp;quot;WTF moments&amp;quot; at some point. It's worth using a library that implements them properly.&lt;/p&gt;&lt;p&gt;As usual, keep in mind that tuples are essentially anonymous. Item1, Item2, etc don't convey any information about what they're holding, only their types.&lt;/p&gt;&lt;h3&gt;Persistent collections&lt;/h3&gt;&lt;p&gt;Persistent lists are one of the most frequently used data structures in functional programming. They're so prevalent that F# has special syntax for them. For example, to define an empty list in F# :&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;empty = []&lt;/pre&gt;&lt;p&gt;F# infers the list element type. In C# things are more verbose:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;empty = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Empty;&lt;/pre&gt;&lt;p&gt;To add an element to a list you actually create a new list that has the new element as head and the other list as tail. Again, F# has special syntax:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;a = 1::empty&lt;/pre&gt;&lt;p&gt;While in C#:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(1, empty);&lt;/pre&gt;&lt;p&gt;or:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Cons(1, empty);&lt;/pre&gt;&lt;p&gt;FSharpx helps with a little sugar here:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = empty.Cons(1);&lt;/pre&gt;&lt;p&gt;You can also create an immutable list from any IEnumerable&amp;lt;T&amp;gt;:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;b = &lt;span style="color: #2b91af"&gt;SeqModule&lt;/span&gt;.ToList(&lt;span style="color: blue"&gt;new&lt;/span&gt;[] { 1, 2, 3 });&lt;/pre&gt;&lt;p&gt;Again, FSharpx adds some sugar:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;b = &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { 1, 2, 3 }.ToFSharpList();&lt;/pre&gt;&lt;p&gt;or:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;b = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;.Create(1, 2, 3);&lt;/pre&gt;&lt;p&gt;How do you use a FSharpList? You can access a particular element just as with a regular mutable list:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(b[2]); &lt;span style="color: green"&gt;// prints &amp;quot;3&amp;quot;
&lt;/span&gt;&lt;/pre&gt;&lt;p&gt;Be aware that random access in an immutable linked list is O(n).&lt;/p&gt;&lt;p&gt;FSharpList implement IEnumerable&amp;lt;T&amp;gt;, so you can traverse it with foreach and use all LINQ functions (Aggregate, Select, Where, etc).&lt;/p&gt;&lt;p&gt;Functional languages often use pattern matching and recursion to process a list. The &lt;a href="\"&gt;F# wikibook has a great chapter explaining it&lt;/a&gt;. FSharpx implements basic pattern matching on lists for C#, so you can write this to reverse a list:&lt;/p&gt;&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;]
&lt;span style="color: blue"&gt;void &lt;/span&gt;Reverse() {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(0, 1000).ToFSharpList();
    &lt;span style="color: blue"&gt;var &lt;/span&gt;r = Loop(&lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Empty, a);
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(r);
}

&lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;T&amp;gt; Loop&amp;lt;T&amp;gt;(&lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;T&amp;gt; acc, &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;T&amp;gt; l) {
    &lt;span style="color: blue"&gt;return &lt;/span&gt;l.Match(() =&amp;gt; acc,
                   (head, tail) =&amp;gt; Loop(acc.Cons(head), tail));
}&lt;/pre&gt;&lt;p&gt;But be careful! F# compiles the equivalent code using &lt;a href="http://en.wikipedia.org/wiki/Tail_call"&gt;tail call optimization&lt;/a&gt;, while C# doesn't have that feature, so the above code blows with a StackOverflowException when given a sufficiently big list (unless you've compiled with optimizations and running in a 64-bit CLR !)&lt;/p&gt;&lt;p&gt;When recursively processing lists, it's best to use Aggregate() instead if possible (usually called &lt;a href="http://en.wikipedia.org/wiki/Fold_(higher-order_function)"&gt;fold&lt;/a&gt; in functional languages), which encapsulates recursion without blowing the stack. It's also simpler:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;Enumerable&lt;/span&gt;.Range(0, 1000000).ToFSharpList();
&lt;span style="color: blue"&gt;var &lt;/span&gt;r = a.Aggregate(&lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;.Empty, (acc, i) =&amp;gt; acc.Cons(i));&lt;/pre&gt;&lt;p&gt;Of course, this is just demo code. If you really want to reverse a list just call &lt;code class="code"&gt;&lt;span style="color: #2b91af"&gt;ListModule&lt;/span&gt;.Reverse(a);&lt;/code&gt;&lt;/p&gt;&lt;p&gt;FSharp.Core also implements a persistent set and dictionary. &lt;/p&gt;&lt;p&gt;Imperative programmers might wonder why they should use an immutable collection when the BCL already has several perfectly good mutable collections.&lt;/p&gt;&lt;p&gt;One of the most cited reasons for using persistent collections (and functional programming in general) is multithreading. Indeed you can freely and safely pass persistent collections around threads, which makes multithreaded development easier. However, the same can be said about passing collections around regular functions: you can be sure that no function can ever modify a list, therefore you have one less thing to keep track of in your head and you &lt;a href="http://blogs.msdn.com/b/jomo_fisher/archive/2007/05/16/leaky-functions-barrel-of-bugs.aspx"&gt;statically eliminate a whole class of bugs&lt;/a&gt;. Immutability makes &lt;em&gt;all&lt;/em&gt; kinds of programming simpler, multithreaded or not. Of course, for immutable collections to really work as immutable, the underlying element type must be also immutable.&lt;/p&gt;&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/data/gg577610"&gt;Reactive extensions&lt;/a&gt; also includes an ImmutableList class, although it's internal.&lt;/p&gt;&lt;h3&gt;The Option type&lt;/h3&gt;&lt;p&gt;I have blogged before about using the F# Option type in C# projects &lt;a href="http://bugsquash.blogspot.com/2011/04/refactoring-to-monadic-c.html"&gt;here&lt;/a&gt; and &lt;a href="http://bugsquash.blogspot.com/2011/08/refactoring-to-monadic-c-part-2.html"&gt;here&lt;/a&gt;. Options are pervasively used in F#, for example several functions on collections use options. The problem is, these functions take the equivalent of a Func but in F#, which is an FSharpFunc, which makes it very inconvenient to use them from C#.&lt;/p&gt;&lt;p&gt;FSharpx wraps these F# functions so they can be used with System.Func and System.Action. For example:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;.Create(1, 2, 3);
a.TryFind(x =&amp;gt; x &amp;gt; 4) &lt;span style="color: green"&gt;// returns FSharpOption&amp;lt;int&amp;gt;
    &lt;/span&gt;.Match(v =&amp;gt; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.Fail(&lt;span style="color: #a31515"&gt;&amp;quot;shouldn't have found value {0}&amp;quot;&lt;/span&gt;, v),
           () =&amp;gt; { /* nothing found */ });&lt;/pre&gt;&lt;h3&gt;The Unit type&lt;/h3&gt;&lt;p&gt;Many functional languages like F# have a type called &amp;quot;Unit&amp;quot;, which is just like &amp;quot;void&amp;quot; in C-like languages, except it's actually usable as a proper type. &lt;/p&gt;&lt;p&gt;By &amp;quot;usable&amp;quot; I mean you can actually define something like a Func&amp;lt;Unit&amp;gt; (you can't have a Func&amp;lt;void&amp;gt;, it's not even syntactically correct even though there is a type System.Void). A Func&amp;lt;Unit&amp;gt; is just like an Action, except it's obviously a Func so it can be used for example in a LINQ expression (i.e. a monad).&lt;/p&gt;&lt;p&gt;&lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt; includes a ToFunc() extension method on Action, Action&amp;lt;T&amp;gt;, Action&amp;lt;T1,T2&amp;gt;, etc. to respectively convert them to Func&amp;lt;Unit&amp;gt;, Func&amp;lt;T,Unit&amp;gt;, Func&amp;lt;T1,T2,Unit&amp;gt; and so on.&lt;/p&gt;&lt;p&gt;You can also use it for types like FSharpOption&amp;lt;Unit&amp;gt; as I &lt;a href="http://bugsquash.blogspot.com/2011/04/refactoring-to-monadic-c.html"&gt;blogged about before&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Reactive Extensions also includes a Unit type.&lt;/p&gt;&lt;h3&gt;Discriminated unions&lt;/h3&gt;&lt;p&gt;I have blogged before about using F# discriminated unions in C# &lt;a href="http://bugsquash.blogspot.com/2011/08/refactoring-to-monadic-c-part-2.html"&gt;here&lt;/a&gt; and &lt;a href="http://bugsquash.blogspot.com/2011/08/validating-with-applicative-functors.html"&gt;here&lt;/a&gt;, in the context of validation. They're very useful to express things like &amp;quot;either this or that&amp;quot; without having to introduce a whole class hierarchy implementing equality / hash / comparison.&lt;/p&gt;&lt;p&gt;Just as with other things, using them in C# is more verbose than in F#.&lt;/p&gt;&lt;p&gt;Let's see an example:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;FSharpChoice&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;.NewChoice1Of2(1);
&lt;span style="color: blue"&gt;if &lt;/span&gt;(a.IsChoice1Of2) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;x = ((&lt;span style="color: #2b91af"&gt;FSharpChoice&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;.&lt;span style="color: #2b91af"&gt;Choice1Of2&lt;/span&gt;)a).Item;
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x + 2);
} &lt;span style="color: blue"&gt;else if &lt;/span&gt;(a.IsChoice2Of2) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;x = ((&lt;span style="color: #2b91af"&gt;FSharpChoice&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;.&lt;span style="color: #2b91af"&gt;Choice2Of2&lt;/span&gt;)a).Item;
    &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x + &lt;span style="color: #a31515"&gt;&amp;quot;;&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;&lt;p&gt;Now that looks &lt;em&gt;really&lt;/em&gt; ugly. And what's with the downcasting?!&lt;/p&gt;&lt;p&gt;FSharpx makes this more usable by implementing pattern matching (basically a &lt;a href="http://en.wikipedia.org/wiki/Visitor_pattern"&gt;visitor&lt;/a&gt;) so you can write instead:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: #2b91af"&gt;FSharpChoice&lt;/span&gt;.New1Of2&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;, &lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(1);
a.Match(x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x + 2),
        x =&amp;gt; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(x + &lt;span style="color: #a31515"&gt;&amp;quot;;&amp;quot;&lt;/span&gt;));&lt;/pre&gt;&lt;p&gt;FSharpx also implements LINQ operators around 2-choice and integrates with Option. Here's an example:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;object &lt;/span&gt;a = 40;
&lt;span style="color: blue"&gt;const string &lt;/span&gt;b = &lt;span style="color: #a31515"&gt;&amp;quot;60&amp;quot;&lt;/span&gt;;
&lt;span style="color: blue"&gt;var &lt;/span&gt;r = &lt;span style="color: blue"&gt;from &lt;/span&gt;i &lt;span style="color: blue"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpOption&lt;/span&gt;.ParseInt(b).ToFSharpChoice(&lt;span style="color: #a31515"&gt;&amp;quot;Invalid value b&amp;quot;&lt;/span&gt;)
        &lt;span style="color: blue"&gt;from &lt;/span&gt;j &lt;span style="color: blue"&gt;in &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpChoice&lt;/span&gt;.Cast&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt;(a).SelectSecond(_ =&amp;gt; &lt;span style="color: #a31515"&gt;&amp;quot;Invalid value a&amp;quot;&lt;/span&gt;)
        &lt;span style="color: blue"&gt;select &lt;/span&gt;i + j;
r.Match(i =&amp;gt; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual(100, i),
        &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.Fail);&lt;/pre&gt;&lt;p&gt;Just as with tuples, discriminated unions are essentially anonymous. Tuples are the generic, anonymous &lt;a href="http://en.wikipedia.org/wiki/Product_type"&gt;product types&lt;/a&gt;. Discriminated unions are the generic, anonymous &lt;a href="http://en.wikipedia.org/wiki/Sum_type"&gt;sum types&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Reactive extensions uses an internal Either&amp;lt;TLeft, TRight&amp;gt; type.&lt;/p&gt;&lt;h3&gt;Async&lt;/h3&gt;&lt;p&gt;Once again, you're stuck with .NET 3.5 drooling over the &lt;a href="http://msdn.microsoft.com/en-us/library/dd460717.aspx"&gt;Task Parallel Library&lt;/a&gt; in .NET 4.&lt;/p&gt;&lt;p&gt;Reactive extensions used to include a backport of System.Threading.dll, but it was unsupported and &lt;a href="http://social.msdn.microsoft.com/Forums/pl-PL/rx/thread/a55a0b30-ff76-40f5-a3d5-7b7496c4ef35"&gt;it's not included in recent releases&lt;/a&gt; any more.&lt;/p&gt;&lt;p&gt;F# has asynchronous workflows, which is similar yet somewhat different from C# 5 await/async (see differences in &lt;a href="http://tomasp.net/blog/async-compilation-internals.aspx"&gt;this series of posts by Tomas Petricek&lt;/a&gt;)&lt;/p&gt;&lt;p&gt;FSharpx has LINQ bindings for this so you can write:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;FSharpAsync&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Get(&lt;span style="color: blue"&gt;string &lt;/span&gt;u) {
    &lt;span style="color: blue"&gt;var &lt;/span&gt;web = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebClient&lt;/span&gt;();
    &lt;span style="color: blue"&gt;return &lt;/span&gt;web.AsyncDownloadString(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(u));
}
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;qq = // qq is of type FSharpAsync&amp;lt;string&amp;gt;
    &lt;span style="color: blue"&gt;from &lt;/span&gt;google &lt;span style="color: blue"&gt;in &lt;/span&gt;Get(&lt;span style="color: #a31515"&gt;&amp;quot;http://www.google.com&amp;quot;&lt;/span&gt;)
    &lt;span style="color: blue"&gt;from &lt;/span&gt;bing &lt;span style="color: blue"&gt;in &lt;/span&gt;Get(&lt;span style="color: #a31515"&gt;&amp;quot;http://www.bing.com&amp;quot;&lt;/span&gt;)
    &lt;span style="color: blue"&gt;select &lt;/span&gt;google + bing;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;result = qq.Run();
&lt;/pre&gt;&lt;/pre&gt;&lt;/pre&gt;&lt;p&gt;Or you can run multiple requests in parallel:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;urls = &lt;span style="color: #2b91af"&gt;FSharpList&lt;/span&gt;.Create(
      &lt;span style="color: #a31515"&gt;&amp;quot;http://www.google.com&amp;quot;
    &lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;http://www.bing.com&amp;quot;
    &lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;http://www.yahoo.com&amp;quot;
    &lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;http://www.microsoft.com&amp;quot;
    &lt;/span&gt;);
&lt;span style="color: blue"&gt;var &lt;/span&gt;result = &lt;span style="color: #2b91af"&gt;FSharpAsync&lt;/span&gt;.Parallel(urls.Select(Get)).Select(s =&amp;gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;.Join(&lt;span style="color: #a31515"&gt;&amp;quot;&amp;quot;&lt;/span&gt;, s)).Run();&lt;/pre&gt;&lt;p&gt;It may not be as powerful as F# async workflows, but still useful.&lt;/p&gt;&lt;h3&gt;BigInteger&lt;/h3&gt;&lt;p&gt;Another one for .NET 2.0 / 3.5 users. FSharp.Core includes System.Numerics.BigInteger for arbitrary-precision arithmetic. It doesn't have all of &lt;a href="http://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx"&gt;.NET 4 BigInteger&lt;/a&gt;'s methods, but it implements the basic operations. Want to calculate 23^25 + 4? No problem:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;var &lt;/span&gt;a = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BigInteger&lt;/span&gt;(23);
&lt;span style="color: blue"&gt;var &lt;/span&gt;b = &lt;span style="color: #2b91af"&gt;BigInteger&lt;/span&gt;.Pow(a, 25);
b += &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;BigInteger&lt;/span&gt;(4);
&lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(b);&lt;/pre&gt;&lt;p&gt;Result: 11045767571919545466173812409689947&lt;/p&gt;&lt;h3&gt;Lazy&lt;/h3&gt;&lt;p&gt;The Lazy&amp;lt;T&amp;gt; type is yet another feature that .NET 4 copied from F#, or so it seems. Are you still writing singletons the &amp;quot;old&amp;quot; way? With Lazy you can just do this in .NET 3.5 (using FSharpx-added sugar):&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MySingleton &lt;/span&gt;{
    &lt;span style="color: blue"&gt;private &lt;/span&gt;MySingleton() {}

    &lt;span style="color: blue"&gt;private static readonly &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Lazy&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MySingleton&lt;/span&gt;&amp;gt; instance = 
        &lt;span style="color: #2b91af"&gt;FSharpLazy&lt;/span&gt;.Create(() =&amp;gt; &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MySingleton&lt;/span&gt;());

    &lt;span style="color: blue"&gt;public static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MySingleton &lt;/span&gt;Instance {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;instance.Value; }
    }
}&lt;/pre&gt;&lt;p&gt;Although to be honest, I don't think I've ever used this.&lt;/p&gt;&lt;h3&gt;Enumerable cache&lt;/h3&gt;&lt;p&gt;Sometimes you have a forward-only iterator wrapped in an IEnumerable, like database results or some data parsed lazily from a web request, and you want to traverse it more than once, but you also want to keep it lazy, so ToList() doesn't cut it. With FSharp.Core you can cache it on demand using &lt;a href="http://msdn.microsoft.com/en-us/library/ee370430.aspx"&gt;Seq.cache&lt;/a&gt;, named SeqModule.Cache in C# / VB.NET.&lt;/p&gt;&lt;p&gt;&lt;a href="http://bartdesmet.net/blogs/bart/archive/2010/01/07/more-linq-with-system-interactive-functional-fun-and-taming-side-effects.aspx"&gt;System.Interactive also has a function like this&lt;/a&gt;, it's called MemoizeAll, although I like the F# name better as it seems to be more an application of caching than &lt;a href="http://en.wikipedia.org/wiki/Memoization"&gt;memoization&lt;/a&gt;.&lt;/p&gt;&lt;h3&gt;Enumerable zip&lt;/h3&gt;&lt;p&gt;Another nifty operator that is only available in .NET 4+. The one in FSharp.Core is slightly different: &lt;a href="http://msdn.microsoft.com/en-us/library/dd267698.aspx"&gt;Enumerable.Zip&lt;/a&gt; includes a mapper, its signature is:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TResult&amp;gt; Zip&amp;lt;TFirst, TSecond, TResult&amp;gt;(
    &lt;span style="color: blue"&gt;this &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TFirst&amp;gt; first,
    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;TSecond&amp;gt; second,
    &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;TFirst, TSecond, TResult&amp;gt; resultSelector)&lt;/pre&gt;&lt;p&gt;while &lt;a href="http://msdn.microsoft.com/en-us/library/ee370261.aspx"&gt;the one in F#&lt;/a&gt; (also in the static SeqModule class) zips directly to a tuple:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Tuple&lt;/span&gt;&amp;lt;T1, T2&amp;gt;&amp;gt; Zip&amp;lt;T1, T2&amp;gt;(&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T1&amp;gt; first, &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T2&amp;gt; second)&lt;/pre&gt;&lt;h3&gt;Conclusion&lt;/h3&gt;&lt;p&gt;If you're working with Visual Studio 2010, the F# runtime is a great library you can take advantage of, even in .NET 2.0 projects. And you already have it, so use it!&lt;/p&gt;&lt;p&gt;If you run .NET 3.5 or better, &lt;a href="https://github.com/fsharp/fsharpx"&gt;FSharpx&lt;/a&gt; makes it more C# friendly. It also makes it easier to interop with F# projects if you ever need it, since they use the same underlying types.&lt;/p&gt;&lt;p&gt;Even in .NET 4, persistent collections, discriminated unions and Option alone are easily worth the dependency.&lt;/p&gt;&lt;p&gt;Also worth mentioning is the &lt;a href="http://fsharppowerpack.codeplex.com/"&gt;F# PowerPack&lt;/a&gt;, a separate library implementing additional collections like HashMultiMap and LazyList and math-specific facilities such as rational and complex numbers, matrix, vectors.&lt;/p&gt;&lt;p&gt;And it's all open source, Apache-licensed.&lt;/p&gt;&lt;p&gt;PS: did you know &lt;a href="http://stackoverflow.com/questions/736629/parse-delimited-csv-in-net/736647#736647"&gt;the VB.NET runtime has a CSV parser&lt;/a&gt;?&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8643857899806162280-1595648084743450913?l=bugsquash.blogspot.com' alt='' /&gt;&lt;/div&gt;
&lt;p&gt;&lt;a href="http://feedads.g.doubleclick.net/~a/B7Bx-_ab4bDeYCvlvFXbhBxiarU/0/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/B7Bx-_ab4bDeYCvlvFXbhBxiarU/0/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;br/&gt;
&lt;a href="http://feedads.g.doubleclick.net/~a/B7Bx-_ab4bDeYCvlvFXbhBxiarU/1/da"&gt;&lt;img src="http://feedads.g.doubleclick.net/~a/B7Bx-_ab4bDeYCvlvFXbhBxiarU/1/di" border="0" ismap="true"&gt;&lt;/img&gt;&lt;/a&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/bugsquash/~4/bxmQZ8c2Fg0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://bugsquash.blogspot.com/feeds/1595648084743450913/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://www.blogger.com/comment.g?blogID=8643857899806162280&amp;postID=1595648084743450913" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/1595648084743450913?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/8643857899806162280/posts/default/1595648084743450913?v=2" /><link rel="alternate" type="text/html" href="http://bugsquash.blogspot.com/2011/10/10-reasons-to-use-f-runtime-in-your-c.html" title="10 reasons to use the F# runtime in your C# app" /><author><name>Mauricio Scheffer</name><uri>http://www.blogger.com/profile/15247972578064164206</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/_O0JK8vpXCXY/S0VmXRKILKI/AAAAAAAACSQ/ldj_FyJCE8w/s1600-R/2c87b8887d870df2747aceb2a44016e0%3Fs%3D128%26d%3Didenticon%26r%3DPG" /></author><thr:total>4</thr:total></entry></feed>

