Skip to content

Instantly share code, notes, and snippets.

@nielsbom
Created November 14, 2018 16:07
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/6195651798b8d7ca2fdda9ea8253193f to your computer and use it in GitHub Desktop.
Save nielsbom/6195651798b8d7ca2fdda9ea8253193f to your computer and use it in GitHub Desktop.
Basic mathematical operations in Reason
/*
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
*/
let basicOp = (operation, a, b) => {
switch (operation) {
| '+' => a + b
| '-' => a - b
| '*' => a * b
| '/' => a / b
| _ => raise(Not_found) /* OK? */
}
};
Js.log(basicOp('+', 4, 7) == 11);
Js.log(basicOp('-', 15, 18) == -3);
Js.log(basicOp('*', 5, 5) == 25);
Js.log(basicOp('/', 49, 7) == 7);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment