Silverlight Hack

Silverlight & related .NET technologies

About Me

Welcome to Silverlighthack.com.  This is a site where you can find many articles on Silverlight, Windows Phone 7 and .NET related technologies.  

My name is Bart Czernicki.  I have been working with computers since 1988 and have over 12 professional years in the IT field focusing on architecture, technology strategy and product management.  I currently work as a Sr. Software Architect at a large software development company.

Below is the cover of my new book that shows how Silverlight's unique RIA features can be applied to create next-generation business intelligence (BI 2.0) applications.

Silverlight 4 Business Intelligence Soft 

Contact: [email protected]

View Bart Czernickis profile on LinkedIn

NONE of the comments or opinions expressed here should be considered ofmy past or current employer(s).  The code provided is as-is without anyguarantees or warranties.

Are you ready to learn F#? Concepts to be familiar with before learning the F# language

I have been learning, absorbing and playing with F# for several months now.  I worked primarily with C# since 2002.  I figured I would write some .NET and developer concepts that one should be familiar with before trying to learn F#.  Hopefully, the list compiled below will help you determine if you are ready to learn F#.  If you are not familiar with most or any of these items below, it doesn't mean you can't learn (or shouldn't learn) F#.  However, in my opinion, the more you know from the items listed below, the easier it will be to learn the language.

Background Information

F# is a new functional programming language (from Microsoft) that is being released with .NET 4.0 and Visual Studio 2010 in March 2010.  The language is a "first-class" language and is completely integrated with the entire .NET ecosystem (debugger, tools, framework, support) like C#, C++.NET and VB.NET.  Therefore, F# is not like Delphi.NET, COBOL.NET or other third-rate .NET languages.  Functional languages are based on mathematics and this makes them ideal for statistical, financial operations on large data sets.  F# is a unique .NET language that will have many uses in the near future.

Programming Concepts

This is a list of programming concepts you should be familiar with before learning F# (Note: I ommited a lot of beginner programming concepts). You will see a lot of these concepts/patterns used in F# programs and examples.

  1. Data Structures - You should have a good understanding of the differences between a linked list, array, hash set, etc.  F# natively uses certain data structures (in underlying types) and knowing the differences between them will aid you when writing programs.  If you need a refresher, check out this FREE PDF book Data Structures and Algorithms Book.
  2. Big Oh Notation - You have to be able to understand the performance implications of your algorithms.  This is especially true for functional programming languages like F#.  If you do not know the difference between O(1) or O(n), you need to refresh your memory.  This is important when writing functional (non-imperative) style programs when doing many operations on a sequence or list of types.
  3. Type Inference - By default,  the F# compiler uses type inference to determine what types a function uses.  C# has partial type inference using the var keyword.  However, in C#, you still need to pass in the type explicitly for method signatures.  Being used to not seeing these "type aids" will help you understand F# syntax faster.
  4. Immutability -  In object-based languages such as C#, C++ or VB.NET the created objects are mutable.  This means that their state can be changed.  Functional programming languages are by default immutable and do not allow the state of the value/variable to change after it has been assigned a value.  For more information on immutability go here.
  5. Recursion - Recursion is the ability of a piece of code to call itself over and over until the desired result has been completed.  In C#, methods can be recursive.  In SQL (SQL 2005), a developer can use CTEs (common table expressions) to recursively iterate through the same algorithms.  This pattern is very popular in functional programming and whatever F# book you pick up, this will be one of the first chapters you read.  If you are familiar with the more advanced topic of recursion like "tail recursion," that will be really beneficial as well.  For more information on recursion, click here.
  6. Closures - Closures are a more advanced topic that most developers use every day without even knowing it.  Closures allow you to use constructs that are declared outside of the scope of the function as if they were part of the local scope.  I recommend Jon Skeet's book to fully understand them.  A link to an article on his website can be found here.
  7. Currying -Function/method currying is the process of creating new functions/methods from existing functions/methods and their parameters.  You will see function currying in a lot of F# examples as it makes code simpler and easier to read.  For more information on currying, click here.
  8. Iterator pattern - I think this is one of the more important design patterns.  For example, as a .NET developer, you should know the difference between a List type and an IEnumerable and how to use the yield keyword to implement the iterator pattern in C# (In F#, an iterator is implemented in a sequence).  As mentioned above, functional programming languages are designed to work with lots of data.  Therefore, optimizing data structure access is essential to create performant F# modules.  For a great article on the iterator pattern, please look at Juwal Lowy's MSDN article.
  9. Asynchronous Programming - Since F# is by default immutable and tries to get the developer to program without side effects, this makes it great for asynchronous programming.  The reason is that by definition, all constructs should be thread-safe since their state cannot change.  Knowing about async programming helps, but it does not mean you can't do synchronous F# programs.
  10. Writing Clean Code - In my opinion, this is probably the most important one of all.  When writing C# code, you have curly brackets and semicolons to aid you in laying out your code and determining scope.  However, in F#, the way you lay out code matters!  This means whitespace and bad code formatting could create unwanted bugs.  The reason I mention this as important is that this could be a hard habit to break.  For example, learning about closures could take a very small amount of time compared to breaking a bad habit in formatting your code.
  11. Other functional languages - This is an obvious one. If you have experience with other functional languages such as Haskell or OCaml, you will obviously feel right at home with F#.

