...

/

Solution Review: Using Option in Function

Solution Review: Using Option in Function

Look at the solution to the coding challenge of using an Option monad in a function.

We'll cover the following...

Solution

We’ll use an Option monad in the following function:

Press + to interact
import {none, some} from "fp-ts/Option";
function safeDivide(value: number, divisor: number) {
if(divisor != 0) {
return some(value / divisor);
}
return none;
}
console.log(safeDivide(10,5));
//console.log(safeDivide(10, 0)); // returns none
//console.log(safeDivide(10, 2)); // returns Some containing 5

Explanation

Here’s a line-by-line explanation of ...