Other interesting ways of using F# at work
Twenty six low-risk ways to use F# at work (part 5)
Last updated
Was this helpful?
Twenty six low-risk ways to use F# at work (part 5)
Last updated
Was this helpful?
This post is the conclusion of the series on .
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.
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
Part 2 - Using F# for development and devops scripts
Part 3 - Using F# for testing
Part 4. Using F# for database related tasks
Part 5: Other interesting ways of using F#
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.
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.
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.
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 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.
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.
To get started, just link in the type provider DLL as we have seen before.
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.
The results are:
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:
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!
There's lots of other F# friendly packages too. You can find out all about them at fsharp.org.
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.
After I posted this, Simon Cousins tweeted that I missed one -- I can't resist adding it.
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 .
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 to write his own parser in a few hundred lines. You can see part of the .
In , Martin Fowler gives an example of this, . And here is an of that DSL.
For more complicating parsing tasks, I highly recommend using , which is perfectly suited for this kind of thing. For example, it has been used for parsing , , , and a .
For example, in a I used F# in conjunction with 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 .
As an alternative to GraphViz, you could also consider using .
for desktop visualizations that is well integrated with F# scripting.
for interactive visualizations in HTML.
, an F# library for working with
You can , , and for even more power and integration, you have the and projects.
Right now, we'll look at two data stores: Freebase and World Bank. More will be available soon -- see the for the latest information.
The code for this section is .
is a large collaborative knowledge base and online collection of structured data harvested from many sources.
The site is throttled, so you'll probably need an API key if you're using it a lot ()
.
We've seen how FsCheck can be used to . Well, you can also get the same affect by getting data from Freebase, which makes the data much more realistic.
showed how to do this in a , and here's an example based on his code:
The code for this section is .
On the other extreme from Freebase is the , which has lots of detailed economic and social information from around the world.
.
If you are familiar with the Pandas library from Python or the ?tseries' package in R, then you should take a serious look at , 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 (of course). This means you can use R packages as if they were .NET libraries. How awesome is that!
27: balance the generation schedule for the uk power station fleet. seriously, the alternative to was way too risky— Simon Cousins (@simontcousins)
You can read more about Simon's real-world of use of F# (for power generation) on . There are more testimonials to F# at .