.NET/C# Concepts that Translate to F#

This is a list of .NET/C# concepts that translate well into F# and will help you understand functional programming more easily.

  1. LINQ/SQL - If you have used LINQ or SQL, this will dramatically help you understand how to write functional algorithms.  LINQ/SQL favor declerative (opposed to imperative) syntax like F#.
  2. Lambda Syntax - If you are familiar with lambda syntax in C#, you will see a very similar lambda syntax in F#.
  3. Extension Methods - This could be associated with knowing LINQ but it is worth mentioning here as extension methods are essential in "fluent" code.
  4. Generics - Generics were introduced to the .NET framework to aid in code reuse.  In the same manner that you can write a generic method in C#, you can write a generic function in F#.
  5. Delegates and Simplified Delegate Syntax - If you have used delegates as parameters or to pass pieces of code around, then the functional composition concept will be familiar to you in F#.  You can read my Evoloution of C# Delegate Syntax article to understand how delegates allow you to write code in a more functional paradigm using C#.
  6. Parallel Task Library/PLINQ - Writing algorithms that can take advantage of many cores should not be hard.  This is exactly what the Parallel Task Library/PLINQ allows you to do in .NET 4.0 in a simpler, functional way.  These concepts are closely aligned to F# asynchronous workflows which allow concurrency (even in Silverlight!!)
  7. var Keyword (type inference) - Being familiar with the var keyword in C# will get you familiar with type inference in F# faster.  Most F# examples make use of this feature and declare types explicitly only when needed.

Summary

If you read reviews of available F# books, some are negative - in my opinion, unfairly - because the developers expect to be taught everything.  There are a lot of core concepts listed above that a single F# resource simply cannot cover.   Understand when purchasing F# resources, you are expected to know a decent amount of these developer topics.  I hope this list aids you in the way you approach learning F# either by jumping into F# directly or by brushing up on some intermediate/advanced programming concepts.

Getting started with F#

Information on F# has been around for several years.  There are many articles, white papers and books on F#.  However, you do have to be careful and get content that is recent and relative to the current F# release.  The F# specification has changed dramatically over the last several months as the language was being "productized" and many methods/functions simply don't exist in F# anymore.   Below are some links I put together where to get started.

Videos

  • Getting Started with F# - This video (from the 2008 PDC) is the place to start if you have zero F# experience and/or if you want a really good introduction into functional programming and some F# examples.

