Functional Programming in .NET | Blog | TomasP.Nethttp://tomasp.net/blogBlog2010, Tomas PetricekBlogDynamic in F#: Reading data from SQL databasehttp://tomasp.net/blog/dynamic-sql.aspxFri, 09 Jul 2010 02:03:00 GMTIn this article we look at working with SQL databases in F# using ADO.NET. We'll see how to use dynamic invoke operator to make the usual ADO.NET code remarkably elegant and simple...Tomas Petricek<p>When reading data from a SQL database in F#, you have a couple of options. You can use F# implementation of LINQ, which allows you to write queries directly in the F# language or you can use standard ADO.NET classes such as <code>SqlCommand</code>. The second option is often good enough - when you just need to call a simple query, you probably don't want to setup the whole LINQ infrastructure (generate classes, reference F# PowerPack, etc.) Unfortunately, ADO.NET classes are a bit ugly to use. When calling an SQL stored procedure, you need to specify the name of the procedure as a string, you need to add parameters one-by-one by creating instances of <code>SqlParameter</code>, you need to dynamically cast results from the <code>obj</code> type and so on...</p> <p>In this article, we'll look how to use the <em>dynamic operator</em> to make the experience of using ADO.NET from F# dramatically better. Dynamic operator (there are actually two of them) are a simple way of supporting dynamic invoke in F#. We can use it to write code that looks almost like an ordinary method call or property access, but is resolved dynamically at runtime (using the name of the method or property). The following example shows what we'll be able to write at the end of this article:</p> <pre lang="fsharp"> // Call 'GetProducts' procedure with 'CategoryID' set to 1 use conn = new DynamicSqlConnection(connectionString) use cmd = conn?GetProducts cmd?CategoryID &lt;- 1 conn.Open() // Read all products and print their names use reader = cmd.ExecuteReader() while reader.Read() do printfn "Product: %s" reader?ProductName </pre> <p>If you ever tried to call a SQL stored procedure directly using the <code>SqlCommand</code>, then you can surely appreciate the elegance of this code snippet. Let's now take a look at a larger example and some of the neat tricks that make this possible...</p> Recording and samples from my Reactive F# talkhttp://tomasp.net/blog/reactive-talk.aspxSun, 27 Jun 2010 14:49:28 GMTAlmost a week ago, I <a href="http://tomasp.net/blog/fun-londoners-reactive.aspx">posted an invitation</a> to my F# talk at the F#unctional Londoners user group. The talk has been recorded, so you can view it online now and you can also get all the Silverlight demos...Tomas Petricek<p>Almost a week ago, I <a href="http://tomasp.net/blog/fun-londoners-reactive.aspx">posted an invitation</a> to my F# talk at the F#unctional Londoners user group. The theme of my talk was <em>reactive programming</em>, which was also a topic of my <a href="http://tomasp.net/academic/reactive-thesis.aspx">Master's thesis</a> (defended earlier in June), so I really enjoyed talking about it. In the talk, I discussed the two approaches that you can use for developing reactive applications in F# (using examples in Silverlight): </p> <a href="http://skillsmatter.com/podcast/open-source-dot-net/tomas-petricek-reactive-programming-in-fsharp"> <img src="http://tomasp.net/articles/reactive-talk-london/pic.jpg" style="margin:10px; float:right; border-style:none;" /> </a> <ul> <li><strong>Declarative</strong> (or <em>data-flow</em> oriented) allows you to describe "what" should be done with data that your component receives from various events (such as mouse position etc.) This can be written using F# event combinators such as <code>Event.map</code> and <code>Event.scan</code>.</li> <li><strong>Imperative</strong> (or <em>control-flow</em> oriented) - in this style, we describe various states of the component (e.g. semaphore with green, orange and red) and describe transitions between the states. This can be written using F# <em>asynchronous workflows</em> and the <code>AwaitObservable</code> primitive (which you can get as part of the source code).</li> </ul> <p>Thanks to the folks from <a href="http://skillsmatter.com">SkillsMatter</a> who provided place for the meetup and helped with the organization, the talk was also recorded and is already available online! Below, you can also get all the nice Silverlight demos that I used during the talk...</p> <p>Thanks again to Carolyn Miller and <a href="http://www.trelford.com/blog/" type="external">Phil Trelford</a> for organizing the talk and also to <a href="http://blogs.msdn.com/b/dsyme">Don Syme</a> for taking me to the airport in the early morning (at 4 AM...) after the talk.</p> <h2>Links &amp; Resources</h2> <ul> <li><a href="http://skillsmatter.com/podcast/open-source-dot-net/tomas-petricek-reactive-programming-in-fsharp" type="external">Reactive Programming in F#</a> (recording) - Skills Matter</li> <li>Slides from the talk (<a href="http://tomasp.net/articles/reactive-talk-london/reactive_fsharp.pdf" type="external">PDF</a>, <a href="http://tomasp.net/articles/reactive-talk-london/reactive_fsharp.pptx" type="external">PPTX</a> formats)</li> <li><a href="http://tomasp.net/articles/reactive-talk-london/reactive_fsharp.zip" type="external">Reactive F# demos in Silverlight</a> (includes <code>AwaitObservable</code> implementation)</li> </ul>Reactive Programming with F# in Londonhttp://tomasp.net/blog/fun-londoners-reactive.aspxTue, 15 Jun 2010 02:54:39 GMTI'll be visiting London on June 23 and I'll be talking at the F#unctional Londoners user group. The theme of the talk is Reactive Programming with F# and you can expect many interesting F# features including asynchronous workflows and event combinators.Tomas Petricek<p>If you’re a registered member of the <a href="http://www.meetup.com/FSharpLondon/" type="external">F#unctional Londoners</a> user group, then you maybe already know that I'll be visiting London on <strong>June 23</strong> and I’ll be talking about <strong>Reactive programming with F#</strong>. If you're not a registered member and occasionally visit London, then you should definitely register. The user group is organized by Carolyn Miller and <a href="http://www.trelford.com/blog/" type="external">Phil Trelford</a> (whom I met some time ago at Microsoft Research in Cambridge). Among others, previous speakers include <a href="http://strangelights.com/blog/" type="external">Robert Pickering</a> (who also presented some samples based on my <a href="http://tomasp.net/blog/accelerator-intro.aspx">F# and Accelerator</a> series). Finally, another reason for joining the group is that it has a great name (as well as a logo)!</p> <img src="http://tomasp.net/img/londoners.png" style="float:right; margin:10px" /> <h2>When, what &amp; where?</h2> <ul> <li><strong><a href="http://www.meetup.com/FSharpLondon/calendar/13501027/" type="external">Tomas Petricek on Reactive Programming with F#</a></strong></li> <li><strong>Date &amp; time:</strong> 23 June (Wednesday), 6:30 PM</li> <li><strong>Location:</strong> The Skills Matter eXchange, 116-120 Goswell Road, London</li> </ul> <p>By the way, I'll also have a free copy of my <a href="http://www.manning.com/petricek" type="external">Real-World Functional Programming</a> book to give away during the talk!</p> <h2>Reactive Programming with F#</h2> <p>I'm sure you're already convinced to come. Nevertheless, you may still want to know what I'm going to talk about. There are many areas where F# offers clear benefits such as parallel &amp; concurrent programming. I believe that <strong>reactive programming</strong> is another great area for F#. In reactive programming, we face quite different problems than in other programming styles. We (as the authors of the application) can no longer specify what the application should do. Instead, the application needs to be able to handle many possible combinations of events. This aspect of programming is sometimes called <em>inversion of control</em>.</p> <p>Reactive programming is important for programming user interfaces, especially today when user interfaces are becoming more interactive and more "fancy". To demonstrate this, I'm working on some nice <strong>Silverlight</strong> demos for the talk! However, it is also needed to handle other kinds of events such as completion of background task or message from other application. We'll look at the two essential techniques that F# provides for reactive programming:</p> <ul> <li><strong>Declarative event combinators</strong> - one way of writing reactive applications is to specify the whole event processing declaratively by saying "what" should be done with occurrences of events. This is particularly useful when we need to encode simpler logic with a clear <em>data-flow</em>.</li> <li><strong>Imperative using workflows</strong> - for more complicated interactions, we can use <em>asynchronous workflows</em>. This makes the code more explicit, but we get full control over the <em>control-flow</em> of the application. Even though this approach is more "imperative" it can be used for writing nicely composable functional code as well.</li> </ul> <p>I'm looking forward to seeing you at the talk next week!</p>ASP.NET and F# (I.) - Creating MVC web applications in F#http://tomasp.net/blog/fsharp-mvc-web.aspxSun, 09 May 2010 20:43:52 GMTThis article shows how to use F# for developing ASP.NET web applications using the MVC framework. It also shows how to access data using LINQ in F# and how to simplify web programming using F# meta-programming, modules and records.Tomas Petricek<p>Some time ago, I wrote a couple of examples of <a href="http://tomasp.net/blog/aspnet-in-fsharp.aspx"> developing web applications in F# using ASP.NET</a>. Since then, the F# language and runtime has changed a little bit and there are also new technologies available in ASP.NET, so I thought I'd write a more up-to-date article on this topic. In this article, I'll present a simple "demo" F# web application that you can use as a starting point when creating your own projects (you'll also find a convenient Visual Studio 2010 template below). The article shows the following interesting things:</p> <ul> <li><strong>ASP.NET MVC</strong> - We're going to use ASP.NET MVC Framework to create the web application. As the article name suggests, most of the actual program code including models and controllers will be implemented in F#.</li> <li><strong>F# LINQ to SQL</strong> - The application uses a sample Northwind database and we'll write queries for selecting data from the database using LINQ support that's available in the <a href="http://fsharppowerpack.codeplex.com/" type="external">F# PowerPack</a>.</li> <li><strong>F# features</strong> - The application also uses some nice F# features that are quite useful for developing web applications. We'll use modules and records to implement the model and we'll also use advanced meta-programming features for constructing LINQ queries.</li> </ul> <p>If you want to use F# for creating an MVC application, you have a few options. It should be possible to create the web application solely as an F# project. However, we'll use a more convenient approach. We'll create a standard C# MVC project and move all the actual implementation to an F# library. We'll look at the application structure shortly. The following screenshot shows a page that lists products in the Northwind database:</p> <div style="text-align:center;"> <img src="http://tomasp.net/articles/fsharp-mvc-web/web.png" /> </div> Programming user interfaces using F# workflowshttp://tomasp.net/blog/excerpt-gui-workflows.aspxThu, 18 Feb 2010 00:25:51 GMTF# asynchronous workflows can be used to solve a wide range of programming problems. In this article we'll look how to use asynchronous workflows for elegantly expressing the control flow of interaction with the user. We'll also look at clear functional way for encoding drag and drop-like algorithm.Tomas Petricek<p>Numerous Manning partners already published several exceprts from my <a href="http://www.manning.com/petricek/" target="external">Real-World Functional Programming</a> book. You can find a list on the <a href="http://functional-programming.net/" type="external">book's web page</a>. However, the last excerpt published <a href="http://dotnetslackers.com/articles/net/Programming-user-interfaces-using-f-sharp-workflows.aspx" type="external">at DotNetSlackers</a> is particularly interesting. It discusses how to use F# asynchronous workflows to write GUI applications. This is a very powerful programming pattern that is very difficult to do in any other .NET language. We first discussed it with Don Syme during my internship at Microsoft Research and I found it very elegant, so I made some space for it in the book. In fact, the entire Chapter 16 discusses various reactive programming techniques that can be used in F#.</p> <img src="http://tomasp.net/articles/excerpt-gui-workflows/app.png" style="margin:15px;float:right;" /> <p style="padding-left:40px;padding-right:40px;font-style:italic;">When designing applications that don't react to external events, you have lots of control flow constructs available, such as if-then-else expressions, for loops and while loops in imperative languages, or recursion and higher-order functions in functional languages. Constructs like this make it easy to describe what the application does. The control flow is clearly visible in the source code, so drawing a flowchart to describe it is straightforward.</p> <p style="padding-left:40px;padding-right:40px;font-style:italic;">Understanding reactive applications is much more difficult. A typical C# application or GUI control that needs to react to multiple events usually involves mutable state. When an event occurs, it updates the state and may run more code in response to the event, depending on the current state. This architecture makes it quite difficult to understand the potential states of the application and the transitions between them. Using asynchronous workflows, we can write the code in a way that makes the control flow of the application visible even for reactive applications.</p> <p style="text-indent:0px">You can read the complete article here: <a href="http://dotnetslackers.com/articles/net/Programming-user-interfaces-using-f-sharp-workflows.aspx">Programming user interfaces using F# workflows </a> [<a href="http://dotnetslackers.com/articles/net/Programming-user-interfaces-using-f-sharp-workflows.aspx" target="_blank">^</a>]. It is also worth adding that Manning offers <strong>30% discount</strong> to DotNetSlackers readers (see the article for details!)</p>Deal of the day: Real-World Functional Programminghttp://tomasp.net/blog/book-deal.aspxSun, 24 Jan 2010 17:00:41 GMTSome time ago, I received my copies of <strong>Real-World Functional Programming</strong>. I started working on it back in May 2008 and as many people who had more experience with writing books told me, it took longer than I was expecting!Tomas Petricek<p>Some time ago, I received my copies of <strong>Real-World Functional Programming</strong>. I started working on it back in May 2008 and as many people who had more experience with writing books told me, it took longer than I was expecting! Anyway, I have to say, it was worth it, holding the actual printed book with my name on the cover is just fantastic!</p> <br /> <a href="http://manning.com/petricek"><img src="http://tomasp.net/articles/book-deal/book.jpg" style="border:0px; float:right;margin:10px;" /></a> <p>The goal of the book is to present functional programming concepts and ideas in a readable form. I wanted to create a book that will teach you how to <em>think functionally</em> without using the usual <em>shock therapy</em> that people usually feel when seeing functional programming for the first time. There are already a couple of reviews that suggest I was quite successful:</p> <ul> <li><em>Functional Programming for the Real World, by Tomas Petricek and Jon Skeet, has been a very helpful book for moving to F# from C#, as the authors do a fantastic job of helping to explain the differences between OOP and FP. </em><br /> <a href="http://www.amazon.com/Functional-Programming-Real-World-Examples/dp/1933988924" type="external">James Black at Amazon.com</a> </li> <li><em>This book isn’t just a simple introduction to programming in F#; it’s an introductory text on functional programming covering the many reasons why it is time for this programming paradigm to finally be accepted by mainstream programmers. And it also contains much more... </em><br /> <a href="http://www.simple-talk.com/community/blogs/clivet/archive/2010/01/03/85227.aspx" type="external">CliveT, Software Engineer at Red Gate Software</a> </li> <li>... and there are many other great comments about the book at <a href="http://www.manning.com/petricek">Manning book page</a>.</li> </ul> <h2>Deal of the day (January 24)</h2> <p>Finally, here is one great news if you're interested in getting the book! <strong>Real-World Functional Programming</strong> is Manning's Deal of the Day this Sunday, January 24. On this day, the print book is available for $20 from the <a href="http://www.manning.com/petricek">Manning website</a>, with code <code>dotd0124</code>.</p> Accelerator and F# (IV.): Composing computations with quotationshttp://tomasp.net/blog/accelerator-quotations.aspxTue, 12 Jan 2010 03:20:36 GMTIn this article series we're talking about the Accelerator project, which can be used from F# to write code that runs in parallel on GPU or multi-core CPU. In this article, we'll look at building complex data-parallel programs with F# quotations, we'll implement blur filter and we'll also discuss architecture and performance.Tomas Petricek<p>In this article series, we're talking about the Accelerator project and I'm presenting an F# library that I implemented, which allows you to use Accelerator [<a href="http://tomasp.net/blog/accelerator-quotations.aspx#accelfsh4links">references</a>] in a more sophisticated way. We've seen two examples of using Accelerator directly (see also <a href="http://tomasp.net/blog/accelerator-intro.aspx">introduction</a> and <a href="http://tomasp.net/blog/accelerator-life-game.aspx">Game of Life</a>). In the <a href="http://tomasp.net/blog/accelerator-dataparallel.aspx">previous article</a> I introduced my F# library for working with Accelerator. We've seen F# functions from the <code>DataParallel</code> module, we implemented an algorithm that rotates an image using these functions and finally, we've seen that we can take this ordinary F# code and run it using Accelerator. This is all possible thanks to F# quotations, which we can use to get an AST (a source code) of an F# function we wrote (if the function is marked in some special way).</p> <img src="http://tomasp.net/articles/accelerator-quotations/blur.png" alt="Blurred photo of Prague" style="float:right; margin:10px" /> <p>In this part of the series, we're going to look at working with quotations explicitly. We'll use meta-programming techniques to work with Accelerator. Meta-programming means writing programs that manipulate with other programs or pieces of code. This is exactly what we're going to do in this article. We'll write an F# function (running on CPU) that builds a program, which we'll then run using Accelerator.</p> <p>This is quite interesting approach, which isn't possible when we call Accelerator methods as standard F# functions or .NET methods. The benefit is that we'll clearly see which parts of program run on CPU and what parts execute on GPU or using X64 multi-core target. We could also perform more complicated optimizations with the code (because this wouldn't affect the readability). Just for your reference, here is the list of articles in this series in case you missed some of them:</p> <ul> <li><a href="http://tomasp.net/blog/accelerator-intro.aspx">Accelerator and F# (I.): Introduction and calculating PI</a></li> <li><a href="http://tomasp.net/blog/accelerator-life-game.aspx">Accelerator and F# (II.): The Game of Life on GPU</a></li> <li><a href="http://tomasp.net/blog/accelerator-dataparallel.aspx">Accelerator and F# (III.): Data-parallel programs using F# quotations</a></li> <li><strong>Accelerator and F# (IV.): Composing computations with quotations</strong></li> </ul> <p>However, enough with theory and let's take a look at some code samples! This time, we'll implement blurring of an image (also called convolution). Another example how to write this in F# using Accelerator is Satnam Singh's blog post [<a href="#accelfsh4links">4</a>]. Our example will be different, because we'll write the code as standard F# program and then have it translated to Accelerator automatically using quotations. We'll also talk about the architecture of the library that we're using and look at some performance results.</p> Accelerator and F# (III.): Data-parallel programs using F# quotationshttp://tomasp.net/blog/accelerator-dataparallel.aspxMon, 04 Jan 2010 12:50:13 GMTWe already discussed how to write programs that run on GPU using MSR Accelerator. In this article, we'll write an image rotation using data-parallel F# functions and then use a library that translates it to Accelerator automatically.Tomas Petricek<p>If you've been following this article series, you already know that Accelerator is a MSR library [<a href="http://tomasp.net/blog/accelerator-dataparallel.aspx#accelfsh3links">1</a>, <a href="#accelfsh3links">2</a>] that allows you to run code in parallel on either multi-core CPU or using shaders on GPU (see <a href="http://tomasp.net/blog/accelerator-intro.aspx">introduction</a>). We also discussed a direct way to use Accelerator from F# (by calling Accelerator methods directly) and <a href="http://tomasp.net/blog/accelerator-life-game.aspx">implemented Conway's Game of Life</a>. In this article, we'll look at more sophisticated way of using Accelerator from F#. We'll introduce F# quotations and look at translating 'normal' F# code to use Accelerator.</p> <img src="http://tomasp.net/articles/accelerator-dataparallel/rotated.png" alt="Rotated Prague photo" style="float:right; margin:10px" /> <p>In general, F# quotations allow us to treat F# code as data structure and manipulate with it. This is very similar to C# expression trees, but the F# implementation is more powerful. We can also mark a standard method or a function with a special attribute that tells the compiler to store quotation of the body. Then we can access the quotation and traverse it or modify it. In this article we'll use a function that takes an F# quotation (containing a limited set of functions) and executes it using MSR Accelerator. Implementing this functionality is a bit complicated, so we won't discuss the implementation now. We'll leave this for some future article of this series. In future, we'll also look at other interesting possibilities that we have when writing code using quotations. Here is a list of articles in this series and of the articles that I'm planning to add:</p> <ul> <li><a href="http://tomasp.net/blog/accelerator-intro.aspx">Accelerator and F# (I.): Introduction and calculating PI</a></li> <li><a href="http://tomasp.net/blog/accelerator-life-game.aspx">Accelerator and F# (II.): The Game of Life on GPU</a></li> <li><strong>Accelerator and F# (III.): Data-parallel programs using F# quotations</strong></li> <li><a href="http://tomasp.net/blog/accelerator-quotations.aspx">Accelerator and F# (IV.): Composing computations with quotations</a></li></ul> Accelerator and F# (II.): The Game of Life on GPUhttp://tomasp.net/blog/accelerator-life-game.aspxMon, 28 Dec 2009 21:16:03 GMTThis article shows how to use Accelerator from F#, which I <a href="accelerator-intro.aspx">already discussed</a>, to implement a massively parallel version of the famous Conway's Game of Life.Tomas Petricek<p>In the <a href="accelerator-intro.aspx">previous article</a>, I introduced the Microsoft Research Accelerator library. It allows us to write computations with arrays in C# and execute them in parallel on multi-core CPU or more interestingly, using GPU shaders. In the previous artcile, we've seen how Accelerator works and how it can be accessed from F#. In this article, we'll look at one more interesting F# demo - we'll implement the famous Conway's Game of Life [<a href="accelerator-life-game.aspx#accelfsh2links">1</a>] using Accelerator. We'll use a v2 version of Accelerator which has been announced just recently and is available from Microsoft Connect [<a href="accelerator-life-game.aspx#accelfsh2links">2</a>].</p> <div style="text-align:center;"> <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/wiZnTfx2re8&amp;hl=en&amp;fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wiZnTfx2re8&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object> </div> <p>This article is the second one from a series about using Accelerator from F#. Today, we'll use Accelerator types directly from F# - this is the simplest possible approach and is very similar to the way you'd work with Accelerator in C#. However, we can use some nice F# features such as custom operators to make the code more readable. In the next article, we'll discuss a different approach - we'll look how to execute more "standard" F# code (that doesn't reference Accelerator explicitly) with Accelerator using F# quotations. The list of articles may change, but here is a list of articles that I'm currently planning to write:</p> <ul> <li><a href="http://tomasp.net/blog/accelerator-intro.aspx">Accelerator and F# (I.): Introduction and calculating PI</a></li> <li><strong>Accelerator and F# (II.): The Game of Life on GPU</strong></li> <li><a href="http://tomasp.net/blog/accelerator-dataparallel.aspx">Accelerator and F# (III.): Data-parallel programs using F# quotations</a></li> <li><a href="http://tomasp.net/blog/accelerator-quotations.aspx">Accelerator and F# (IV.): Composing computations with quotations</a></li> </ul> Accelerator and F# (I.): Introduction and calculating PIhttp://tomasp.net/blog/accelerator-intro.aspxMon, 21 Dec 2009 03:21:03 GMTThis article shows how to write F# programs that run in parallel as shader programs on GPU or on multi-core CPU using Microsoft Research Accelerator project.Tomas Petricek<img src="http://tomasp.net/articles/accelerator-intro/montecarlo.png" alt="Calculating Pi using Monte-Carlo" style="float:left; margin:10px" /> <p>I already wrote about two projects that I worked on during an internship at MSR back in 2007 (<a href="http://tomasp.net/blog/aspnet-in-fsharp.aspx" type="external">ASP.NET support in F#</a> and <a href="http://tomasp.net/blog/fswebtools-intro.aspx" type="external">F# WebTools</a>). Even though this was more than 2 years ago (and I did one more internship at MSR in the meantime), I still have one more project that I never published on the web. The folks from the F# team reminded me of this project recently, so I thought I could finally publish it. The project used Microsoft Research Accelerator [<a href="#accelfsh1links">1</a>, <a href="#accelfsh1links">2</a>], which is a C# library for developing array-based computations and executing them on a GPU. More recently, the Accelerator team at MSR published Accelerator v2 [<a href="#accelfsh1links">3</a>], which was a good motivation to update my original project...</p> <p>In this article, we'll look at the simplest way of using Accelerator from F#. Accelerator provides a managed interface that can be naturally used from both C# and F#. We can use a mix of method calls and overloaded operators to describe a computation. In F#, we'll also define our additional custom operators to make the code a bit nicer. After we introduce Accelerator using a simple C# demo, we'll look how to calculate an approximate value of the PI number using a Monte-Carlo method.</p> <p>This article is the first one from a series about using Accelerator from F#. The list of articles may change, but here is a list of articles that I'm currently planning to write:</p> <ul> <li><strong>Accelerator and F# (I.): Introduction and calculating PI</strong></li> <li><a href="http://tomasp.net/blog/accelerator-life-game.aspx">Accelerator and F# (II.): The Game of Life on GPU</a></li> <li><a href="http://tomasp.net/blog/accelerator-dataparallel.aspx">Accelerator and F# (III.): Data-parallel programs using F# quotations</a></li> <li><a href="http://tomasp.net/blog/accelerator-quotations.aspx">Accelerator and F# (IV.): Composing computations with quotations</a></li> </ul> Real-World Functional Programming: Completed and printed!http://tomasp.net/blog/book-completed.aspxSat, 19 Dec 2009 21:54:28 GMTIf you're following my blog, you probably noticed that I was working on a book about functional programming using F# and C#. The great news is that the book is finally completed and will be available on December 24!Tomas Petricek<p>If you're following my blog or if you're interested in F# or functional programming in .NET, you probably noticed that I was working on a book <a href="http://manning.com/petricek">Real-World Functional Programming</a>. At some point, we called it <em>Functional Programming for the Real-World</em>, but then we changed the title back to a better sounding version <em>Real-World Functional Programming</em> (subtitle <em>With examples in F# and C#</em>). The book is also reason for a lower number of blog posts over the last year. Over the last month or so, we were doing the final edits, reviewing the final PDF version (I fixed quite a lot minor issues, synchronized book with the Beta 2 F# release and so on). Anyway, before a few days, I received the following email (as an author, I receive the same emails as those who ordered the book through the <a href="http://www.manning.com/about/meap.html">Manning Early Access Program</a>, so that I can see what we're sending to our dear readers):</p> <table><tr><td> <img style="margin:10px" src="/articles/book-completed/cover.png" /> </td><td> <p style="font-style:italic; margin:10px 30px 10px 10px; text-indent:0px;"> Dear Tomas Petricek, <br /> We are pleased to announce that <strong>Real-World Functional Programming</strong> is now complete! As a MEAP subscriber you can download your copy of the finished ebook right now! (...) This ebook is the final version, identical to the softbound edition, which is currently being printed and will be available on <strong>December 24</strong>. If you chose the printed book option when you originally subscribed, we'll ship it to you automatically—no action required from you. </p> </td></tr></table> <h2>Finally finished!</h2> <p>Yes, that's right. The book is finally completed and as far as I know, it has been printed last week! If you already ordered the book, you won't receive it before Christmas, but it should come shortly after. I can't wait to see the book actually <em>printed</em>. The transition from the Word drafts I initially wrote to a final PDF version was already felt fantastic and I thought <em>"It looks like a real book!"</em> Among other things, there are now graphical arrows with comments inside listings, which looks really great and makes code listings much easier to read. Now I can look forward to seeing the <em>actual book</em>. Maybe I'm too conservative, but I have to say that I'm really glad that I wrote the book before everything is going to be published just electronically!</p> <p>Here is a couple of links that you may found interesting if you want to look inside the book...</p>Functional Programming: Available Chapter Excerpts &amp; Discounthttp://tomasp.net/blog/functional-excerpts-and-discount.aspxSun, 26 Jul 2009 03:41:14 GMTThe work on my book Functional Programming for the Real World is slowly getting to the end. If you want to look inside, you can read some of the chapter excerpts that were published on the internet.Tomas Petricek<p>The work on my book <a href="http://www.functional-programming.net" target="external">Functional Programming for the Real World</a> is slowly getting to the end. I'm currently creating index for the last couple of chapters and doing final updates based on the feedback from reviews and also from the <a href="http://www.manning-sandbox.com/forum.jspa?forumID=460&amp;start=0" type="external">forum at manning.com</a> (this means that if you have some suggestions, it's the best time to post them - I haven't yet replied to all of them, but I'll certainly do that before the manuscript will go to the production).</p> F# Webcast (IV.) - Developing standard .NET librarieshttp://tomasp.net/blog/fsharp-webcast-objects.aspxMon, 15 Jun 2009 20:09:04 GMTThis webcast demonstrates how to wrap an existing F# code into an object oriented library that can be nicely used from C# and how to use it when developing ASP.NET web application. The demo uses script for processing RSS feeds from the previous webcast.Tomas Petricek<p>In the previous parts of this webcast series we've developed an F# script that downloads RSS feeds asynchronously and in parallel and searches them for the specified keywords. We followed the usual F# development style, so after introducing <a href="fsharp-webcast-functional.aspx">the basic functional concepts</a>, we wrote the code in <a href="fsharp-webcast-dotnet.aspx">the simples possible style</a> and demonstrated how to use <code>System.Xml</code> and <code>System.Net</code> namespaces. Then we <a href="fsharp-webcast-async.aspx">refactored the existing code</a>, to run asynchronously and process the results potentially in parallel, which was very easy thanks to F# <em>asynchronous workflows</em>.</p> <p>In this part of the series, we'll make the next evolutionary step of our sample application. We'll turn the code that originally used F# tuples and lists into code that uses standard .NET objects and we'll also see how to declare a class in F#. This simple modification will turn the script into an F# library that is almost indistinguishable from a library developed in C#. We'll also look how you can use the library from C# web application to show the interop between C# and F# in practice. We'll start with the code from the <a href="fsharp-webcast-async.aspx">previous part</a>, so if you missed that, you may want to check it out or download the source code.</p> F# Webcast (III.) - Using Asynchronous Workflowshttp://tomasp.net/blog/fsharp-webcast-async.aspxFri, 05 Jun 2009 03:39:39 GMTIn the previous part you've seen how to write a simple function for downloading RSS feeds and processing them. In this part, we look how to improve the function to download data asynchronously and process them potentially in parallel.Tomas Petricek<p>In this webcast, we'll look at improving the code for downloading and processing RSS feeds that I presented in <a href="fsharp-webcast-dotnet.aspx">the second part</a> (if you didn't see earlier parts, <a href="fsharp-webcast-dotnet.aspx">the first one</a> was an introduction to basic functional ideas). The previous part demonstrated how to use .NET libraries and we implemented a simple <code>downloadUrl</code> function for obtaining content of from the web and we've also seen how to load the data into an XML document object and how to filter items. In this part, we'll modify the code to run asynchronously and potentially in parallel. To use some of the functionality, you'll need to get <code>FSharp.PowerPack.dll</code>, which is available with the VS 2008 installation or as a separated download for VS 2010 [<a href="#fswc_asy">4</a>]. </p> <p>Now that we have the first version of the code, we can start <em>refactoring</em> it. I'm using the term in a slightly vague meaning - we're of course going to change the behavior of the code. We'll wrap it into F# <em>asynchronous workflow</em> to run without blocking threads and we'll also run multiple downloads in parallel. However, this can still be viewed as refactoring in some sense, because we're not changing the <em>core</em> behavior of the code. As you can see from the webcast, these kinds of refactorings are also very nicely supported by F# syntax...</p> F# Webcast (II.) - Using .NET librarieshttp://tomasp.net/blog/fsharp-webcast-dotnet.aspxMon, 01 Jun 2009 14:57:53 GMTThis is the second part of the webcast series that introduces the F# language. It shows how to use .NET libraries from F# to download RSS feed and how to work with the obtained data using tuples, sequence expressions and other F# features.Tomas Petricek<p>About a week ago I posted <a href="fsharp-webcast-functional.aspx">the first part</a> of my F# webcast series. It focused on explainining the basic ideas behind functional programming such as immutability, recursion and passing functions as arguments to other functions (or methods in C#). In the first part, we've seen some C# code to demonstrate the ideas and also a bit of F#, mainly to show the basic language features. </p> <p>The second part is going to be exclusively about F#. It'll demonstrate how we can start writing a demo application that grabs data from RSS feeds and processes them. You'll learn how to access .NET libraries from F# (in particular, we'll use <code>System.Net</code> and <code>System.Xml</code>). We'll develop the code iteratively, which means that we'll start by simply enumerating the RSS elements using <code>for</code> loop and printing the results and then we'll refactor the code to use <em>tuples</em> and <em>sequence expressions</em> to turn it into processing code that generates a sequence of feed items. Finally we'll also demonstrate how to use some of the functions from the previous part such as <code>List.filter</code> in practice.</p>F# Webcast (I.) - Introducing functional conceptshttp://tomasp.net/blog/fsharp-webcast-functional.aspxMon, 25 May 2009 13:39:54 GMTNow that Visual Studio 2010 beta 1 is out, it may be a good time finally try the new F# language. To make the exploration easier, I created a web cast series that demonstrates interesting aspects of F#. In this part, we'll look at basic functional concepts and working with data.Tomas Petricek<p>Now that <a href="http://www.microsoft.com/visualstudio/en-us/products/2010/default.mspx" type="external">Visual Studio 2010 Beta 1</a> is out, it is finally a good time to take a look at one of the (in my opinion) most interesting new features in the new release - <a href="http://blogs.msdn.com/dsyme/archive/2009/05/20/visual-studio-2010-beta1-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx" type="external">the F# language</a>. F# existed for quite a long time now as Microsoft Research project, but is now becoming a real Microsoft product. Interestingly, F# is still available as a <a href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=7bb32f32-9fac-4f34-ad56-b0bda130cf00" type="external">plugin for Visual Studio 2008</a>, so if you want to try it you don't have to install the whole new beta of 2010.</p> <p>There are already many resources for learning F# including my <a href="http://tomasp.net/blog/functional-overview.aspx">functional programming overview</a>, which is a Manning Greenpaper for the book <a href="http://www.functional-programming.net" type="external">Functional Programming for the Real World</a> that I'm writing with Jon Skeet and my <a href="http://tomasp.net/blog/fsharp-i-introduction.aspx">four-part F# introduction</a>. There are also some useful links on the official <a href="http://www.fsharp.net">F# web site</a> including some talk recordings. However, I haven't yet seen any good F# webcast focusing mainly on showing F# source code, starting from simple functional concepts to the real-world features like asynchronous workflows and object-oriented programming in F#, so I decided to create one.</p> <p style="text-indent:0px;"><strong>So, here it is...</strong></p> Imperative computation in F# (II.) - Writing break and continuehttp://tomasp.net/blog/imperative-ii-break.aspxSat, 25 Apr 2009 16:31:15 GMTIn the previous article of this series, we've implemented a computation expression that allows us to write imperative 'return' in F#. In this article, we'll add support for 'break' and 'continue'.Tomas Petricek<p>As I already wrote in the <a href="http://tomasp.net/articles/imperative-i-return.aspx">first part of this series</a>, the F# language doesn't support some of the language constructs known from imperative languages such as C#. In particular, we cannot use imperative <code>return</code> statement that returns the result of a function from any place in the function code. In functional languages, every construct is an expression, so to get the overall result of the function, the F# language evaluates the expression and the value of the expression is used as the result. In the previous article, we've seen that we can simulate this construct in the F# language using F# computation expressions and I showed how to implement computation named <code>imperative</code> that allows us to write for example the <code>exists</code> function for working with sequences like this:</p> <pre lang="fsharp"> let exists f inp = imperative { for v in inp do if f(v) then return true return false } </pre> <p>In this article, we're going to look at two more imperative constructs and we're going to talk about <code>break</code> and <code>continue</code>. We'll see that we can quite easily extend the computation builder from the previous article to allow writing code that is syntactically very close to what you would write in C#. As I already mentioned, there are of course some performance overheads when using computation expressions, but I find it very interesting how nice imperative syntax we can get in functional F#:</p> <pre lang="fsharp"> imperative { for x in 1 .. 10 do if (x % 3 = 0) then do! continue printfn "number = %d" x } </pre> <p>The only difference between this code and the code we'd probably write if F# supported <code>continue</code> as a keyword is that we need to wrap the code inside the <code>imperative</code> computation and that we need to add the <code>do!</code> primitive before the <code>continue</code> value. Now that we've seen an example of using the <code>continue</code> value inside the imperative computations, let's look how we can extend the computation builder from the previous article to add this feature...</p> Imperative computation in F# (I.) - Returning results from a functionhttp://tomasp.net/blog/imperative-i-return.aspxThu, 19 Mar 2009 02:05:03 GMTEven though F# supports some imperative constructs such as for and while loops, it doesn't support some of the advanced constructs like imperative return and break. In this series, we'll look how we can implement these constructs ourselves using F# computation expressions.Tomas Petricek<p>One of the limitations of F# is that it doesn't very well support some of the advanced imperative language constructs such as <code>break</code>, <code>continue</code> or imperative style of returning value from a function, meaning that you can't write something like <code>return false</code> in the middle of the function. This has good reasons. F# doesn't in principle have the notion of <em>currently executing statement</em> and instead treat every code you write as an expression. Clearly, when there is no <em>current statement</em>, we cannot jump to other statements. If you're looking for more information about these basic principles, you can take a look at my book <a href="http://www.functional-programming.net" target="external">Real World Functional Programming</a>, which covers this distinction in details in chapter 2, but we'll look at a brief example that will clarify this idea shortly.</p> <p>Often, there is really no need to use <code>break</code> or other imperative constructs in F#, because you can write the same thing more elegantly using one of the provided higher order function such as <code>Seq.exists</code> or <code>Seq.tryfind</code>. However, there are still some cases where the imperative programming style makes it easier to express our original intention. Also, implementing your own higher order functions (akin to <code>Seq.exists</code>) would sometimes be much easier if we could just use imperative return.</p> <p>So, what can be done about this?</p>Real World Functional Programming: Second review finished!http://tomasp.net/blog/functional-2nd-review.aspxMon, 02 Mar 2009 00:40:50 GMTI've been working on the <a href="http://www.functional-programming.net" target="external">Real World Functional programming in .NET</a> book for quite some time now. In fact, I had the first discussions with Michael Stephens from Manning in March last year and I started thinking about the book at that time...Tomas Petricek<p>I've been working on the <a href="http://www.functional-programming.net" target="external">Real World Functional Programming in .NET</a> book for quite some time now. In fact, I had the first discussions with Michael Stephens from Manning in March last year and I started thinking about the book at that time, so it has been occupying my mind for almost a year now! Until recently, I was feeling that we're not getting much closer to the end of this project, because writing a book is just <em>a lot</em> of work. However, I think I can finally see that we're getting closer to actually finishing the book. At Manning, we've recently finished the second review, which means that I've just got another set of very useful comments - a big thanks to all the reviewers! I'm also getting close to finishing the first draft of the whole manuscript (depending on the reviews, the content may still change a bit, but I expect to write at most one new chapter from now). Hopefully, the drafts will soon make it to the MEAP release of the book.</p> Source code for Real World Functional Programming available!http://tomasp.net/blog/book-source-available.aspxThu, 12 Feb 2009 02:10:59 GMTJust a short update regarding the book I'm working on. First of all, the source code for the first 11 chapters is now available! Secondly, I did some changes to the TOC and you can now find the commented TOC on the book web site.Tomas Petricek<a href="http://cid-8c3f26b4befe2dfa.skydrive.live.com/self.aspx/2009%20-%20Winter%20-%20Cambridge/P1060023.jpg" target="_blank"><img src="http://tomasp.net/articles/book-source-available/cambridge.jpg" class="ldecor" /></a> <p>As you can see, there has been quite a bit of silence on this blog for a while. There are two reasons for that - the first is that I'm still working on the book <a type="external" href="http://www.manning.com/petricek">Real World Functional Programming</a>, so all my writing activities are fully dedicated to the book. The second reason is that I'm having a great time doing an internship in the <a href="http://research.microsoft.com/en-us/groups/ppt">Programming Principles and Tools</a> group at Microsoft Research in Cambridge with the F# team and namely the F# language designer <a href="http://blogs.msdn.com/dsyme/" type="external">Don Syme</a>. The photo on the left side is the entrance gate to the Trinity College of the Cambridge University taken during the few days when there was a snow. I recently started using Live Gallery, so you can find more photos from Cambridge in my <a href="http://cid-8c3f26b4befe2dfa.skydrive.live.com/browse.aspx/2009%20-%20Winter%20-%20Cambridge" type="external">online gallery</a>. Anyway, I just wanted to post a quick update with some information (and downloads!) related to the book... </p> Functional Programming in .NET using C# and F# (Manning Greenpaper)http://tomasp.net/blog/functional-overview.aspxThu, 11 Dec 2008 01:48:07 GMTThis article is partially an excerpt from my <a href="http://www.manning.com/petricek">book</a>. It introduces the benefits of functional pogramming, and explains the essential aspects of the functional style using F# and C# 3.0. Finally, it also shows how functional style influences the application architecture.Tomas Petricek<p> Functional programming languages have been around for a while and were always astonishing for their ability to express the ideas in a succinct, declarative way allowing the developer to focus on the essence of problem rather than on technical details of the solution. Recently, functional paradigm is gaining new prominence as an efficient way to handle development of multi-processor, parallel and asynchronous applications.</p> <p> Functional ideas are arising in C# as well as in other main-stream languages and functional languages are becoming an alternative for real-world projects. Also, Microsoft recently introduced a new language called F#, which has a strong background in traditional functional languages, but as a .NET language also benefits from the rich .NET and Visual Studio ecosystem.</p> <div class="rdecor"> <img src="http://www.tomasp.net/img/cover_main.gif" alt="Book cover" /><br /><small>Available via MEAP | 500 pages<br />Softbound print: March 2009 (est.)</small></div> <p> This article is partially an excerpt from my book <strong><a href="http://www.manning.com/petricek">Real-world Functional Programming in .NET</a></strong> [<a href="#fpgpref">1</a>]. Thanks to my editors at <a href="http://www.manning.com" type="external">Manning</a> I have the permission to publish it on my blog. We’ll look at several aspects of functional programming and how the same concepts, which are essential for the functional paradigm, look in the F# and in C# 3.0 with LINQ. We will shortly look at the basic programming language features like lambda functions and type inference that are now available in both F# and C#. Functional programming isn’t only about language features, but also about using different programming style, so we’ll look at some high level concepts as well. These include using immutable data structures for developing code that can be executed in parallel and writing code in a more declarative style.</p> <p> Thanks to the combination of C# 3.0 and F#, this article shows the ideas in a way that should be familiar to you in C#, but also shows a further step that you can take with a primarilly functional language F#. If you're a .NET developer and you want to understand what functional programming is and how it can help you to become better and more productive then continue reading. If you'll find this article interesting, then don't forget to check out the <a href="http://www.manning.com/petricek">book</a>, which explains everything in larger detail and discusses many other interesting ideas.</p> Reactive Programming (IV.) - Developing a game in Reactive LINQhttp://tomasp.net/blog/reactive-iv-reactivegame.aspxMon, 24 Nov 2008 03:00:00 GMTIn this article, we'll implement a simple iteractve game using the <strong>Reactive LINQ</strong> project. The article shows that this way of handling events gives us amog other things a great degree of composability.Tomas Petricek<p>In this part of the article series about <strong>Reactive LINQ</strong>we're going to implement a slightly more complicated application using the library that I introduced in the previous three articles. We're going to use basic event stream queries from the <a href="reactive-ii-csevents.aspx">second article</a> as well as advanced operators introduced in the <a href="reactive-iii-linqoperators.aspx">third part</a>. This time, I'll also show the F# version of all the examples, so we're going to build on the ideas from the <a href="reactive-i-fsevents.aspx">first part</a>.</p> <p>I originally wanted to write the demo only in Visual Basic, because I think that it is really amazig to show an idea that came from functional programming in a language that no one (maybe until recently) connects with functional programming. Then I realized that I really want to show the F# version too, because F# was an inspiration for the whole <strong>Reactive LINQ</strong> idea and it is interesting alone as well. But finally, I thought that don't showing the C# version may look offensive to many readers (especially since I'm still C# MVP...). So, I ended up writing the game in all three languages, but the code is surprisingly similar in all of them!</p> Reactive Programming (III.) - Useful Reactive LINQ Operatorshttp://tomasp.net/blog/reactive-iii-linqoperators.aspxFri, 21 Nov 2008 19:59:02 GMTIn the previous article, I introduced <strong>Reactive LINQ</strong>. Today, we're going to look at other operators that canbe used for working with events. We'll see aggregation is useful and how to dynamically change (switch) behavior.Tomas Petricek<p>In the <a href="reactive-ii-csevents.aspx">previous article</a>, I introduced <strong>Reactive LINQ</strong>. I explained the different point of view that we can use when working with .NET events. The idea is that .NET events can be viewed as streams of values. The value is information about the event (such as position of a mouse click or a mouse movement). These streams can be processed using LINQ queries - we can for example filter all values that are not interesting for us using <code>where</code> LINQ clause. For example if we want to handle clicks only in some specified area.</p> <p>In the previous article, I talked about basic LINQ query operators such as <code>select</code> and <code>where</code> and some useful methods that <strong>Reactive LINQ</strong> provides (for example for merging event streams). Today, we'll take a look at two more advanced kinds of operations that we can use for working with event streams. In particular, we'll talk about aggregation operators (that you certainly know from LINQ) and about switching. Switching is a concept from functional reactive programming and it allows us to change dynamically how the application behaves. However, I'll explain this in a more detail soon.</p> <p>In this article, I'm going to use mostly C# (and some Visual Basic). The functionality that I'm describing in this part isn't part of the standard F# <code>Event</code> module that I discussed in <a href="reactive-i-fsevents.aspx">the first part</a>. I implemented most of them in F# too, but I'm not going to write the samples in both of the versions in this part. If you've seen the first two articles, you'll be definitely able to use the F# versions as well, because they follow exactly the same ideas as the C#/LINQ versions. I'm going to talk about a larger demo application in the last section and I'll show an F# version as well, so you'll see some F# examples in the next part. This part serves more as a reference of the available operators, so you may read only some parts of it, then jump to the last one (to see an exciting example!) and then come back here.</p> Reactive programming (II.) - Introducing Reactive LINQhttp://tomasp.net/blog/reactive-ii-csevents.aspxWed, 19 Nov 2008 19:57:15 GMTIn the second part of the series about reactive programming, I introduction the <strong>Reactive LINQ</strong> project, which allows writing event processing code in C# using LINQ queries.Tomas Petricek<p>In this article I'm going to introduce my project <strong>Reactive LINQ</strong>. This is largely inspired by the ideas that come from functional reactive programming in the Haskell language and from functionality that's available for working in events in F#. I introduced these ideas in my previous article in this mini-series, so if you're interested in learning more about these interesting technologies, you should definitely read the previous article about <a href="reactive-i-fsevents.aspx">First class events in F#</a>.</p> <p>In this article, we're going to look how to apply the same ideas to the C# language and how to use LINQ queries for processing events. This article is <a href="http://tomasp.net/blog/dynamic-linq-queries.aspx">just</a> <a href="http://tomasp.net/blog/csharp-async.aspx">another</a> my article that shows how to implement some idea from functional programmin in C#. I believe this will once again show that the new features in the C# 3.0 aren't just about querying databases, but are more widely useful even in situations that are not directly related to data-processing. </p> Reactive programming (I.) - First class events in F#http://tomasp.net/blog/reactive-i-fsevents.aspxSun, 16 Nov 2008 17:14:04 GMTThe LINQ project and changes in C# 3.0 are interesting because they allow implementing many ideas from functional languages in C#. In this article I'll explain "first-class events" in F# and later I'll implemet similar concept using LINQ.Tomas Petricek<p>I believe that the LINQ project and changes in C# 3.0 and VB 9 are interesting because they allow rewriting of many ideas from functional programming. An ability to express queries easily is one of these ideas, but it is definitely not the only one. There are many other interesting ideas. The C# 3.0 language isn't primary a functional language, so it isn't easy to discover the idea if you use only C#, but it is possible to implement it if you know the idea already.</p> <p>I already wrote a few interesting C# examples that were inspired by some functional idea. I'm a big fan of the F# language, so it is not a surprise that I started with an F# version of the problem and then looked at the way to do the same thing in C#. In particular, this is how my article about building dynamic queries in C# came to the existence - the F# version used <a href="http://tomasp.net/blog/dynamic-flinq.aspx">FLINQ and Quotations</a> and then I demonstrated how to do the same in <a href="http://tomasp.net/blog/dynamic-linq-queries.aspx">C# using expression trees</a>. Another example is my article about asynchronous programming in C# <a href="http://tomasp.net/blog/csharp-async.aspx">using iterators</a>, which shows how to implement something like F# <a href="http://blogs.msdn.com/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx">asynchronous workflows using iterators</a> in C# 2.0.</p> <h3>Functional Reactive Programming</h3> <p>Today, I'm going to look at another very interesting idea from functional programming. It is called <em>Functional Reactive Programming</em> and it comes from the Haskell community. You can find a list of related <a href="http://www.haskell.org/frp/" type="external">Haskell projects here</a>. However, similar things (though they are not purely functional and simplified) are available in the F# language as well. Don Syme introduced them in his blog post called <a href="http://blogs.msdn.com/dsyme/archive/2006/03/24/559582.aspx">F# First Class Events: Simplicity and Compositionality in Imperative Reactive Programming</a>. In this article, I'm going to briefly introduce the implementation available in F# and I'll extend it a little bit to allow some more interesting things. In the second article from this series, I'll show how to implement the same thing in C# 3.0 (and in VB 9 too!)</p> Calculating with infinite sequences on MSDNhttp://tomasp.net/blog/sequences-on-msdn.aspxThu, 13 Nov 2008 02:36:30 GMTAbout a year ago, I wrote an article about infinite sequences on MSDN. I remembered about it this week, because these topics will be discussed in the book I'm working on as well...Tomas Petricek<p>About a year ago, I wrote <a href="http://tomasp.net/blog/lazy-computation-on-msdn.aspx">an article</a> about using lazy computations in C# 3.0. It was published by the C# Community PM <a href="http://blogs.msdn.com/charlie/" type="external">Charlie Calvert</a> at the C# Developer Center. The article was a first of two articles where I wanted to demonstrate that C# 3.0 can be used for implementing useful constructs known from functional languages. I realized that I never posted the link to the second article to my blog, so you can find the annotation and link below.</p> <p>However, I remembered about these two articles because I was just working on chapters 11 and 12 of the <a href="http://tomasp.net/blog/functional-net-update.aspx">Real-world Functional Programming in .NET</a> book that I’m writing. Lazy values, which were the topic of my first article, are discussed in the second part of chapter 11 and IEnumerable and F# sequences are the topic for the first part of chapter 12. Because I already wrote two articles on this topic, I had to think really hard to find better (and still simple enough) examples where these concepts are useful in practice. I also finally have enough space to show how these two concepts relate and talk about some interesting background – for example in Haskell, lazy sequences are in fact just ordinary lists that are lazy thanks to the Haskell nature. </p> <p>A year ago, I definitely wouldn’t believe that today, I’ll be writing about the same topics, but this time as part of a book that has partly the same goal as these two articles – to show that functional programming ideas are really useful in the real-world and can enrich your programming toolbox (no matter whether you’re using C# or F# language). Anyway, here is the link to the second article – as usual when I look at something that I worked on a long time ago, I think I should rewrite it to make it better :-), but it still gives you an idea what is the book that I’m working on about...</p>Functional Programming in .NET book - An updatehttp://tomasp.net/blog/functional-net-update.aspxMon, 20 Oct 2008 22:10:04 GMTRecently, I announced on my blog that I’m working on a book for Manning called Real-world Functional Programming in .NET. It is more than a month since I wrote about it, so here is a brief update...Tomas Petricek<p>Recently, I announced on my blog that I’m working on a book for <a href="http://www.manning.com" type="external">Manning</a> called <em>Real world Functional Programming in .NET</em>. The goal of the book is to explain the most interesting and useful ideas of functional programming to a real world C# developer. I'm writing this book, because I believe that functional programming is becoming increasingly important. Here is a couple of reasons why you should have this book on your bookshelf:</p> <ul> <li><strong>Ideas behind C# 3.0 and LINQ</strong> - these main-stream technologies are inspired by functional programming and the new C# 3.0 features give us definitely much more than just a new way to query databases. The book explains the ideas behind these features and shows how to use them more efficiently.</li> <li><strong>Learning the F# language</strong> - <a href="http://blogs.msdn.com/somasegar/archive/2007/10/17/f-a-functional-programming-language.aspx" type="external">F# is becoming a first-class citizen</a> in the Visual Studio family of languages, which alone would be a good reason for learning it! Even if you're not going to use it for your next large .NET project, you'll find it useful for quick prototyping of ideas and testing how .NET libraries work thanks to the great interactive tools. </li> <li><strong>Real world examples</strong> - the book includes a large set of real-world examples that show how to develop real applications in a functional way - both in F# and C#. Among other things, the examples show how to utilize multi-core CPUs, how to better obtain and process data and how to implement animations and GUI applications in a functional way.</li> </ul> <p>The book is available via the MEAP (Manning Early Access Program) and if you want to get a better idea what is the book about, you can read the <a href="http://www.manning.com/petricek/" type="external">first chapter for free</a>. Anyway, it is more than a month since I <a href="http://tomasp.net/blog/functional-programming-book.aspx">posted the announcement</a>, so I decided to write a brief update....</p>Announcing: Real-world Functional Programming in .NEThttp://tomasp.net/blog/functional-programming-book.aspxTue, 02 Sep 2008 20:03:15 GMTIf you’ve been reading my blog or seen some my articles, you know that I’m a big fan of the F# language and functional programming style. Today, I'm very pleased to announce that I'm writing a book on this topic!Tomas Petricek<p>If you’ve been reading my blog or seen some my articles, you know that I’m a big fan of the F# language and functional programming style. I’m also often trying to present a bit different view of C# and LINQ – for me it is interesting mainly because it brings many functional features to a main-stream language and allows using of many of the functional patterns in a real-world. Elegant way for working with data, which is the most commonly used feature of C# 3.0, is just one example of this functional approach. Talking about real-world applications of functional programming, there is also fantastic news about F#. It was announced last year that F# will become fully supported Visual Studio language and the first CTP version of F# was released this week!</p> <p>I always thought that the topics mentioned in the previous paragraph are really interesting and that functional programming will continue to become more and more important. That’s why I’m really excited by the news that I’d like to announce today – I’m writing a book about functional programming in F# and C#....</p>F# Overview (IV.) - Language Oriented Programminghttp://tomasp.net/blog/fsharp-iv-lang.aspxSat, 03 Nov 2007 00:00:04 GMTIn the fourth article of the F# overview series, I will shortly describe how I understad the language oriented paradigm and how the F# language can be used for developing libraries using this paradigm.Tomas Petricek<p>In the fourth article of the F# overview series, I will shortly describe how I understad the language oriented paradigm and how the F# language can be used for developing libraries using this paradigm. We will look how discriminated unions relate to this paradigm and at three specific features that support this paradigm, namely <em>active patterns</em>, <em>computation expressions</em> and <em>quotations</em>.</p> <p>Defining precisely what the term <em>language oriented programming</em> means in context of the F# language would be difficult, so I will instead explain a few examples that will demonstrate how I understand it. In general, the goal of language oriented programming is to develop a <em>language</em> that would be suitable for some (more specific) class of tasks and use this language for solving these tasks. Of course, developing a real programming language is extremely complex problem, so there are several ways for making it easier. As the most elementary example, you can look at XML files (with certain schema) as language that are processed by your program and solve some specific problem (for example configuring the application). As a side note, I should mention that I'm not particularly happy with the term ‘language’ in this context, because the term can be used for describing a wide range of techniques from very trivial constructs to a complex object-oriented class libraries, but I have not seen any better term for the class of techniques that I’m going to talk about.</p>F# Overview (III.) - Imperative and Object-Oriented Programminghttp://tomasp.net/blog/fsharp-iii-oop.aspxSat, 03 Nov 2007 00:00:03 GMTIn the third part of the F# overview, we will look at the F# features that are essential for a smooth interoperability with other .NET languages and form a second part of the F# core language - that is object oriented and imperative programming.Tomas Petricek<p>In the third part of the F# Overview article series, we will look at language features that are mostly well known, because they are present in most of the currently used programming languages. Indeed, I'm talking about imperative programming, which is a common way for storing and manipulating application data and about object oriented programming which is used for structuring complex programs.</p> <p>In general, F# tries to make using them together with the functional constructs described in the <a href="fsharp-ii-functional.aspx">previous part</a> [<a href="fsharp-ii-functional.aspx" target="_blank">^</a>] as natural as possible, which yields several very powerful language constructs.</p>F# Overview (II.) - Functional Programminghttp://tomasp.net/blog/fsharp-ii-functional.aspxSat, 03 Nov 2007 00:00:02 GMTIn the second part of the F# overview we will look at functional programming, which is probably the most important paradigm used with the F# language, because F# is built on the same grounds as many functional languages.Tomas Petricek<p>In the second part of the F# overview we will look at functional programming, which is probably the most important paradigm used with the F# language, because F# is built on the same grounds as many functional languages. We will first examine the standard F# data types, which are useful to allow the functional programming style and we will also look at a few functional tricks.</p> <p>As already mentioned in the <a href="fsharp-i-introduction.aspx">Introduction</a> for this article series, F# is a typed functional language, by which I mean that types of all values are determined during the compile-time. However, thanks to the use of a type inference, the types are explicitly specified in the code very rarely as we will see in the following examples. Basic data types (aside from a standard set of primitive numeric and textual types that are present in any .NET language) available in F# are tuple, discriminated union, record, array, list, function and object. In the following quick overview, we will use the F# interactive, which is a tool that compiles and executes the entered text on the fly.</p> F# Overview (I.) - Introductionhttp://tomasp.net/blog/fsharp-i-introduction.aspxSat, 03 Nov 2007 00:00:01 GMTFirst article of the F# Overview series introduces the F# language and gives a quick overview of the programming paradigms that will be discussed in the upcoming articles.Tomas Petricek<p>In my bachelor thesis I included a short introduction that covered all of the important aspects of the F# programming language and I thought that it may be useful to extend it a little bit to cover also a topics that were not important for my thesis and post it as an article, so there is one and relatively short article that introduces all the interesting F# features. The article got however a bit longer than I expected, so I decided to split it into a three parts that would introduce three different <em>paradigms</em> that are supported by F#. Of course, this series won't teach you everything about F#, but it tries to cover the main F# design goals and (hopefully) presents all the features that make F# interesting and worth learning. In this first part I will shortly introduce F# and the supported paradigms that will be discussed in the upcoming articles.</p> Lazy Computation in C# on MSDNhttp://tomasp.net/blog/lazy-computation-on-msdn.aspxSat, 06 Oct 2007 01:29:59 GMTI think that one of the interesting things about C# 3.0 is that it gives you the ability to use many techniques known from functional languages...Tomas Petricek<p>I think that one of the interesting things about C# 3.0 is that it gives you the ability to use many techniques known from functional languages (like Haskell or F#). Most of the articles about C# 3.0 and LINQ focus on the queries and LINQ to SQL, but I believe that using these functional techniques deserve some attention as well. This is why I'm very happy that my article about one of these techniques - representing lazy computations - is now available at the C# Developer Center. I would like to thank to <a href="http://blogs.msdn.com/charlie/default.aspx">Charlie Calvert</a> [<a href="http://blogs.msdn.com/charlie/default.aspx" target="_blank">^</a>], who is the Community Program Manager for C# and who edited and published my article there. Here is the annotation:</p> <p style="padding-left:40px;padding-right:40px;font-style:italic;">Most of the programming languages used in practice (including for example C#, VB.NET, C++, Python or Java) employ so called eager evaluation, which means that the program evaluates all expression and statements in the order in which they are written, so all the preceding statements and expressions are evaluated before executing the next piece of code. This, for example, means that all arguments to a method call are evaluated before calling the method. Sometimes it may be useful to delay an execution of some code until the result is actually needed, either because the result may not be needed at all (but we can’t tell that before executing some computation) or because we don’t want to block the program for a long time by executing all computations in advance and instead we want to execute the computations later, when we will actually need the result. </p> <p style="padding-left:40px;margin-bottom:20px;padding-right:40px;font-style:italic;">In this article we will look how these lazy computations can be written in C# (using some of the new language features from version 3.0). We will first implement a Lazy class to represent this kind of computation, then look at a few simple examples to demonstrate how the class can be used, and finally we will examine one slightly more complicated, but practically useful application. </p> <p style="text-indent:0px">You can read the complete article here: <a href="http://msdn2.microsoft.com/en-us/vcsharp/bb870976.aspx">Lazy Computation in C# </a> [<a href="http://msdn2.microsoft.com/en-us/vcsharp/bb870976.aspx" target="_blank">^</a>]</p>Keep your multi-core CPU busy with F#http://tomasp.net/blog/fsparallelops.aspxSat, 24 Mar 2007 23:13:48 GMTCode which makes it possible to write applications that take advantage of multi-core CPUs by parallelizing F# filter and map functions.Tomas Petricek<p>The growth of computer CPU speed is slowly being replaced by the growth of number of CPUs (or cores) in the computer at least for the close future. This causes a revolution in the way software is written, because traditional and most widely used way of writing concurrent applications using threads is difficult and brings several serious issues. Some predictions say that within a few years, almost every computer will have about 16 cores, so there is a huge need for programming paradigms or idioms that help developers write concurrent software easily (see also <a href="http://www.gotw.ca/publications/concurrency-ddj.htm">The Free Lunch Is Over</a> [<a href="http://www.gotw.ca/publications/concurrency-ddj.htm" target="_blank">^</a>] written by Herb Sutter).</p> <p>Functional programming languages (especially pure functional languages) are interesting from this point of view, because the program doesn't have side-effects which makes it very easy to parallelize it (programs in pure functional languages can't have any side-effects by design, in other functional languages like F# the side-effects can be eliminated by following functional programming style). </p> <p>This article describes the code that makes it possible to parallelize some common F# constructs like the <code>List.map</code> and <code>List.filter</code>...</p>Concepts behind the C# 3.0 languagehttp://tomasp.net/blog/csharp3-concepts.aspxSun, 15 Oct 2006 15:19:03 GMTIn this article I'll describe concepts that influenced the design of the C# 3.0. Most of these concepts are known from other programming languages like Haskell, LISP or languages developed at Microsoft Research.Tomas Petricek<p>One of the lectures that I attended last year was <a href="http://www.mff.cuni.cz/toISO-8859-2.en/vnitro/is/sis/predmety/kod.php?kod=PRG003">Programming Methodology and Philosophy of Programming Languages</a>. The lecture was mostly about history of programming languages and how several features evolved, disappeared and than after many years appeared again in another programming language.</p><p>As I final work I decided to write an article that describes ideas that influenced the design of the C# 3.0 language. Some of these features are known from functional languages (for example from LISP or Haskell), some other were developed at Microsoft Research and appeared in the F# language or Cω. I also wanted to show in what ways are these features limited in the C# 3.0. I think that thanks to these limitation, the C# 3.0 is still a simple (or at least not difficult) to understand which is very important for mainstream language, but I find it interesting to know what is possible in other (less limited) languages.</p><ul><li>You can also <a href="http://tomasp.net/articles/csharp3-concepts/csharp3-concepts.pdf">download the article in PDF</a> (404kB)</li><li>The article is also available at <a href="http://www.codeproject.com/useritems/csharp3-concepts.asp">CodeProject.com</a></li></ul>