I love using pipelines of combinators to munge data but OCaml’s stdlib is missing some obvious ones such as
count_by. You can write them easily enough in vanilla OCaml using Hashtbl, e.g.
let distinct_by f xs = let seen = Hashtbl.create 16 in let result = ExtArray.empty() in xs |> Array.iter (fun x -> let fx = f x in if Hashtbl.mem seen fx then () else ( Hashtbl.add seen fx (); ExtArray.append result x )); ExtArray.to_array result
but that relies upon polymorphic equality and hashing. If I want to write these kinds of functions using functors I guess I’d use first-class modules to pass a concrete Hashtbl implementation (generated by Hashtbl.Make) into that function. What exactly would that look like?
Furthermore, can I factor out any commonality between equivalent functions for different ordered collections such as
Queue and so on using functors?