Most recent Thoughts and Writings

Getting started with Bucklescript

Wed, 6 Dec 2017 11:07:00 GMT

BuckleScript is a relatively new option for writing Javascript that's faster, safer and smaller.

It allows you to code in OCaml – an easy-to-learn statically typed programming language – for both the backend and frontend portion of your website. Coding nirvana? Maybe.

For the backend, well, you'll just write OCaml and run it natively. The frontend is a two-step process.

  1. Run the BuckleScript compiler
  2. Roll up the output using rollup.js

Dave Granlund, Wednesday May 27, 2016 Facebook will try to sell you the complete React package, complete with their own OCaml syntax called Reason. You don't need to drink the kool-aid.

There is nothing particularly wrong with OCaml's syntax as-is.

My impression is that good code looks good in OCaml's syntax, but Reason is more forgiving.

Now that we don't have React or anything like that, how do we interact with the DOM?

There's a much more simple alternative which is to just use a function-based virtual dom library. You can use snabbdom because someone made super-convenient bindings: bs-snabbdom.

Here is what it looks like:

open Snabbdom.Base

(* Define a function that returns a new virtual dom node *)
let view title =
  h "div" [
    style "box-shadow" "0px 0px 10px black";
    children [
      h "h1" [text ("Hello, " ^ title ^ "!")]

(* Create a patch function from an array of Snabbdom modules *)
let patch = init [|module_style|]

(* Patch a dom element with id "#app" to the new virtual dom node *)
let () = patch (Snabbdom.VNode.from_dom_id "app") (view "Snabbdom")

That's adapted from the example on the project page. You'll probably want to enable all features:

let patch = init [|

You realistically need two more things to really get anywhere:

That's a total of 3 packages and you're all set. What's fun about it is that it's back to basics – actual coding. You're not just trying to feverishly navigate through other people's code to piece together what you need; you're writing your own code and make your own application design.

Light and nimble

With snabbdom, the gzipped, minified output for a complete, non-trivial client-side is typically in the area of 10 - 15 KB. BuckleScript does its own tree-shaking to make sure you only ship what's actually needed.

Of course, you can do this in plain old vanilla JavaScript, too. The point here is that it's painless to get the same result using BuckleScript and the warm fuzzy feeling of using a statically typed language. Arguably, TypeScript lets you write statically-typed code too. But it's a superset of JavaScript and as such, it's still a dynamic language.

I'll try to put together an example repository for my next post on this subject. In particular, there's a lot to be said on backend development with OCaml – the learning curve is a bit more steep.


A Letter for Laura

Sat, 10 Dec 2016 08:30:00 GMT

Laura was my recruiter. She'd found me from a pile of discarded resumes and thought perhaps it was a mistake. I think she is no longer entirely of that persuasion – I didn't get the job – but I do think she knows that tech hiring is broken and not just for old farts.

Because it's still true that the tech interview is in most respects a reverse turing-test: prove that you're not human.

Dilbert, Wednesday August 27, 2003

It's well-known that you've got to concentrate to do good work. You're unlikely to get to that state in an interview. The assumption is that it doesn't matter because it's the same for everyone. That we can in fact use your bad performance vis-a-vis your peers as a heuristic for future performance.

If she says she can do it
Then she can do it,
she don't make false claims

– David Bowie

I think I'm a false negative because I can do it. My past performance shows it and certainly, anyone can visit my open source profile on Github and see for themselves.

I realize that most developers just aren't very good and do need to be weeded out and that not everyone will have a portfolio of past work. And perhaps we're only ever as good as our last two year's work. I'll argue that if you can give good answers to the following questions then you're ahead of most other candidates.

  1. What is dynamic vs static typing?
  2. What does type safe mean?
  3. How do you think objects and relational databases go together?

Of course, I'm biased. I think the answer to the last question is "not at all". But I'll accept your answer if it's good. I want to work with people who care about software – philosophy included. It's ultimately about getting things done, but it's equally important to sometimes get out of the weeds, take a step back and talk about what we think of this field.

After all, the first step in gettings things done is not having so much to do.

The Sign Up With Google Mistake You Can't Fix

Tue, 1 Mar 2016 18:30:00 GMTUpdated

This blog post has been revised to more accurately reflect some details. Thanks to Fleep CEO Henn Ruukel for reaching out.

Today – by regrettable oversight – I exported my recent e-mail history to Fleep, a collaboration platform used by a new client, and let their software synchronize future e-mails. I thought I was just signing up, giving up my basic info.

The mistake was all mine. I did not notice that the authentication screen was requesting for permission to allow Fleep to "manage my e-mail". But I did realize the consequences moments later when I saw that many of my contacts and my recent e-mail history had been pulled into their system.

It turns out that Fleep actually only pulls down the most recent 200 e-mails. But this wasn't apparent at all to me because I wasn't expected any of my e-mail to be imported. I had unwittingly allowed an app to download my entire personal correspondence for about a decade.

While I do blame Fleep for using what I perceive to be a euphemism – connect with gmail – and not making it crystal clear that you're not just signing up for a messenger app but actually fully integrating your e-mail account, the bigger problem here lies with how Google makes this possible:

  1. At all
  2. Without asking for your password

It's just too easy to give away your personal information on the internet and this needs to be fixed.

We have a similar problem with apps on mobile devices that ask for permission to access all photos in order for you to be able to select just one. I think for the most part you can trust apps to do the right thing – but the way it's currently set up, there is no transparency in what these companies do with the data you have granted them access to.

Legally, I think the EU Data Protection Directive has me covered, but once you have handed over your data to an internet company, it's really out of your control.

Big internet companies, please take privacy seriously and help your users understand the consequences of their actions.

A Skip Dict for CPython

Fri, 26 Sep 2014 11:00:00 GMT

I have released an implementation of a skip dict datastructure for CPython. It's written in C and works on both Python 2.7+ and Python 3.3+.

It uses a skip list combined with a mapping from keys to values, a design inspired by the sorted sets datastructure found in Redis.

Python's Missing String Type

Thu, 17 Apr 2014 17:35:00 GMT

When Python 3.0 came out in late 2008, it was expected that the eventual wide adoption of the 3.x series would take roughly five years.

And on some Linux systems today, it's even the default interpreter.

$ python
Python 3.5.2 (default, Jun 28 2016, 08:46:01)

Yet, I don't know anyone who actually uses Python 3 for application development. I think there are two primary reasons for this:

  1. The advantages are few.
  2. The disadvantages are many.

The most controversial change in Python 3 was that the string type was changed from an 8-bit raw byte string to a unicode-based string type which makes sense because the string type is for human-readable text and unicode is able to represent any text.

Unfortunately, it broke almost every existing library. But it also missed the mark.

In Python 2 we have str and unicode. In Python 3 we have str and bytes. But there's a design that allows us to combine the functionality of both in a single type.


We can use a rope-like data structure where each leaf is a sequence of bytes with an encoding such as utf-8 (see also the paper from 1994 by Boehm, Atkinson and Plass.)

Rope data-structure

We can add any two str instances together, regardless of encoding, and use all of the common string methods and operators such as len and split. In all cases, the methods would respect the encodings of the various segments.

To "flatten" a rope, we encode it:

>>> string.encode('utf-8')

This is typically necessary only for I/O or use with external libraries.

What about raw bytes? Easy:

>>> data = open('foo.png', 'rb').read()

And if we know that a particular substring is actually encoded:

>>> header = data[1:4].decode('utf-8')

This works because data was read as raw bytes from a file. When we decode this data we get a rope that's composed of a single segment with a unicode-compatible encoding – utf-8.

Discussion on Hacker News.