Pages

Wednesday, September 7, 2011

F# pipeline and composition performance

Seems that F# pipeline and function composition is very similar. I experiment the performance of these two statements:

    let f0 x = x + 1
    let f1 x = x * 3

    let f2 = f0 >> f1
    let f3 x = x |> f0 |> f1

    let sw = Stopwatch()

    sw.Reset()
    sw.Start()
    [1..10000] |> Seq.iter (fun n -> (f2 n) |> ignore )
    sw.Stop()
    printfn "%d" (sw.ElapsedMilliseconds)

    sw.Reset()
    sw.Start()
    [1..10000] |> Seq.iter (fun n -> (f3 n) |> ignore )
    sw.Stop()
    printfn "%d" (sw.ElapsedMilliseconds)

    Console.ReadKey() |> ignore

The result is, in release mode, the pipeline is almost 3 times faster than composition.

I run the code from Debug mode and release mode. And invoke the .EXE from file system.  Please feel free to perform the same tests and give me feedback.

As suggested by Arseny Kapoulkine, I tried to use the Seq.map and then Seq.Length to force evaluation. Unfortunately, the result is similiar:


open System.Diagnostics

let f0 x = x + 1
let f1 x = x * 3

let f2 = f0 >> f1
let f3 x = x |> f0 |> f1

let sw = Stopwatch()

sw.Reset()
sw.Start()
[1..10000] |> Seq.map (fun n -> (f2 n) |> ignore ) |> Seq.length |> ignore
sw.Stop()
printfn "f2 = %d" (sw.ElapsedMilliseconds)

sw.Reset()
sw.Start()
[1..10000] |> Seq.map (fun n -> (f3 n) |> ignore ) |> Seq.length |> ignore
sw.Stop()
printfn "f3 = %d" (sw.ElapsedMilliseconds)  

System.Console.ReadKey() |> ignore


Really appreciate the feedback from Arseny!

the code he present is at  http://fssnip.net/8e. The result shows the difference between composite and pipeline is very small.


Now seems that composite and pipeline are same.

Tuesday, September 6, 2011

Deploy F# application - Very simple

some people wondering how to deploy a F# application. The solution is very simple: xcopy everything under your release folder.

For Windows Phone 7 app, you might also need to include the resource folder.

In one word, xcopy everything under your release folder.

Another source is: http://msdn.microsoft.com/en-us/library/ee241127.aspx

WPF/SilverLight F# Converter

Since 2006 I started to develop WPF application, I experienced the pain of writing XAML, binding, converter, coerce, and the MVVM pattern. I find the F# is the best tool to write converters. I can still recall somebody was trying to write some framework to chain some multiple converters. I published a snippet on http://fssnip.net/7Q.

First of all, let me justify converter is still useful when MVVM pattern is available. First application of converter is for graphic designers. The converter provide graphic designer some basic mathematically tool to design a control. The MVVM pattern is too much for a graphic designer. Secondly, the MVVM itself can use converter as well. The last point is how big is your project. I would not say MVVM pattern is suitable for a small demo project. Use the right tool at the right time to do the right job is the key.

The converter snippet design idea is simple. It groups the functions into a module and function composition and pipeline can chain different function and form a new function. The converter basically is a function, you can use strategy pattern (http://fssnip.net/7k) as the starting point to design a converter.

The hard part is how to make your function look like (object, type, object, cultureInfo) format. The following function is the function to convert your function to (object, type, object,cultureInfo) signature.


let convert<'T> f (obj:System.Object) (t:Type) (para:System.Object) (culture:Globalization.CultureInfo)  = (obj :?> 'T) |> f |> box
The concrete implementation is to chain up different function to form a string to visibility as convert function and nullFunction as convertBack function. The converter is trying to reduce the coding effort and make sure the generated code can work with Blend. I added another converter which I used a lot in my data binding debugging. The converter itself does not do anything, but can be used to check if the binding actually work or not.

Wednesday, August 31, 2011

Create generic type using F#

you can use typeof operator to get a concrete type, just like C#'s typeof operator. But when thing comes to the generic type, it becomes different.

in C#, you can do: typeof(List<>), but in F#, if you do typeof> you will get List type. The solution is simple: you have to use typedefof>,this will give a generic type definition and you can use GenerateGenericType to create the concrete type. In summary: typeof: for get concrete type typedefof: get the generic type definition.

Sunday, August 21, 2011

F# quotation and expression tree

If have a function like "sample", how to write out the expression with F# quotation? I have been struggle this for whole night.


[ < ReflectedDefinition > ]
let sample (x) =
    printfn "###%A" x

It seems that we can only get the function parameters, and the methodInfo stops the expression traversing. Here is a way to solve this problem:

match expression with

| Patterns.Call (expOption, mi, expList)  ->
        matchExp expOption
        match mi with
            | DerivedPatterns.MethodWithReflectedDefinition n -> recursive call to access n
            | _ -> print mi

Saturday, August 20, 2011

F# format for Other .NET invoke

Sometimes I need to define a library let other .NET library and recognize or invoke. It must be like C# format:

namespace AAA
{
     class BB
     {
           some functions here.
     }
}

It took me one day to figure the equivalent format:

namespace AA

type BB () =
     some functions

module Program
     let b = BB()
   


F# and Design patterns

When you bet your career to a technology on F# and following Microsoft for so many years, I really do not want F# to stay like a niche language. This is the first post for several design patterns on the f# snippets web site. Hopefully more people will apply F# into more commercial software system.


I will put some description and how I think about using these pattern and F# design a software system in future post.