The OCaml standard library provides many convertion functions of the form Foo.{to,of}_bar between its various types. These are incomplete – and somewhat arbitrary – so it’s often necessary to go via intermediate types to achieve the convertion you want. (Either that or roll your own with a fold or iter function.)
What’s worse, you may need to mix forwards and backwards conversions (to_ and of_ style respectively), requiring you to search both forwards and backwards in the documentation to get the conversion you need. For example, consider the case of ('k, 'v) Hashtbl.t -> ('k * 'v) list:
Hashtbl
][hashtbl-doc], we see that there’s noHashtbl.fold
. On OCaml 4.07+, we have Hashtbl.to_seq : ('k, 'v) Hashtbl.t -> ('k, 'v) Seq.t
, so let’s start by using that.Seq
][seq-doc], and find no conversions. Once again,Seq.fold_left
to build our own. Or, weList
][list-doc], we find the function List.of_seq : 'a Seq.t -> 'a List.t
. Now we can complete the chain:let hashtbl_to_list h = Hashtbl.to_seq h |> List.of_seq
OCaml
Rather than keeping the entire Stdlib documentation open in browser tabs, it can
be helpful to view all of the available conversions as a directed graph. That’s
what this repository provides. (These graphs show the conversions available on
OCaml 4.11.0; your mileage may vary.)
#data visualization #data analysis