Created
November 20, 2015 17:40
-
-
Save bahmanm/4121bf991736fd4380bb to your computer and use it in GitHub Desktop.
OCaml `List.group_by`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let group_by (f : 'a -> 'b) (ll : 'a list) : ('b, 'a list) Hashtbl.t = | |
List.fold_left | |
(fun acc e -> | |
let grp = f e in | |
let grp_mems = try Hashtbl.find acc grp with Not_found -> [] in | |
Hashtbl.replace acc grp (e :: grp_mems); | |
acc) | |
(Hashtbl.create 100) | |
ll;; |
martinklepsch
commented
Nov 24, 2017
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment