Last active
August 29, 2015 14:14
-
-
Save lambdahands/603f16297bd171c05002 to your computer and use it in GitHub Desktop.
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
| module ListExtra = struct | |
| open Core.Std | |
| (*- Maps a function to each "column" in a list of "rows". | |
| mapv [[1;2;3]; [3;4;5]] ~f:(List.fold ~init:0 ~f:(+));; | |
| - : int list = [4; 6; 8] | |
| *) | |
| let mapv ls ~f = let open List in | |
| let cmp x y = if length x = length y then 0 | |
| else if length x > length y then 1 | |
| else -1 in | |
| let min = min_elt ls ~cmp:cmp in | |
| let fn' i _ = map ls ~f:(fun x -> nth_exn x i) |> f | |
| in match min with | |
| | Some m' -> mapi m' ~f:fn' | |
| | None -> [] | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment