March 2019
Intermediate to advanced
208 pages
5h 11m
English
Consider this code, which is similar to what we saw in Using Pipe First:
| | let toFloat = (s:string): option(float) => { |
| | switch (float_of_string(s)) { |
| | | result => Some(result) |
| | | exception(Failure("float_of_string")) => None |
| | } |
| | }; |
| | |
| | let reciprocal = fun |
| | | 0.0 => None |
| | | x => Some(1.0 /. x); |
| | |
| | let cube = (x) => x *. x *. x; |
| | |
| | let makeDisplayText = fun |
| | | Some(value) => "The result is " ++ value |
| | | None => "Could not calculate result."; |
| | |
| | let calculation = (input: string): string => { |
| | toFloat(input) |
| » | -> Belt.Option.flatMap(reciprocal) |
| » | -> Belt.Option.map(cube) |
| » | -> |
Read now
Unlock full access