Showing posts with label F#. Show all posts
Showing posts with label F#. Show all posts

Wednesday, January 29, 2014

In Search of Maybe in C#

One of the things working in Scala has taught me about C# is that the maybe monad is nicer than null.

In case you're not sure what the maybe monad is: It's a generic type that either contains a value or not. In Scala as well as in F# this type is called Option and can be either Some - in which case it contains a value - or None - in which case it doesn't. This allows you to represent the absence of a value in an explicit way visible to the compiler, since Option<T> is a separate type from T. This turns out to be a whole lot stronger than relying on null to represent the absence of a value. I'll point you to an article about F#'s Option for more explanation on why.

Functional languages will usually let you pattern match over Option, making it very easy to clearly differentiate the case of Some from the case of None. In F# this look like so:


In C# we don't have this. The closest thing is Nullable<T>, but that doesn't work over reference types. But is Nullable<T> really the closest thing in C# to Option? - NO. As was pointed out to me in a recent email exchange the F# Option type is available in C# too.

Let's see if we can redo the above F# code in C#. First the declaration the name variable:


The F# Option type is part of the FSharp.Core assembly and the is actually called FSharpOption. Hence the line above.
On to the nameOrApology variable:


Not nearly as clear as the F# counterpart. What's in the if part; what's in the else part. Not nearly as clear to me as the pattern matching in F#.
We can do better, using this little helper extension function:


Which let's us initialize nameOrApology like this:


Now the two outcomes - a value inside the Option or no value - are much more clear to me.

This is OK. Although the name, FSharpOption, is not too nice. Some of the method names - e.g. get_IsSome - aren't nice either. More importantly: Another thing Scala taught me is that it's nice if Option can be used in place of a collection. But that's for another post.

Sunday, September 8, 2013

Playtime: Riak, Azure, F#

Sifting through the programme for this years GOTO conference one of the talks I thought I'd go see was the one on Riak in the big data track. This is not a new topic at GOTO. In fact I've seen several really good Riaks talks there (like the Riak on mobiles one, that has also been at GOTO). Each time I've gone out of the talk thinking "I've gotta play around with Riak! It's cool." This time around I thought I'd start the playing around beforehand, with the added benefit that I have a bit more context coming into this years Riak talk.

What follows is a short account of my Hello World level experiences of getting Riak up and running on Azure and talking to it from a bit of F# sharp code.

Riak on Azure
As someone who works mostly on Windows the first thing I ran into with Riak is that there is no Windows option on the downloads page - it's different Unix flavors all the way through. Not worry though, in the cloud age spinning up a new Unix box is easy. As it turns out getting Riak on there is pretty easy too (just a few curl commands and that's it): I just followed the step-by-step guide to running Riak on Azure. The guide is little bit out of date with regards to the screenshots from Azure, but nothing that will trip you. The stumbling block was the very last curl command in the guide which is

curl -s http://rekon.basho.com | sh

but http://rekon.basho.com just gives you a redirect to a page on github. It's not a hard stumbling block though; you just follow the redirect and put in this command instead:

curl -s https://raw.github.com/basho/rekon/master/download.sh | sh

At this point I had a VM in Azure running Riak. I was able to put data into it and read it again through Riaks HTTP API simply using more curl commands.

Talking to Riak with F#
The basics of Riaks HTTP API are pretty easy - HTTP PUT to create a new key-value pair in a bucket, HTTP PUT to modify existing values, and HTTP GET to read them - but even doing that with curl gets a bit cumbersome pretty quickly. So here is how to talk to Riak with F# code using xUnit as an execution container.

First, checking the Riak cluster is even reachable can be done by pinging it like this:


Moving on to adding a key-value pair and implicitly creating a bucket too looks like this:


Finally reading data out again looks like this:


And that's it for getting data in and out of Riak from F# code.

Wednesday, June 26, 2013

A Belated NDC Roundup

NDC is well over. It was lots fun. Great chit-chatting with folks and some good talks as well. So what stuck? Which talks do I actually remember here couple of week later? For me it was mainly these 3:

  • Sebastian Lamblas talk on caching in HTTP. The guy knows his stuff and he can explain it too. On the surface of things caching in HTTP may sound like small topic, but it turns out there is more to it than meets the eye. Useful stuff for anybody building web apps (be it internal facing or public facing)  with any significant load.
  • Don Symes talk on F# type providers. Type providers are a cool concept. If you haven't checked them out you should, and this talk is a good place to start, although you probably need to know basic F# to get the most out of it.
  • Jonas Winje, Einar W. Høst and Bjørn Einar Bjartnes talk on drones and Rx. This was a really fun talk packed with breakneck demos stacking Node.js, Rx, Roslyn, Lisp, Nancy, a drone and probably a few other things I've forgotten on top of each other. Mixed in with all the fun and games where some good sound technical content especially around reactive programming.
