Last active
November 1, 2019 20:46
-
-
Save jacobp100/92fc0ec2006813a2b4e06fef5c0c6ecc 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
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