Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Last active November 1, 2019 20:46
Show Gist options
  • Save jacobp100/92fc0ec2006813a2b4e06fef5c0c6ecc to your computer and use it in GitHub Desktop.
Save jacobp100/92fc0ec2006813a2b4e06fef5c0c6ecc to your computer and use it in GitHub Desktop.
type scalar = [ | `Fraction(int, int) | `Decimal(float)];
type value = [ scalar | `Vector(list(scalar)) | `nan];
let addScalar = (a, b) =>
switch (a, b) {
| (`Fraction(n1, d1), `Fraction(n2, d2)) =>
`Fraction((n1 * d2 + n2 * d1, d1 * d2))
| (`Fraction(n, d), `Decimal(f))
| (`Decimal(f), `Fraction(n, d)) =>
`Decimal(f *. float_of_int(n) /. float_of_int(d))
| (`Decimal(f1), `Decimal(f2)) =>
`Decimal(f1 *. f2)
};
let add = (a, b) =>
switch (a, b) {
| (#scalar as a, #scalar as b) => addScalar(a, b)
| (`Vector(a), `Vector(b)) => `Vector(List.map2(addScalar))
| _ => `nan
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment