Other interesting ways of using F# at work

Twenty six low-risk ways to use F# at work (part 5)

This post is the conclusion of the series on low-risk and incremental ways to use F# at work.

To wrap up, we'll look at a few more ways in which F# can help you with various development tasks around the edges, without impacting any core or mission critical code.

Series contents

Before moving on to the content of the post, here's the full list of the twenty six ways:

Part 1 - Using F# to explore and develop interactively

1. Use F# to explore the .NET framework interactivelyarrow-up-right 2. Use F# to test your own code interactivelyarrow-up-right 3. Use F# to play with webservices interactivelyarrow-up-right 4. Use F# to play with UI's interactivelyarrow-up-right

Part 2 - Using F# for development and devops scripts

5. Use FAKE for build and CI scriptsarrow-up-right 6. An F# script to check that a website is respondingarrow-up-right 7. An F# script to convert an RSS feed into CSVarrow-up-right 8. An F# script that uses WMI to check the stats of a processarrow-up-right 9. Use F# for configuring and managing the cloudarrow-up-right

Part 3 - Using F# for testing

10. Use F# to write unit tests with readable namesarrow-up-right 11. Use F# to run unit tests programmaticallyarrow-up-right 12. Use F# to learn to write unit tests in other waysarrow-up-right 13. Use FsCheck to write better unit testsarrow-up-right 14. Use FsCheck to create random dummy dataarrow-up-right 15. Use F# to create mocksarrow-up-right 16. Use F# to do automated browser testingarrow-up-right 17. Use F# for Behaviour Driven Developmentarrow-up-right

Part 4. Using F# for database related tasks

18. Use F# to replace LINQpadarrow-up-right 19. Use F# to unit test stored proceduresarrow-up-right 20. Use FsCheck to generate random database recordsarrow-up-right 21. Use F# to do simple ETLarrow-up-right 22. Use F# to generate SQL Agent scriptsarrow-up-right

Part 5: Other interesting ways of using F#

23. Use F# for parsingarrow-up-right 24. Use F# for diagramming and visualizationarrow-up-right 25. Use F# for accessing web-based data storesarrow-up-right 26. Use F# for data science and machine learningarrow-up-right (BONUS) 27: Balance the generation schedule for the UK power station fleetarrow-up-right

Part 5: Other ways of using F# outside the core

This last group of suggestions is a bit of a mish-mash I'm afraid. These are things that didn't fit into earlier posts, mostly concerning using F# for analysis and data processing.

23. Use F# for parsing

It is surprising how often you need to parse something in the course of routine development: splitting strings at spaces, reading a CSV file, doing substitutions in a template, finding HTML links for a web crawler, parsing a query string in a URI, and so on.

F#, being an ML-derived language, is ideal for parsing tasks of all kinds, from simple regexes to full fledged parsers.

Of course, there are many off-the-shelf libraries for common tasks, but sometimes you need to write your own. A good example of this is TickSpec, the BDD framework that we saw earlier.

TickSpec needs to parse the so-called "Gherkin" format of Given/When/Then. Rather than create a dependency on another library, I imagine that it was easier (and more fun) for Philarrow-up-right to write his own parser in a few hundred lines. You can see part of the source code herearrow-up-right.

Another situation where it might be worth writing your own parser is when you have some complex system, such as a rules engine, which has a horrible XML configuration format. Rather than manually editing the configuration, you could create a very simple domain specific language (DSL) that is parsed and then converted to the complex XML.

In his book on DSLsarrow-up-right, Martin Fowler gives an example of this, a DSL that is parsed to create a state machinearrow-up-right. And here is an F# implementationarrow-up-right of that DSL.

For more complicating parsing tasks, I highly recommend using FParsecarrow-up-right, which is perfectly suited for this kind of thing. For example, it has been used for parsing search queries for FogCreekarrow-up-right, CSV filesarrow-up-right, chess notationarrow-up-right, and a custom DSL for load testing scenariosarrow-up-right.

24. Use F# for diagramming and visualization

Once you have parsed or analyzed something, it is always nice if you can display the results visually, rather than as tables full of data.

For example, in a previous post I used F# in conjunction with GraphVizarrow-up-right to create diagrams of dependency relationships. You can see a sample below:

The code to generate the diagram itself was short, only about 60 lines, which you can see herearrow-up-right.

As an alternative to GraphViz, you could also consider using FSGrapharrow-up-right.

For more mathematical or data-centric visualizations, there are a number of good libraries:

And finally, there's the 800 lb gorilla -- Excel.

Using the built-in capabilities of Excel is great, if it is available. And F# scripting plays well with Excel.

You can chart in Excelarrow-up-right, plot functions in Excelarrow-up-right, and for even more power and integration, you have the FCellarrow-up-right and Excel-DNAarrow-up-right projects.

25. Use F# for accessing web-based data stores

There is a lot of public data out on the web, just waiting to pulled down and loved. With the magic of type providers, F# is a good choice for direct integrating these web-scale data stores into your workflow.

Right now, we'll look at two data stores: Freebase and World Bank. More will be available soon -- see the fsharp.org Data Access pagearrow-up-right for the latest information.

Freebase

The code for this section is available on githubarrow-up-right.

Freebasearrow-up-right is a large collaborative knowledge base and online collection of structured data harvested from many sources.

To get started, just link in the type provider DLL as we have seen before.

The site is throttled, so you'll probably need an API key if you're using it a lot (api details herearrow-up-right)

Once the type provider is loaded, you can start asking questions, such as...

"Who are the US presidents?"

Result:

Not bad for just four lines of code!

How about "what awards did Casablanca win?"

The result is:

So that's Freebase. Lots of good information, both useful and frivolous.

More on how to use the Freebase type providerarrow-up-right.

Using Freebase to generate realistic test data

We've seen how FsCheck can be used to generate test data. Well, you can also get the same affect by getting data from Freebase, which makes the data much more realistic.

Kit Easonarrow-up-right showed how to do this in a tweetarrow-up-right, and here's an example based on his code:

The results are:

World Bank

The code for this section is available on githubarrow-up-right.

On the other extreme from Freebase is the World Bank Open Dataarrow-up-right, which has lots of detailed economic and social information from around the world.

The setup is identical to Freebase, but no API key is needed.

With the type provider set up, we can do a serious query, such as:

"How do malnutrition rates compare between low income and high income countries?"

The result is:

Similarly, here is the code to compare maternal mortality rates:

The result is:

More on how to use the World Bank type providerarrow-up-right.

26. Use F# for data science and machine learning

So you're putting all these suggestions into practice. You're parsing your web logs with FParsec, extracting stats from your internal databases with the SQL type provider, and pulling down external data from web services. You've got all this data -- what can you do with it?

Let's finish up by having a quick look at using F# for data science and machine learning.

As we have seen, F# is great for exploratory programming -- it has a REPL with intellisense. But unlike Python and R, your code is type checked, so you know that your code is not going to fail with an exception halfway through a two hour processing job!

If you are familiar with the Pandas library from Python or the ?tseries' package in R, then you should take a serious look at Deedlearrow-up-right, an easy-to-use, high quality package for data and time series manipulation. Deedle is designed to work well for exploratory programming using the REPL, but can be also used in efficient compiled .NET code.

And if you use R a lot, there's an R type providerarrow-up-right(of course). This means you can use R packages as if they were .NET libraries. How awesome is that!

There's lots of other F# friendly packages too. You can find out all about them at fsharp.org.

Series summary

Phew! That was a long list of examples and a lot of code to look at. If you've made it to the end, congratulations!

I hope that this has given you some new insights into the value of F#. It's not just a math-y or financial language -- it's a practical one too. And it can help you with all sorts of things in your development, testing, and data management workflows.

Finally, as I have stressed throughout this series, all these uses are safe, low risk and incremental. What's the worst that can happen?

So go on, persuade your team mates and boss to give F# a try, and let me know how it goes.

Postscript

After I posted this, Simon Cousins tweeted that I missed one -- I can't resist adding it.

@ScottWlaschinarrow-up-right 27: balance the generation schedule for the uk power station fleet. seriously, the alternative to #fsharparrow-up-right was way too risky— Simon Cousins (@simontcousins) April 25, 2014arrow-up-right

You can read more about Simon's real-world of use of F# (for power generation) on his blogarrow-up-right. There are more testimonials to F# at fsharp.orgarrow-up-right.

Last updated