Fun with Functors
Welcome to fun with functors, my functional programming blog.
Here I write about programming, mainly with a functional-first bias, but may cover other topics along the way.
Posts
Impact analysis of sliding loan-to-value ratios in mortgage amortisation (in OCaml)
2025-01-22
Interest-rate volatility is one of the most important factors anyone considers when taking out or changing their mortgage. One of the main things volatility can influence is the term of the mortgage, which can also vary significantly based on where you live. For example, in areas such as the Netherlands mortgages periods are typically significantly longer than those issued here in the United Kingdom, with 15-20 year terms not being unusual. One rather obvious benefit of this is that purchasers are shielded against rising rates — the inverse being that if rates fall you’re stuck with what you started on. A potentially more subtle downside to this is that you can’t benefit from any changes to your loan-to-value ratio; as you pay your mortgage off your mortgage your LTV decreases which may allow you to apply for better rates. This challenge is further compounded by the fact that you don’t pay a static amount of interest every month due to mortgage amortisation, which we’ll discuss this more below.
Building a back-end web stack in OCaml — Postgres, Caqti V2 & Opium
2023-11-01
There’s a couple of choices when looking to build web-servers in OCaml; 2 commonly used choices seem to be postgres and caqti for the database layer, and dream for running the http-server. Both have half decent documentation and a couple of examples in the git repo’s to work off, however caqti recently released a new major version (>2) and dream relies on an older version (<=1.9.0); some fairly hefty breaking changes in this version precludes our using the two together if we want to use the most recent versions of both. One other option for building a web-layer is Opium; this has a very similar, fairly high-level feel to dream. The official docs for opium reference this excellent blog post however again the version of caqti is pretty old; you can get the gist from here but if you’ll run into issues if you try to lift and shift and use caqti >2 so i’ve put together this post which uses the latest version.
What the applicative? Optparse-applicative from the ground up (Part 2)
2023-09-01
In this post we’re going to be following on from the previous entry covering some of the foundations of how applicatives work with a slightly more concrete example. We’ll be breaking down an example usage of optparse-applicative quite slowly to explore applicatives; if you’re just looking for some examples of usage, I’d refer to the already excellent documentation here.
What the applicative? Optparse-applicative from the ground up (Part 1)
2023-05-15
Functors and monads are both (relatively) well understood topics these days, even if they’re not always labeled as these directly. Functors are everywhere, and many of the basic structures that imperative programmers use on a day-to-day basis form mondad’s and can be composed accordingly. As such, once the penny drops on what a monad actually is, it’s trivial to understand their application and widespread importance. Additionally, many languages have support specifically for this style of programming, making the leap very natural. Applicative functors on the other hand, are not widely used by Johnny-Java-Developer and because of this there’s a temptation to view them as little less intuitive and it’s harder see their applications immediately. In reality applicatives are certainly no more complex than many functional concepts and can be used to facilitate some really nice patterns.
Currency arbitrage on Spark and Scala -> part 1
2021-04-19
With all the interest over the last few years surrounding cryptocurrencies, paired with their naturally open nature, I thought this might be a nice topic for a series on currency arbitrage. Arbitrage, the practice of exploiting differences in prices of the same asset across multiple markets, is an area of finance particularly well suited to a computational approach. The theory is simple enough; we take an asset, say pork bellies, and find someone selling at one price (£1) and someone buying at another (£2) and simply buy from the first to immediately sell to the second, profiting ourselves £1. Applying this to currencies, the natural extension of this is to start applying this idea across multiple pairs; say we have 3 currencies GBP, USD & JPY (with quotes GPB/USD = 1.5, USD/JPY = 1, JPY/GBP = 1.5) if we trade these simultaneously (or very quickly) GBP->USD->JPY->GBP, we’d end up with more GBP than we started with, risk-free.
Quickstart with Zeppelin, Scala, Spark & Docker
2021-02-23
Recently, I’ve been working on a problem that required doing some exploratory analysis on some of our more high throughput datasources. Where we’ve had to do this kind of work in the past the approach has typically been to reach for the usual suspects; python and pandas, probably running in jupyter notebooks. Whilst I’ve written quite a bit of python over the years and will occasionally reach for it to whip up a script for this or that, I find the lack of type safety for anything more involved than bashing together a script to automate something simple really frustrating, so I decided to try out Apache Spark using Scala. After a quick trial of running some code in both Apache Zeppelin and Jupyter using the spylon-kernal, Zeppelin quickly emerged victorious (in my mind) and so far has been an absolute joy to work with.