maybe-extra icon indicating copy to clipboard operation
maybe-extra copied to clipboard

New function `mapOr : (a -> b) -> b -> Maybe a -> Maybe b`

Open Aleod-m opened this issue 10 months ago • 0 comments

I find this pattern often in various cases where i want to map an optional value or put a default if its nothing but keep it optional.

mapOr : (a -> b) -> b -> Maybe a -> Maybe b
mapOr fn default ma =
	case ma of 
		Nothing -> Just default 
		Just a -> Just (fn a)
-- Changes :
 ma |> Maybe.map f |> Maybe.withDefault d |> Just 
-- or
 ma |> Maybe.map f |> Maybe.Extra.orElse (Just d)
-- To :
 ma |> MapOr f d

Aleod-m avatar Apr 02 '24 11:04 Aleod-m