Literate programming in F-Sharp using org-mode

Org Babel has a wide selection of supported programming languages. But F# was still missing even though it’s well suited for Org spreadsheets by the use of strongly typed access to connected information via type providers. So I started an implementation of Org Babel F# support.

In the following use case I adapted an Emacs Lisp implementation for querying the Kraken cryptocurrency exchange. I also used Org Babel Shell to install and use Paket: a dependency manager for .NET.

The source file for this document can be found here: kraken-fsharp.org

Download Paket

Use curl to download the Paket cli tool:

curl -L  -z paket.exe -o paket.exe \
     https://github.com/fsprojects/Paket/releases/download/5.0.0/paket.exe

Install FSharp.Data

Reference FSharp.Data in the paket.dependency file:

source https://nuget.org/api/v2
nuget FSharp.Data

Install the NuGet dependencies into the packages folder using:

mono paket.exe install

Access The Kraken Ticker Restservice

Instead of manually parsing the Kraken JSON API we let the compiler do the job by using the JSON Type Provider. This Org Babel source block named ticker will evaluate to a result block also named ticker wich can be referenced from within an Org Table formula.

#r @"packages/FSharp.Data/lib/net40/FSharp.Data.dll"
open FSharp.Data

type Kraken = JsonProvider<"https://api.kraken.com/0/public/Ticker?pair=ETHEUR,XBTEUR">
let ticker = Kraken.GetSample()
[float <| ticker.Result.Xethzeur.C.[0]; float <| ticker.Result.Xxbtzeur.C.[0]]
317.0 2270.664

Access the ticker result within an Org Table

The result can now be referenced in the following Org Table using the formula @2$3 = remote(kraken, @1$1);%.2f for Ethereum and @3$3 = remote(kraken, @1$2);%.2f for Bitcoin.

Coin Units Unit Price € Current Value € Fraction
Ethereum 2 317.00 634.00 0.05
Bitcoin 5 2277.57 11387.85 0.95
Total 7 12021.85
comments powered by Disqus