Hacker News new | ask | show | jobs
by srean 4456 days ago
Since there has been a resurgence of interest in OCaml on HN, some may be curious to see how a production quality Btree implementation looks like in OCaml. Algebraic data types, make it particularly convenient for these sorts of things. For comparison one may contrast it with an implementation in a language that does not have algebraic types, C++ for example. So glad Rust chose to have them.

Here is a Btree implementation from the mirage project [1] https://github.com/cgreenhalgh/ocaml-btree/blob/master/lib/b...

EDIT: removed link to binary tree after kerneis pointed it out.

[1] http://www.openmirage.org/

3 comments

> It can be shorter still, if all you want is insert and read

Despite the name, I'm afraid this is a simple binary tree implementation, not a BTree:

type 'a tree = Empty | Node of('a * 'a tree * 'a tree);;

Obviously the author didn't know what he was doing (the intended scope was very large, AVL, BTrees, etc. but he stopped after committing this single file).

> Here is a Btree implementation from the mirage project

And in fact, this implementation does not show a lot because the actual B-tree work is done by the baardskeerder library: https://github.com/Incubaid/baardskeerder

It looks very large and complex, it is most certainly possible to write a simpler, pedagogical implementation.

Found cpc https://github.com/kerneis/cpc from your comment stream. Looks super interesting. http://felix-lang.org/share/src/web/tut/fibres_index.fdoc might pique your interest. It also compiles coroutines portably and efficiently (to a C++ runtime library). Felix tries to do for C++ what F# does to C# (in case there is any confusion, I am just a new user)
Sweet. I remember writing an implementation in TurboPascal in the nineties. This is... refreshing :-)