Books (I have read four different F# books and the two below are by far the best ones and most current.  Don Syme's F# book is good but a little outdated.)

Websites

  • http://www.fsharp.net/- This the main Microsoft F# page.  Includes numerous links, downloads for Visual Studio, F# extensions, etc.
  • Don Syme's blog - Don Syme is one of the main "creators" of F#.  His blog is a must read to get insight into advanced topics and upcoming F# news.

 

Posted: Dec 06 2009, 12:29 by Bart Czernicki | Comments (3) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: .net | F#
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Silverlight 3 and F# Support in Visual Studio 2010

The goal of this blog post is to make you aware of F# support in Silverlight in Visual Studio 2010.  In addition, this blog post shows an example why F# is going to be very important for Silverlight architects and developers.  Note:  This is NOT an intro to F#.

Demo and Visual Studio 2010 source code are provided with this article.

In Visual Studio 2010, F# is a first-class language.  Over the last several months, the language has been "productized."  It is officially part of the Visual Studio 2010 ecosystem (tools and .NET 4.0 framework).  However, F# is not limited to the full .NET framework; it is also available in the Silverlight runtime.  This is really exciting as this allows you to take advantage of functional programming features in Silverlight.

 

In Visual Studio 2010, the "F# Silverlight Library" project is natively supported.

 

Silverlight 3 application that includes an F# assembly.

  

Why should I care about F# in Silverlight?

Most C# developers/architects who do not have experience with functional programming language features could assume that F# is another language like Visual Basic, J# or C++.  It would be wrong to compare F# to any of these languages (including C#).  As a functional language, F# allows developers to design software using the functional paradigm.  What does that mean?  This means functional patterns such as interactive programming, pattern matching, immutable data types, type inference, lambda expressions, workflows, higher-order functions, discriminated unions, etc., are all included in F#.  While taking advantage of these features, a developer using F# can use the type system, implement object-oriented design and make use of the .NET classes.  This makes F# a very unique language that combines all the features of a first-class .NET language (i.e., C#) and a professional functional language.

Note: F# compiles to IL just like any other .NET language.  However, F# has been designed from the ground up over the last couple of years.  Therefore, the IL it generates has been optimized and many new language optimization features have been implemented.  For example, you may have seen blog posts that compare C# and F# syntax that shows F# compiling into cleaner IL and faster code.  Conversely, C#'s core functionality has not been redesigned in about a decade (C#/.NET has been extended with functional features such as type inference, extension methods, lambdas, AST, LINQ, etc.)

 

Show me some code that proves how F# can add value to Silverlight

This short blog post is not meant to be an F# introduction (Please see links below for intro material).  However, I did want to pick at least one compelling feature that really shows off the power and functional brevity of F# married with Silverlight functionality.  The feature that I decided to show off in a Silverlight F# module is a concurrent programming pattern.

If you do not already know, Silverlight supports distributing logic up to eight processor cores (This frustrates me that Microsoft doesn't adverstise this enough). In my opinion, concurrent programming is THE biggest feature of Silverlight over other RIA technologies (i.e., Flash, JavaFX).  However, implementing asynchronous patterns in Silverlight is not trivial.  The callback pattern is messy and leads to a lot of code.  Silverlight runs in an STA mode and requires UI elements to only be modified by the main thread (dispatching or synchronization).  Even asynchronous constructs like the BackgroundWorker that are supposed to make concurrent programming simple lead to many messy lines of code with events.  Wouldn't it be great to get the benefit of asynchronous programming without having to write many lines of additional code?

One of the fantastic features of F# is that the default type immutability and functional design allows the language to express the desired result declaratively.  This means you write F# syntax by telling the program what you want it to do rather than how to do it.  For example, if you are using constructs that use secondary threads and allocate code to these threads, then you are writing imperative code.  A lot of lines of code are wasted telling the code how we want it to behave.  Wouldn't it be better to define pieces of logic and simply say that we want them to execute in parallel?  Functional declarative code in F# can do exactly that.  F# includes a feature called asynchronous workflows that allow you to write logic by composition with the aim to be executed in parallel.

Initial Steps

  • I created a simple Silverlight application.
  • I added an "F# Silverlight Library" project
  • In the F# assembly, I added an F# code file called ConcurrencyTest.fs.
  • I created an F# function called addNumbers that takes two parameter:
    • The first parameter is an accumulator that is the existing sum in the running sequence (This will be passed by the reduce sequence function).
    • The second parameter is the number in the sequence we want to add.
  • I created an F# function called squareNumber that takes two parameters:
    • The first parameter is an accumulator that is the existing square sum in the running sequence (This will be passed by the reduce sequence function).
    • The second parameter is the number in the sequence we want to square.
  • I created an F# function (equivelant to a C# method) that takes no input parameters named PerformTwoLongRunningTasks:
    • Inside that function, I defined two tasks that use the addNumbers and squareNumber functions to add the numbers across a sequence of numbers from 0 to 10,000,000
    • The PerformTwoLongRunningTasks function returns an array of 64-bit integers.  The array of 64-bit integers are the results of the two aggregates functions.
    • The first function returns the sum of the numbers.  The second function returns the square of the numbers.

The F# static method/function is shown below (Some familiarity with F# is required to understand what is going on, but I commented on the code for those unfamiliar with F#):


Now that we have this function (static method) defined in our F# library, it is ready to be used.  In the Silverlight application, we can add a reference to the F# library and then we can call the function (static method) as shown below in our C# Silverlight MainPage class.  Note that below we are calling the function just like we would call a static method with the [Name of Class].[Name of public function].  Notice also that because F# uses the base .NET type system, the returned array of integers does not need special casting/marshalling to C#.

Parallizing the Tasks

Now it is time to parallize the two tasks and scale it across multiple logical/physical processor cores.  As mentioned earlier, we are going to accomplish this with asynchronous workflows. In the figure below, I highlighted the changes to the function and it now scales across multiple cores.  This was done in two steps:

  • Wrap the body of the member functions with the async {... } syntax.
    • Add a return keyword (This signifies the return parameter/completion of the body).
    • The body of the function Seq.reduce addNumbers {0L..10000000L} now becomes async { return Seq.reduce addNumbers {0L..10000000L} }.
  • The main function (PerformTwoLongRunningTasks) now returns an array of async tasks.  These can be thought of delegates that need an Invoke-type command to execute them.  We simply change let results = [| task1; task2 |] to let results = Async.RunSynchronously (Async.Parallel [ task1; task2 ]).
    • All this does is tells the F# compiler to parallize these tasks.  However, run the result synchronously.  Therefore, the code returns to the main thread and we do not need dispatching/synchornization, etc.  This is analogous to creating a wait event in C# and having the two pieces of logic scheduled on secondary threads and waiting for this process to come back.


Asynchronous workflows allowed us to simply wrap the tasks as an "async function" declaratively.  This is a very important distinction as we just declared the logic and told it we want this to run in parallel.  Notice we didn't tell it "how to run in parallel" (imperative code).  Therefore, we did not have to:

  • explicitly start, stop threads (i.e., Thread.Start)
  • use BackgroundWorker
  • use callbacks (AsyncCallBack)
  • use BeginExecute asynchronous pattern

We simply told the code we want the tasks distributed among multiple cores and let the F# compiler figure out the hard part and generate the IL.  This is really powerful and in the very near future of "declarative parallel processing".  This is an example of what I mentioned earlier that the F# compiler has been designed recently and can include this type of "magic" right in the language.  C# does not have this feature and needs to be extended to provide this kind of automation.


Can't I do this with PLINQ?

Those familiar with the Parallel Extensions Libraries that will be added to .NET 4.0 might be aware that LINQ has been extended with parallelism features.  This allows you to extend your existing LINQ queries with parallism features.  This is known as PLINQ (parallel LINQ).  For example, a screen shot below shows a LINQ query that can be easily parallized by simply adding the AsParallel() extension method.

Unfortunately, the Parallel Extensions Library will NOT be available for Silverlight for the Visual Studio 2010 release.  However, I think that there is a good chance future versions of Silverlight will eventually get this feature.  There are some big features in the Silverlight assemblies that are simply missing and need to be added before features like PLINQ can be added.  This is exactly where asynchronous workflows can be substituted for PLINQ in a Silverlight application.  If you are working with large data sets, complex math computations, AI for a game, etc., parallelizing your code in F# libraries makes pefect sense for a Silverlight application.  This is MUCH easier than writing your own imperative multithreaded code.

How about the Performance?

I extended the functions and created functions for two, four and eight tasks and put this in a Silverlight application.  On my workstation (four cores), there was about a 50% improvement by parallelizing the two tasks.  With four tasks, the improvement in performance was about 3.5x.  As you can see, asynchronous workflows can easily dramatically improve the performance of your code on the Silverlight client.

Click the picture below to launch the demo application

 

Getting started with F#

Information on F# has been around for several years.  There are many articles, white papers, and books on F#.  However, you do have to be careful and get content that is recent and relative to the current F# release.  The F# specification has changed dramatically over the last several months as the language was being "productized" and many methods/functions simply don't exist in F# anymore.   Below are some links I put together where to get started.

Videos

  • Getting Started with F# - This video (from the 2008 PDC) is the place to start if you have zero F# experience and/or if you want a really good introduction into functional programming and some F# examples.

Books (I have read four different F# books and the two below are by far the best ones and most current.  Don Syme's F# book is good but a little outdated.)

Websites

  • http://www.fsharp.net/- This the main Microsoft F# page.  Includes numerours links, downloads for Visual Studio, F# extensions, etc.
  • Don Syme's blog - Don Syme is one of the main "creators" of F#.  His blog is a must read to get insight into advanced topics and upcoming F# news.

 

I hope this post gets Silverlight architects/developers exited about upcoming F# support.  F# is another tool that software architects can use that helps them create better software.  Furthermore, F# suport is another example of Microsoft's ability to integrate the Silverlight runtime across the .NET development stack.

Posted: Nov 04 2009, 16:16 by Bart Czernicki | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us