Have you ever been looking for a snippet of F# code for converting an object/from to JSON? For getting a key from the Windows Registry? For defining Perl-style "?" regular expression matching operators? Or hundreds of other common tasks?

The new F# Snippets site by Tomas Petricek now has hundreds of snippets of F# code prepared and entered by the F# community, and more snippets are being added all the time. You'll be able to find all sorts of things here, and what you can't find, you can enter and tag, for the benefit of all F# programmers!

A huge thank you to Tomas for such a wonderful job on this site. Here's Tomas' original announcement, from December.

When writing F# programs, I often write some nice code snippet or some useful helper function that I'd like to share with the F# community. Unfortunately, my blog posts are usually longer and more elaborate, so I never get to blogging about it. Now that I have a twitter (follow me if you don't already) I thought I could just post the snippet somewhere, but there was no good web site for posting F# snippets - until now!

  • If you can't wait any further, then go directly to the new F# snippets web site at fssnip.net.

Introducing F# Snippets

The web site uses my earlier project F# Web Snippets to format F# source code. It highlights colors and adds JavaScript tooltips using services provided by the F# compiler, which means that you'll get almost the same experience as when reading code in Visual Studio. I also wanted to make the page a repository of browsable snippets, so you can use it in two ways:

  • Add public snippet - If you have any F# snippet that could be generally useful, you can send it as a public snippet. This means that the snippet will be listed on the web site and will be findable using search engines. You'll be asked to add some additional information such as brief description, your name or nickname and a few tags, so that the snippet can be categorized.
  • Private snippets - If you need to share some F# code at Twitter, get a nicely formatted version of code for your blog posts or attach a snippet with proper colorization and tooltips to you answer or question in some forums, you can post it as a private snippet. In this case, it won't be linked from the web site (and you won't have to enter description, your name or tags). You'll still get a nice short URL that you can use to link to your snippet (but you'll have to remember it).

Website features

When inserting an F# snippet, the web site runs a compiler on the server, which is relatively slow (so please be patient when posting snippets). Unlike for example pastebin.com or similar pages for posting snippets, the web site targets only F#, but it provides some unique F# features.

  • Colorization & Tool tips The colorization uses F# tokenizer (meaning that it handles some advanced aspects like nested comments). It also uses F# type-checker to get tool tips with type information about all identifiers and generates nice JavaScript tool tips.
  • Versioning & passcode If you post a snippet, it is possible to update it later. If you want, you can enter a passcode, so that only you can update the snippet (don't use secure password). The history of source code is stored (but changes in comments and tags are not tracked)
  • Listing & tags Public items have author and a list of tags and they are listed by the name and by tags, so you can easily browse snippets. This currently looks like a good way of finding items (together with search engines that will hopefully start indexing the web now that I posted a public link!)

The web site is still evolving and I expect to improve it as it becomes more popular. I'll do changes that will look important based on how the web site is used, so feel free to use it and it will adapt to work well :-). One of the first plans is to automatically export all snippets to some source control, so that they are archived in some independent place...

Summary

Thanks to Robert Pickering, Antonio Cisternino and James Margetson, the web site already contains a few useful F# snippets in addition to those that I submitted during the development. I think that with your help, the web site can become really useful resource of reusable snippets that solve some of the common problems that F# developers often need to solve. If you ever find yourself thinking that some snippet you just wrote is interesting and could be useful or if you write something when explaining F# to your colleagues, then please go on and post it to the web site! I'm sure I'll do that - I started working on the web site when I got irresistible feeling to share this snippet.

Learn F# Programming at fsharp.net