Last active
August 29, 2015 14:21
-
-
Save refi64/3ca06336634f8fa19d9e to your computer and use it in GitHub Desktop.
Felix entry for "Four MLs (and a Python)"
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
fun add_to(totals: list[float])(values: list[float]) => | |
if values.len != 1.size then let n = totals.len in match n with | |
| 0uz => values | |
| $(values.len) => (add of (float^2)).map $ totals.zip2 values | |
// this ugliness is because of a bug in Felix that generates invalid C++ code | |
// see https://github.com/felix-lang/felix/issues/71 | |
| _ => #(fun () = { raise "Inconsistent-length rows"; return #list[float]; }) | |
endmatch else totals; | |
fun add_line_to(totals: list[float])(line: string) => | |
totals.add_to $ (float of string).map $ split (line, ','); | |
fun sum_file(filename: string) => | |
add_line_to.fold_left #list[float] $ split (filename.load, '\n'); | |
proc sum_and_print_file(filename: string) { | |
res := (str of float).map $ sum_file filename; | |
println $ fold_left (fun (a:string)(b:string) => a+","+b) res.head res.tail; | |
} | |
match #System::args with | |
| Cons (_, Cons (filename, Empty)) => sum_and_print_file filename; | |
| _ => raise "Exactly 1 filename must be given"; | |
endmatch; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment