Skip to content

Instantly share code, notes, and snippets.

@nielsbom
Created November 15, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielsbom/89e370be3acd9330ebabfa3c0fcaa72f to your computer and use it in GitHub Desktop.
Save nielsbom/89e370be3acd9330ebabfa3c0fcaa72f to your computer and use it in GitHub Desktop.
Use variants
/*
https://www.codewars.com/kata/basic-mathematical-operations
Your task is to create a function that does four basic mathematical operations.
The function should take three arguments - operation(string/char), value1(number), value2(number).
The function should return result of numbers after applying the chosen operation.
basicOp('+', 4, 7) // Output: 11
basicOp('-', 15, 18) // Output: -3
basicOp('*', 5, 5) // Output: 25
basicOp('/', 49, 7) // Output: 7
*/
type operator = Plus | Minus | Multiply | Divide;
let basicOp3 = (operation, a, b) => {
switch (operation) {
| Plus => a + b
| Minus => a - b
| Multiply => a * b
| Divide => a / b
}
};
Js.log(basicOp3(Plus, 4, 7) == 11);
Js.log(basicOp3(Minus, 15, 18) == -3);
Js.log(basicOp3(Multiply, 5, 5) == 25);
Js.log(basicOp3(Divide, 49, 7) == 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment