-
-
Save orbitz/05afcda28a33f784d2fe to your computer and use it in GitHub Desktop.
This file contains 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.Std | |
let values_of_line line = | |
let fields = String.split ~on:',' line in | |
List.map ~f:Float.of_string fields | |
let add_to totals values = | |
match totals with | |
| [] -> values | |
| _ -> | |
if List.length totals = List.length values then | |
List.map2_exn ~f:(+.) totals values | |
else failwith "Inconsistent-length rows" | |
let sum_channel chan = | |
let folder tot line = add_to tot (values_of_line line) in | |
In_channel.fold_lines ~f:folder ~init:[] chan | |
let sum_and_print_file filename = | |
In_channel.with_file filename | |
~f:(fun chan -> | |
let result = sum_channel chan in | |
print_string | |
((String.concat ~sep:"," (List.map ~f:Float.to_string result)) ^ "\n")) | |
let main () = | |
match Sys.argv with | |
| [| _; filename |] -> sum_and_print_file filename | |
| _ -> failwith "Exactly 1 filename must be given" | |
let () = main () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to use Core as well and independently got a very similar solution: