.Net

Real World Functional Programming With examples in F# and C#

September 2nd, 2008 / Development in a Blink

Tomas Petricek, who interned under the supervision of Don Syme, is publishing.

Book cover

“Thinking differently about problems" is available for free.

Parallelization of functional programs

Imagine that we’re writing a computer game and we have a list of characters controlled by the computer. In a single step of the game, we want to remove all characters that died since the last step and we want to calculate new state of the game character (such as new location). Also a character can perform some operation with the world (for example picking an item from the world).

Imperative Solution

List<GameCharacter> dead = new List<GameCharacter>();

foreach(GameCharacter ch in Characters) {
  if (!CharacterIsAlive(ch))
    dead.Add(ch);
  else
    ch.PerformStep(this.World);
}

Characters.RemoveAll(dead);

Functional Version

  1 Characters = Characters.Filter(CharacterIsAlive);
  2 Characters = Characters.Select(PerformStep);
  3 this.World = Characters.Aggregate(this.World, ModifyWorld);
  1. Filter out dead characters
  2. Calculate new state for each character
  3. Calculate new state of the world

Prism 2.0 to support Silverlight

August 31st, 2008 / Tales from a Trading Desk

Finally it sounds like somebody at Microsoft has seen the light, with Prism 2.0 offering Silverlight support. More interesting is the hint that binary compatability between WPF and Silverlight is on the cards -madness if this doesn’t happen. “We’d like to get to the point where views, styles and themes can all be re-used across WPF and Silverlight” - so would we all! “Code that is shared is linked between two projects so that it is automatically compiled into each target” ( Painful!

Silverlight Spy

Silverlight 2.0 RTM? Silverlight vNext

August 28th, 2008 / Tales from a Trading Desk

By my reckoning we are due for Silverlight 2.0 RTM sometime shortly given that beta 2 was released in June. Having played with beta 2 I still think Microsoft has some way to go to catch Adobe. Specifically I think they need to consider the following for vNext:

  • More controls - at a mininum the equivalent controls that are available in Flex 3 e.g accordian
  • Full commanding support
  • Better parity with WPF
    • Triggers
    • Richer data-binding
    • 3D support
    • Bitmap Effects
    • FlowDocument
    • Better rendering control (e.g. WPF SnapsToDevicePixel)
  • A sensible push service in IIS, not this silly polling implementation! Think Lightstream, Liberator…..
  • DataGrid and LINQ select anonymous types in your queries support

Interesting Silverlight reading:

Off-topic stuff:

F# For Scientists Misses the Boat On Mathematica Performance

August 26th, 2008 / Cogitatio
I recently purchased F# For Scientists by Dr. Jon Harrop after the author mentioned it on the Mathematica Mailing List. According to Dr. Harrop,

Mathematica's .NET-Link technology allows Mathematica and .NET programs to interoperate seamlessly. Moreover, Microsoft's new functional programming language F# provides many familiar benefits to Mathematica programmers:
The marriage of Mathematica with F# can greatly improve productivity for a wide variety of tasks.

I am a big fan of Mathematica and functional programming and have been wanting to check out F# for some time so I decided to give the book a shot. It just arrived today so I can't post a full review but I did jump directly to the small section (5 pages) on using F# with Mathematica.

What did I learn? Well this section rightly claims that Mathematica has awesome symbolic math capabilities (it does). But then it goes on to claim that F# can beat the pants off of Mathematica on raw calculation. Thus it suggested F# programmers should call out to Mathematica for symbolic integration but then evaluate the result in F# for speed (to the tune of 3.4 times Mathematica's speed). I was naturally dubious. The explanation of this speed up is give as

The single most important reason for this speed boost is the specialization of the F# code compared to Mathematica's own general purpose term rewriter. ... Moreover, the F# programming language also excels at compiler writing and the JIT-compilation capabilities of the .NET platform make it ideally suited to the construction of custom evaluators that are compiled down to native code before being executed. This approach is typically orders of magnitude faster than evaluation in a standalone generic term rewriting system like Mathematica.


Okay, hold the phone! First off, I did not know the F# language could write compilers. I'll forgive this as poetic use of language. I guess I sort of know what he meant to say. More interesting is that we have gone from 3.7 times to "orders of magnitude". Now, I don't take anything away from the brilliant folks at Microsoft, but the equally brilliant folks at Wolfram have been focusing exclusively on mathematics software for 20 years and you might think they learned a thing or two about computational speed!

Here is the example from the book...

First, he uses Mathematica to integrate a function.


Integrate[Sqrt[Tan[x]],x]

(-2*ArcTan[1 - Sqrt[2]*Sqrt[Tan[x]]] +
2*ArcTan[1 + Sqrt[2]*Sqrt[Tan[x]]] +
Log[-1 + Sqrt[2]*Sqrt[Tan[x]] - Tan[x]] -
Log[1 + Sqrt[2]*Sqrt[Tan[x]] + Tan[x]])/(2*Sqrt[2])


He then goes to show that Mathematica takes 26 secondsto evaluate this function in loop for 360,000 iterations.

He then shows a translator that converts the Mathematica to F# and the F# code does the same work in 7.595 seconds.

So far Dr. Harrop is correct but like some many others who are in a rush to show their new favorite language superior to another's, he forgets to read the manual! Particularly, the section on optimization! If he had he would have found a handy little Mathematica function called Compile. Hmm, sounds promising. And in fact....


cf = Compile[{{x, _Complex}}, Evaluate[Integrate[Sqrt[Tan[x]],x]]]
Timing[Do[cf[x + y I],{x,-3.0,3.0,0.01},{y,-3.0,3.0,0.01}]]

{5.281,Null}

That's 5.281 seconds on my relatively underpowered laptop (Thinkpad X60) !

Some might feel I'm being a bit harsh on Dr. Harrop but after all he made me layout bucks for a book that promised me "many familiar benefits" only to deliver 5 measly pages of half truth. F# programmers may benefit from Mathematica but the jury is still out as to whether the reverse is true.

FlexUnit, Silverlight and other Random Thoughts

August 26th, 2008 / Tales from a Trading Desk

  • FlexUnit is now out in Open Source land. Hopefully Adobe can begin to provide more enterprise development tools. The one downside of FlexUnit is that there appears to be no console mode, which is unfortunate is your running on an enterprise build plant (
  • No surprises in this list of Silverlight RIA applications. It’s unfortunate that the RIA I was instrumental in building last year isn’t in this list (
  • Oslo insights
  • Looks like Steve had to pull the BWM surface video :(, but at least there are a number of other Vectorform Surface video’s still online - Mercedes-Benz etc
  • REST with WCF - way to much REST in my life these days
  • C++0x: Interview with Bjarne Stroustrup
  • With Amazon continuing to add to the cloud, one has to ask if Oslo will provide the Microsoft Cloud?
  • NVISION 2008 has some impressive video’s. CPU’s continue to play to “financial market analyses”
  • Are Panamaps (A Multi-Layered Map) useful in finance?
  • Moth on Make Object ID
  • As blogged elsewhere, Photosynth is Released.
  • Kind of cool to see that Linden is rolling out Mono servers
  • Google mojo?

Rotor 2.0

August 20th, 2008 / Tales from a Trading Desk

Joel and Ted have release a draft of Shared Source CLI Essentials.

Intersoft’s Silverlight Controls Demo

August 20th, 2008 / Tales from a Trading Desk

Catching up after vacation

August 19th, 2008 / Tales from a Trading Desk

Returning from vacation is hard - the context switch back to work mode is relatively easy, what I find hard is the early morning starts (5am). There is also the 1000’s of work emails to catch up on coupled with Google Reader items.

  • While I’ve been out the office it appears the Windows 7 blog has come alive. The latest posting on feature teams is worth a read if your into knowing how the Microsoft machine works.
  • HSBC appears to be considering moving from Blackberry to iPhone, not surprising given that everyone appears to want an iPhone these days.
  • David Chappell has written a cloud white paper which is probably worth a ready - although this article is actually more interesting as it references Red Dog and Zurich.
  • With the Olympics in full swing there’s a number of posting around NBC’s Silverlight site: here, here and here.
  • August 2008 MSDN Magazine has a number of interesting articles:
  • Learning Parallel Programming.
  • CUDA U
  • NVIDIA Shows Interactive Ray Tracing on GPUs - real-time ray tracing with adequate performance for fluid motion is the future.
  • Still no sign of a killer financial Microsoft Surface application (
  • .NET Framework 3.5 SP1 got released. Unfortunately the WPF DataGrid didn’t make it into the release, but at least it made it to codeplex. If your moving into WPF, you might find this book interesting.
  • Visual Studio IronPython Integration Deep Dive
  • Glenn Block on Prism, Unity, and MEF
  • Coding the Market thinks Caplin in the answer to SDP. Maybe Mostly should consider Caplin for an OMS?
  • Sounds like Microsoft needs to add a lot more servers to its cloud.
  • New Gigaspaces MonteCarlo application.

Learn Expression

August 19th, 2008

At the Microsoft Expression Community Learning Center:

Just starting out with Expression? We’ve collected tutorials, starter kits, Quick Start Guides, videos, and other helpful resources to get you started quickly. Get answers from fellow users on the Discussion Forums, read our FAQs, or contact Support: it’s all here. For any questions or comments about the training content, please visit the Expression Discussion Forums.

(hat tip: Paul Mooney)

PureMVC

August 13th, 2008

Cross-language implementation of the MVC meta-pattern - PureMVC. Supported languages:

  • ActionScript 2
  • ActionScript 3
  • C#
  • ColdFusion
  • haXe
  • Java
  • Perl
  • PHP
  • Python
  • Ruby

DDJ: One Perspective on Silverlight

August 4th, 2008 / Tales from a Trading Desk

Written sometime ago, but only recently published.

Parallel programming in .NET

August 3rd, 2008 / Tales from a Trading Desk

Oslo Language, Midori and More

August 3rd, 2008 / Tales from a Trading Desk

  • “Oslo” language is one of the many PDC sessions that will be worth watching.
  • Geva has a curious posting on GigaSpaces and the Economics of Cloud Computing
  • NetMap - a set of .NET Framework 2.0 class libraries that can be used to add network graphs to custom applications.
  • F# CTP
  • More details on Midori (”asynchronous-only architecture that is built for task concurrency and parallel use of local and distributed resources, with a distributed component-based and data-driven application model, and dynamic management of power and other resources”). Midori sounds promising assuming this article is true, and assuming (big) that Microsoft can deliver this side of 2015.
  • This article appears to concur with my that Windows compatability is a real issue, and needs to be broken in the future. “astonishingly ambitious undertaking” is an under statements, especially given the history of what Microsoft has been able to release. Lets hope Midori does materialise sometime soon.
  • WCF Visualizers Tool
  • Bad Locking Patterns - I’m still amazed at the number of people that still use lock(this)

Running WPF Application with Multiple UI Threads

July 29th, 2008
Introduction

It’s a good, time-proven practice to perform long, CPU intensive tasks on some sort of a background thread to improve your UI thread responsiveness. Sometimes though UI-related tasks themselves can be quite expensive. WPF, for examples, forces you to do all UI work on the thread that created the UI. A very flexible WPF measure/layout paradigm for UI rendering also comes with high CPU usage cost. In a very UI intensive application (for example, trading app with about ten windows showing real-time montage and blotter data) simply the cost of generating and laying out visuals can become too high for a single thread to keep up. When your UI thread saturates individual windows may start skip rendering cycles, become slow to response to user input, or even freeze. If your UI thread approaches this kind of saturation you should consider creating dedicated UI threads for some (or all) of your UI-intensive windows. This post is a step by step walk-through of doing just that.

Read the rest of the article…

44 Silverlight 2.0 Screencasts

July 17th, 2008 / Tales from a Trading Desk

Mike Faulty does it again.

Cloud Services: Workflow

July 16th, 2008 / Tales from a Trading Desk

Cloud based Workflows - BizTalk Services

IList Performance Decrease

July 16th, 2008 / Tales from a Trading Desk

Interesting read from Kirill.

Off-topic .NET reading:

Slimmer, trimmer messaging from Google

July 15th, 2008 / Joe on Computing

Google’s Protocol Buffers offer lightweight, language-independent object serialization. I love the design, especially as I’m increasingly seeing enterprise networks clogged with hordes of oversized XML messages. Protocol buffer bindings are available for C++, Java, and Python, but not C# yet. Once there is support for .NET, I think this could be a really interesting technology for financial applications.

The Road to Cairo?

July 14th, 2008 / Tales from a Trading Desk

It’s been clear for quite some time that Microsoft would at some point like to offer an OS in managed code (remember WinFX and the managed API PDC statements?). Couple this view with recent “cloud” thoughts, and you can see that one of Microsoft’s future goals is a distributed cloud managed OS. Microsoft Research has been actively rolling down this road with a number of initiatives - Singularity, CHESS and Dryad - have a look at slide 35. Midori may or may not be the product that we see in a few years that provides this managed stack.

One of the problems as I understand it with the Windows OS today is its need to be backwards compatible. One though I had after seeing MinWin was that Microsoft should release MinWin as essentially a VM OS (25Mb), which by default hosts Windows Vista and XP. Singularity/Midori should be the third hosted OS, which would offer the first 100% Microsoft managed OS and move .NET to the next paradigm. Using MinWin avoids the need to pollute Midori, thereby allowing a clean future generation OS to evolve, yet maintaining Microsoft’s backward compatibility for legacy applications.

Midori in summary could possible be Microsoft’s latest attempt to delivery Cairo. However, based on previous attempts to delivery Cairo, at this stage its unclear if we will ever actually see Singularity/Midori outside of the incubator stage. Maybe Microsoft needs to put its research division at the heart of the company to keep innovation alive - we don’t want another WinFS….

C# 4.0

July 11th, 2008 / Tales from a Trading Desk

Channel9 video here.