extra-0.1: Extra functions I use.

Safe HaskellSafe-Inferred

Data.List.Extra

Description

This module extends Data.List with extra functions of a similar nature. The package also exports the existing Data.List functions. Some of the names and semantics were inspired by the text package.

Synopsis

Documentation

module Data.List

trimBy :: (a -> Bool) -> [a] -> [a]Source

list :: b -> (a -> [a] -> b) -> [a] -> bSource

uncons :: [a] -> (a, [a])Source

unsnoc :: [a] -> ([a], a)Source

groupSort :: Ord k => [(k, v)] -> [(k, [v])]Source

groupSortOn :: Ord a => (k -> a) -> [(k, v)] -> [(k, [v])]Source

nubOn :: Eq b => (a -> b) -> [a] -> [a]Source

groupOn :: Eq b => (a -> b) -> [a] -> [[a]]Source

sortOn :: Ord b => (a -> b) -> [a] -> [a]Source

chop :: ([a] -> (b, [a])) -> [a] -> [b]Source

for :: [a] -> (a -> b) -> [b]Source

rep :: Eq a => a -> a -> a -> aSource

reps :: Eq a => a -> a -> [a] -> [a]Source

disjoint :: Eq a => [a] -> [a] -> BoolSource

distinct :: Eq a => [a] -> BoolSource

dropEnd :: Int -> [a] -> [a]Source

takeEnd :: Int -> [a] -> [a]Source

breakEnd :: (a -> Bool) -> [a] -> ([a], [a])Source

spanEnd :: (a -> Bool) -> [a] -> ([a], [a])Source

dropWhileEnd :: (a -> Bool) -> [a] -> [a]

The dropWhileEnd function drops the largest suffix of a list in which the given predicate holds for all elements. For example:

 dropWhileEnd isSpace "foo\n" == "foo"
 dropWhileEnd isSpace "foo bar" == "foo bar"
 dropWhileEnd isSpace ("foo\n" ++ undefined) == "foo" ++ undefined

takeWhileEnd :: (a -> Bool) -> [a] -> [a]Source

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

Return the prefix of the second string if its suffix matches the entire first string.

Examples:

 stripSuffix "bar" "foobar" == Just "foo"
 stripSuffix ""    "baz"    == Just "baz"
 stripSuffix "foo" "quux"   == Nothing

concatUnzip :: [([a], [b])] -> ([a], [b])Source

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

mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]Source

wordsBy :: (a -> Bool) -> [a] -> [[a]]Source

linesBy :: (a -> Bool) -> [a] -> [[a]]Source

firstJust :: (a -> Maybe b) -> [a] -> Maybe bSource

breakOn :: Eq a => [a] -> [a] -> ([a], [a])Source

Find the first instance of needle in haystack. The first element of the returned tuple is the prefix of haystack before needle is matched. The second is the remainder of haystack, starting with the match.

Examples:

 breakOn "::" "a::b::c" == ("a", "::b::c")
 breakOn "/" "foobar"   == ("foobar", "")

Laws:

 \needle haystack -> let (prefix,match) = breakOn needle haystack in prefix ++ match == haystack

breakOnEnd :: Eq a => [a] -> [a] -> ([a], [a])Source

Similar to breakOn, but searches from the end of the string.

The first element of the returned tuple is the prefix of haystack up to and including the last match of needle. The second is the remainder of haystack, following the match.

 breakOnEnd "::" "a::b::c" == ("a::b::", "c")

splitOn :: Eq a => [a] -> [a] -> [[a]]Source

Break a list into pieces separated by the first list argument, consuming the delimiter. An empty delimiter is invalid, and will cause an error to be raised.

Examples:

 splitOn "\r\n" "a\r\nb\r\nd\r\ne" == ["a","b","d","e"]
 splitOn "aaa"  "aaaXaaaXaaaXaaa"  == ["","X","X","X",""]
 splitOn "x"    "x"                == ["",""]
 splitOn "x"    ""                 == [""]

and

 \s x -> s /= "" ==> intercalate s (splitOn s x) == x
 \c x -> splitOn [c] x                           == split (==c) x

split :: (a -> Bool) -> [a] -> [[a]]Source

Splits a list into components delimited by separators, where the predicate returns True for a separator element. The resulting components do not contain the separators. Two adjacent separators result in an empty component in the output. eg.

 split (=='a') "aabbaca" == ["","","bb","c",""]
 split (=='a') ""        == [""]

chunksOf :: Int -> [a] -> [[a]]Source