Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New function mapOr : (a -> b) -> b -> Maybe a -> Maybe b #81

Open
Aleod-m opened this issue Apr 2, 2024 · 0 comments
Open

New function mapOr : (a -> b) -> b -> Maybe a -> Maybe b #81

Aleod-m opened this issue Apr 2, 2024 · 0 comments

Comments

@Aleod-m
Copy link

Aleod-m commented Apr 2, 2024

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant