Safe Haskell | Safe |
---|---|

Language | Haskell2010 |

- module Data.List
- catMaybes :: [Maybe a] -> [a]
- chunksOf :: Int -> [a] -> [[a]]
- extractLast :: a -> [a] -> ([a], a)
- groupOn :: Eq b => (a -> b) -> [a] -> [(b, [a])]
- groupOn' :: Eq b => (a -> (b, c)) -> [a] -> [(b, [c])]
- groupOnSort :: Ord b => (a -> b) -> [a] -> [(b, [a])]
- groupOnSort' :: Ord b => (a -> (b, c)) -> [a] -> [(b, [c])]
- groupBySort :: Ord b => (b -> b -> Ordering) -> (a -> b) -> [a] -> [(b, [a])]
- groupUnsortedOn :: forall a b. Eq b => (a -> b) -> [a] -> [(b, [a])]
- headM :: Monad m => [a] -> m a
- lastElems :: Int -> [a] -> [a]
- lastM :: Monad m => [a] -> m a
- lookupM :: (Eq a, Monad m) => (a -> String) -> a -> [(a, b)] -> m b
- makeMapping :: (Eq a, Hashable a) => [(a, b)] -> [(a, b)]
- mapMaybe :: (a -> Maybe b) -> [a] -> [b]
- maximumM :: (Monad m, Ord a) => [a] -> m a
- merge :: Ord a => [a] -> [a] -> [a]
- middle :: [a] -> Maybe a
- minimumM :: (Monad m, Ord a) => [a] -> m a
- monotone :: Ord a => [a] -> Bool
- nubMerge :: Ord a => [a] -> [a] -> [a]
- prefixesAndSuffixes :: [a] -> [([a], [a])]
- sconcatBy :: (Ord b, Foldable f, Semigroup s) => (a -> b) -> (a -> s) -> f a -> [(b, s)]
- spanTailRec :: (a -> Bool) -> [a] -> ([a], [a])
- stripSuffix :: Eq a => [a] -> [a] -> Maybe [a]
- tryStripPrefix :: Eq a => [a] -> [a] -> [a]
- ungroupMay :: [(a, [b])] -> Maybe [(a, b)]
- withLast :: (a -> a) -> [a] -> [a]

# Documentation

module Data.List

catMaybes :: [Maybe a] -> [a] #

The `catMaybes`

function takes a list of `Maybe`

s and returns
a list of all the `Just`

values.

#### Examples

Basic usage:

`>>>`

[1,3]`catMaybes [Just 1, Nothing, Just 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 )`

`>>>`

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

`>>>`

[1,3]`catMaybes $ [readMaybe x :: Maybe Int | x <- ["1", "Foo", "3"] ]`

extractLast :: a -> [a] -> ([a], a) Source #

groupOnSort :: Ord b => (a -> b) -> [a] -> [(b, [a])] Source #

groupOnSort' :: Ord b => (a -> (b, c)) -> [a] -> [(b, [c])] Source #

groupBySort :: Ord b => (b -> b -> Ordering) -> (a -> b) -> [a] -> [(b, [a])] Source #

groupUnsortedOn :: forall a b. Eq b => (a -> b) -> [a] -> [(b, [a])] Source #

makeMapping :: (Eq a, Hashable a) => [(a, b)] -> [(a, b)] Source #

mapMaybe :: (a -> Maybe b) -> [a] -> [b] #

The `mapMaybe`

function is a version of `map`

which can throw
out elements. In particular, the functional argument returns
something of type

. If this is `Maybe`

b`Nothing`

, no element
is added on to the result list. If it is

, then `Just`

b`b`

is
included in the result list.

#### Examples

Using

is a shortcut for `mapMaybe`

f x

in most cases:`catMaybes`

$ `map`

f x

`>>>`

`import Text.Read ( readMaybe )`

`>>>`

`let readMaybeInt = readMaybe :: String -> Maybe Int`

`>>>`

[1,3]`mapMaybe readMaybeInt ["1", "Foo", "3"]`

`>>>`

[1,3]`catMaybes $ map readMaybeInt ["1", "Foo", "3"]`

If we map the `Just`

constructor, the entire list should be returned:

`>>>`

[1,2,3]`mapMaybe Just [1,2,3]`

merge :: Ord a => [a] -> [a] -> [a] Source #

Merge two sorted list so that the resulting list is sorted as well. and contains all elements from one of the lists. The length of the resulting list is the sum of the lengths of the given lists.

middle :: [a] -> Maybe a Source #

Computes the element in the middle of the list If the list has an even number of elements, you will get the element after the middle of the list.

nubMerge :: Ord a => [a] -> [a] -> [a] Source #

Merge the two sorted lists and remove all duplicates.

prefixesAndSuffixes :: [a] -> [([a], [a])] Source #

spanTailRec :: (a -> Bool) -> [a] -> ([a], [a]) Source #

stripSuffix :: Eq a => [a] -> [a] -> Maybe [a] Source #

tryStripPrefix :: Eq a => [a] -> [a] -> [a] Source #

Strips as elements of a given prefix list as possible. Stops stripping if the prefix doesn't match anymore or is exhausted and returns the remaining string.

ungroupMay :: [(a, [b])] -> Maybe [(a, b)] Source #