2 Matching Annotations
- Apr 2024
-
fsharpforfunandprofit.com fsharpforfunandprofit.com
-
let map oneTrackFunction twoTrackInput = match twoTrackInput with | Success s -> Success (oneTrackFunction s) | Failure f -> Failure f And here it is in use with canonicalizeEmail: let usecase = validate1 >=> validate2 >=> validate3 >> map canonicalizeEmail // normal composition Note that normal composition is now used because map canonicalizeEmail is a fully two-track function and can be connected to the output of the validate3 switch directly. In other words, for one-track functions, >=> switch is exactly the same as >> map. Your choice.
QUESTION
map
can be defined usingbind
andswitch
,let map f = bind (switch f)
but how to do this with
>=>
?...ANSWER
Found a solution, but this is quite stupid:
let map' f result = match result with // The value of `o` is irrelevant, it is only needed // because `>=>` returns a function, but we need // a value and both operands of the input `f` are // provided by the closures | Ok o -> ((fun _ -> result) >=> (switch f)) o | Error e -> Error e
NOTE<br /> Call them as:
map ((+) 2) ((Ok 27) : Result<int,string>);; map' ((+) 2) ((Ok 27) : Result<int,string>);;
-
- Feb 2021
-
en.wikipedia.org en.wikipedia.org
-
another to compose functions that output monadic values (called monadic functions)
-