Created
February 19, 2016 10:11
-
-
Save keleshev/d273fd721c3d088dfb10 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
open Core_kernel.Std | |
let (>>) f g x = g (f x) | |
module Row = struct | |
let parse_exn = String.split ~on:',' >> List.map ~f:Float.of_string | |
let to_string = List.map ~f:Float.to_string >> String.concat ~sep:"," | |
let print = to_string >> print_endline | |
end | |
let folder sum line = | |
let row = Row.parse_exn line in | |
match sum with | |
| [] -> row | |
| _ when List.length sum = List.length row -> List.map2_exn ~f:(+.) sum row | |
| _ -> failwith "Inconsistent-length rows" | |
let () = match Sys.argv with | |
| [|_; filename|] -> | |
In_channel.(with_file filename ~f:(fold_lines ~init:[] ~f:folder)) | |
|> Row.print | |
| _ -> failwith "Exactly 1 filename must be given" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can inline
Row
functions to shorten it to 16 lines, but I find it less readable: