The #FSharp way: how to write a log

The objective is very simple: I want to give an exact definition – by example – of the functional programming approach in code development.

fsharpway

I’ll use the idiomatic syntax of F# and I’ll assume we want to make a logger for any sort of array.

The principle

We want to abstract out the type of the results when we write a code to collect them. This is the only real world principle of functional programming. If you like the for/foreach/while loop syntax, just use the nice C# language, no worries. But – in that case – don’t talk about idiomatic F# please. Thank you!

The logger – simple version

Ok, so we have a basic understanding of the development guidelines and we can express the main idea of a method, which will help us debugging our code.

let logFile path res =
    use sw =
        new StreamWriter(path, false)
    res
    |> Array.iter (fprintfn sw "%A")

Not bad. Bat we can go further into the abstraction.

The logger – advanced version

Look, there are 3 parts:

  1. we open a context
  2. we iterate a delegate on the array
  3. we close the above said context
let log (env: unit->'ctx) (fn: 'ctx -> 'a -> unit) (a: 'a array) (exit: 'ctx -> unit)  =
    let ctx = env()
    a
    |> Array.iter (fn ctx)
    exit ctx

Cool, now?

let logFile path a =
    log
        (fun () ->
            new StreamWriter(path, false))
        (fun sw i ->
            fprintfn sw "%A" i)
        a
        (fun sw ->
            sw.Close()
            sw.Dispose())

Usage – that simple

anyArray
|> logFile "anyArray.txt"

Yes, awesome indeed.

Advertisements

4 thoughts on “The #FSharp way: how to write a log

  1. As someone coming from c# I must say that although foreach might not be the idiomatic way, the way you’ve written the f# logger looks like garbage. How on earth is that more readable than a simple foreach loop?

    Like

    • Well. Let’s focus on the correct comparison. That is exactly the .iter function. You can think about it as similar to a Linq extension. Just imagine the parallel between the C# select and the F# map. Now here we are returning a unit. Mathematically it’s a singleton. In category theory it’s the terminal object. You also mention the word “garbage”. The only garbage in this post is the garbage collection: the mechanism to avoid leaking references. John A De Goes would say linear type systems. They would support even quantum computation and the no cloning theorem. In the industrial proven dotnet language it is the very simple use/using statement: the real world monadic way to access resources in a context.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s