:: [Maybe a] -> [a]

The catMaybes function takes a list of Maybes and returns a list of all the Just values.

Examples

Basic usage:
>>> catMaybes [Just 1, Nothing, Just 3]
[1,3]
When constructing a list of Maybe values, catMaybes can be used to return all of the "success" results (if the list is the result of a map, then mapMaybe would be more appropriate):
>>> import Text.Read ( readMaybe )

>>> [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[Just 1,Nothing,Just 3]

>>> catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]
[1,3]
This is the cousin of takeWhile analogously to catMaybes being the cousin of filter.
>>> takeWhileJust [Just 'a', Just 'b', Nothing, Just 'c']
"ab"
Example: Keep the heads of sublists until an empty list occurs.
>>> takeWhileJust $ map (fmap fst . viewL) ["abc","def","","xyz"]
"ad"
For consistency with takeWhile, partitionMaybe and dropWhileNothing it should have been:
takeWhileJust_ :: (a -> Maybe b) -> a -> [b]
However, both variants are interchangeable:
takeWhileJust_ f == takeWhileJust . map f
takeWhileJust == takeWhileJust_ id
Analogous to catMaybes in Data.Maybe.
Keep running an operation until it becomes a Just, then return the value inside the Just as the result of the overall loop.
Run the supplied Maybe computation repeatedly until it returns a value. Returns that value.
Takes all of the Just values from a sequence of Maybe ts and concatenates them into an unboxed sequence of ts. Since 0.6.2
Keep running an operation until it becomes a Nothing, accumulating the monoid results inside the Justs as the result of the overall loop.
Evaluate each action in the sequence from left to right, and collect the results.
This is a version of sequence based on difference lists. It is slightly faster but we mostly use it because it uses the heap instead of the stack. This has the advantage that Neil Mitchell’s trick of limiting the stack size to discover space leaks doesn’t show this as a false positive.
Evaluate each action in the structure from left to right, and collect the results. For a version that ignores the results see sequenceA_.

Examples

Basic usage: For the first two examples we show sequenceA fully evaluating a a structure and collecting the results.
>>> sequenceA [Just 1, Just 2, Just 3]
Just [1,2,3]
>>> sequenceA [Right 1, Right 2, Right 3]
Right [1,2,3]
The next two example show Nothing and Just will short circuit the resulting structure if present in the input. For more context, check the Traversable instances for Either and Maybe.
>>> sequenceA [Just 1, Just 2, Just 3, Nothing]
Nothing
>>> sequenceA [Right 1, Right 2, Right 3, Left 4]
Left 4
Evaluate each action in the structure from left to right, and collect the results. For a version that ignores the results see sequenceA_.
Evaluate each action in the structure from left to right, and and collect the results. For a version that ignores the results see sequenceA_.
Evaluate each monadic action in the structure from left to right, and collect the results. For a version that ignores the results see sequence_.

Examples

Basic usage: The first two examples are instances where the input and and output of sequence are isomorphic.
>>> sequence $ Right [1,2,3,4]
[Right 1,Right 2,Right 3,Right 4]
>>> sequence $ [Right 1,Right 2,Right 3,Right 4]
Right [1,2,3,4]
The following examples demonstrate short circuit behavior for sequence.
>>> sequence $ Left [1,2,3,4]
Left [1,2,3,4]
>>> sequence $ [Left 0, Right 1,Right 2,Right 3,Right 4]
Left 0
Evaluate each monadic action in the structure from left to right, and collect the results. For a version that ignores the results see sequence_.