There were other good talks, but these are the ones that mostly stuck in my mind. I'd recommend watching them when the videos come out.

Monday, March 18, 2013

Hello Nancy World in F#

As you may noticed (if you follow this blog or go to the same conferences as me) I really like the Nancy web framework. So naturally, since I've decided to finally learn F# properly, I threw together a hello world Nancy app in F#. It's very simple; the complete code is listed below. All I did was create an F# console application, install the Nancy and Nancy.Hosting.Self NuGet packages and type in this code, which will return the string "Hello" if you point your browser to "http://localhost:8888/nancy/":



So what's going in that code? Fist thing to notice is the type HelloModule which inherits NancyModule. On start up Nancy will pick up all NancyModules and run their constructor. In it's constructor HelloModule sets up a single route, "/", and tells Nancy what to do on a HTTP GET to that route, namely to execute a lambda that always returns "Hello". Lastly, since this is self hosted, there is a bit of setup in the "main" function: A Nancy host is instantiated, told where to listen for requests and started. This will make the Nancy self host start up Nancy and start listening for requests. That's it. Short and sweet.

Thursday, May 10, 2012

Slides from my Community Day Copenhagen 2012 Talk

I did a talk on "alternative" .NET web frameworks, because I think it's important to realize that there are serious alternatives to ASP.NET - even in .NET-land. Anyway here are the slides:


Thursday, February 3, 2011

Random Thoughts on Tiobe Index January 2011


The Tiobe index is an index of the popularity of programming languages (formally defined here).
According to the Tiobe web site "the index can be used to check whether your programming skills are still up to date or to make a strategic decision about what programming language should be adopted when starting to build a new software system."

Some Data to Notice From the Index

As someone who has worked mainly in .NET through the last several years, my interest in the index focuses on .NET languages (and on my favorite language for recreational coding for some time now: Scala). The rankings of .NET languages in the January 2011 index are:
  • F# ranked between 50 and 100 (the index put these 50 in one big pile because differences the in scores are minuscule)
  • VB.NET at no. 49
  • PowerShell at no. 28
  • C# at no. 6

For perspective here are some selected other languages:

  • BETA ranked between 50 and 100
  • AWK at no. 31
  • T-SQL at no. 17
  • Ruby at no. 10
  • VB at no. 7
  • Python at no. 5

And also worth noticing is that Java, C and C++ take the top 3 spots, and have for a decade.

What I Get From That Data

Now what does this mean for a .NET developer?

  1. C# is hugely popular, but still much, much less popular than Java. Looking at the long term trend C# is steadlily gaining ground. So having and maintaining C# skills is a really good idea in terms of making a living.
  2. VB.NET is so unpopular it's almost off the charts...in a bad way. But (!) considering how the Tiobe index is done I don't think it distinguishes VB and VB.NET properly. So IMHO it's more fair to add the VB and VB.NET scores which would place VB.NET at 5th place - where VB is now. If this is true having VB.NET skills is a really good idea too, but the long term VB.NET trend is downwards, especially over the last couple of years. So maybe it's time to translate those VB.NET chops to C# chops?
  3. F# doesn't seem to be going anywhere. So don't learn F# for job security. Learn it to open your mind. But then again, if you want to open up your mind, and you're a .NET dev, why not look outside .NET and do Scala or Clojure or Erlang or something completly left field?
  4. PowerShell seems to be becoming important. Maybe it's time to really learn it.
 What does it mean for the .NET community?

  1. Assuming the VB and VB.NET points needs to be added C# and VB.NET combined would weigh in at a 3rd place, meaning that .NET is doing very, very well in terms of popularity. 
  2. Both Ruby and Python are doing very well. So if the Iron* languages are dropped an opportunity for engaging two very large communities is lost. That would be really sad!

Those were my 2 cents. Go check the index out, and make up your own mind. And let me know what you think :-)



Oh, and Scala is at place 50, just within the list proper. Still rooting for it :-)