Skip to content

Instantly share code, notes, and snippets.

@AeroNotix
Created April 26, 2014 14:58
Show Gist options
  • Save AeroNotix/d609fcc8ad99f9e65f90 to your computer and use it in GitHub Desktop.
Save AeroNotix/d609fcc8ad99f9e65f90 to your computer and use it in GitHub Desktop.
open Core.Std
let maybe_list this that =
match this with
| Some l ->
Some (List.append l [that]);
| None -> Some [that]
let group_by f l =
let ht = Hashtbl.Poly.create () in
List.fold
~f:(fun m el ->
let res = f el and
cur = Hashtbl.Poly.find ht el in
Hashtbl.Poly.replace ht cur (maybe_list cur el);
ht)
~init:ht l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment