Skip to content

Commit

Permalink
Harden variance string parser with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cedmans committed Nov 15, 2019
1 parent eb9d8ac commit 83437ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions 2018/01-frequency-changes/src/Frequency.re
@@ -1,4 +1,6 @@
[@bs.val] external __dirname: string = "__dirname";
exception InvalidVarianceString(string);
exception InvalidSign;

let inputFilename = "input.txt";

Expand All @@ -13,14 +15,13 @@ let parseVariance = (varianceString: string) => {
let r = [%re "/([+-])(\\d+)/"];
let result = Js.String.match(r, varianceString);
switch (result) {
| None => 0
| Some([|_entire, sign, value|]) =>
switch (sign) {
| "+" => int_of_string(value)
| "-" => int_of_string(value) * (-1)
| _ => 0
| _ => raise(InvalidSign)
}
| _ => 0
| _ => raise(InvalidVarianceString(varianceString))
};
};
let parseVariances = (varianceStrings: array(string)) =>
Expand Down
2 changes: 1 addition & 1 deletion 2018/01-frequency-changes/src/input.txt
Expand Up @@ -957,4 +957,4 @@
+17
-15
+19
-72362
-72362

0 comments on commit 83437ea

Please sign in to comment.