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
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/net45/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]]
338.97 | 5585.5 |
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 | 338.97 | 677.94 | 0.06 |
Bitcoin | 5 | 5585.50 | 27927.50 | 2.32 |
Total | 7 | 12021.85 |