Something Interesting in F#


I’m busy learning how to use SimpleDB. Since I can do the experimentation in F#, I am. Right now, I’m using the .NET SimpleDB client that Amazon has here. I had written the following code, which I only want to run when I discover that my SimpleDB domain does NOT exist:

    let createDomain =

            let createParam = new Model.CreateDomainRequest()

            createParam.DomainName <- domainName

            let response = SimpleDBClient.CreateDomain(createParam)

            ()

A downside to this code is that it gets evaluated EVERY time. Creating a domain, even an existing one, takes several seconds from sending the request to receiving the response. Ideally, creation would only run as needed. What I really want is function type behavior for createDomain. That is, the actual work shouldn’t happen until I need it to. Then, I remembered that functions are first class citizens in F#. I transformed my code to this:

        let createDomain =

            (fun () ->

                let createParam = new Model.CreateDomainRequest()

                createParam.DomainName <- domainName

                let response = SimpleDBClient.CreateDomain(createParam)

                ())

Now, when I want to create a domain, I write createDomain() to get the code to execute. Without that, the call to SimpleDB doesn’t happen.

And yes, I’m aware that many folks who are using F# know this stuff cold. I’m blogging these nuggets now so that I can find them later. That, and it lets all the other newbs not feel so bad about missing the simple stuff;)

  1. Leave a comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: