>>> import GHC.Internal.Text.Read ( readMaybe ) >>> let readMaybeInt = readMaybe :: String -> Maybe Int >>> mapMaybe readMaybeInt ["1", "Foo", "3"] [1,3] >>> catMaybes $ map readMaybeInt ["1", "Foo", "3"] [1,3]If we map the Just constructor, the entire list should be returned:
>>> mapMaybe Just [1,2,3] [1,2,3]
let f x = if x == "a" then Just "new a" else Nothing mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
let f x = if x == "a" then Just "new a" else Nothing mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
>>> toList $ mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..10]) :: [Int] [1,3,5,7,9]
>>> mapMaybe (\x -> if odd x then Just x else Nothing) (source [0..2]) :: SourceT Identity Int fromStepT (Effect (Identity (Skip (Yield 1 (Skip Stop)))))Illustrates why we need Skip.
let f x = if x == "a" then Just "new a" else Nothing mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"
mapMaybe (f >=> g) = mapMaybe f >-> mapMaybe g mapMaybe (pure @Maybe . f) = mapMaybe (Just . f) = map f mapMaybe (const Nothing) = drainAs a result of the second law,
mapMaybe return = mapMaybe Just = cat
>>> import Text.Read ( readMaybe ) >>> let readMaybeInt = readMaybe :: String -> Maybe Int >>> mapMaybe readMaybeInt ["1", "Foo", "3"] [1,3] >>> catMaybes $ map readMaybeInt ["1", "Foo", "3"] [1,3]If we map the Just constructor, the entire list should be returned:
>>> mapMaybe Just [1,2,3] [1,2,3]
let f x = if x == "a" then Just "new a" else Nothing mapMaybe f (fromList [(5,"a"), (3,"b")]) == singleton 5 "new a"