Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function to map Maybes to a monad

I often use expressions of the form maybe (return ()) someFunc someMaybe.

Searching for a function Monad m => (a -> m ()) -> Maybe a -> m () on hoogle doesn't yield a specific result. Isn't there any library function for this?

like image 598
Duschvorhang Avatar asked Dec 04 '11 11:12

Duschvorhang


People also ask

Is maybe a functor?

Summary. Together with a functor called Either, Maybe is one of the workhorses of statically typed functional programming.

Is map a monad?

Map is not one of the defining properties of monads, however, because it's technically just a special case of FlatMap. A lifting function like Unit will wrap its object in a container, even if that object is itself the same type of container.

What is Haskell monad function?

What is a Monad? A monad is an algebraic structure in category theory, and in Haskell it is used to describe computations as sequences of steps, and to handle side effects such as state and IO. Monads are abstract, and they have many useful concrete instances. Monads provide a way to structure a program.

Is maybe a monad?

The Maybe type is also a monad. It is a simple kind of error monad, where all errors are represented by Nothing . A richer error monad can be built using the Either type.


1 Answers

You can use mapM_ or forM_ from Data.Foldable for this. See also the recent discussion on the librariers mailing list.

like image 137
Roman Cheplyaka Avatar answered Sep 19 '22 20:09

Roman Cheplyaka