Standard library `map` function for Either and Optional
- Arne_GebertOP
Is there a function in the standard library that allows me to
mapoverOptionals orEithers?I didn’t find it in the documentation and the following snippet also doesn’t compile:
testEMap : Script () testEMap = script do let x : Either Int Int = Right 42 let x2 = map (\val -> show x <> "hello") x pure ()It fails with
couldn't match expected type ‘[a0]’ with actual type ‘Either Int Int’(I know how I could write a
mapfunction myself, I am just surprised I can’t find one.) - cocreature 1
OptionandEitherare both instances of Functor so you can usefmap:testEMap : Script () testEMap = script do let x : Either Int Int = Right 42 let x2 = fmap (\val -> show x <> "hello") x pure () - Arne_Gebert
Thanks!
- Leonid_Rozenberg
L33t D@ml hackers use
<$>and point free style to avoid @cocreature 's bug!let x2 = (<> "hello") . show <$> x