|Maintainer||John Goerzen <firstname.lastname@example.org>|
This module provides various helpful utilities for dealing with lists.
Written by John Goerzen, email@example.com
- merge :: Ord a => [a] -> [a] -> [a]
- mergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
- startswith :: Eq a => [a] -> [a] -> Bool
- endswith :: Eq a => [a] -> [a] -> Bool
- contains :: Eq a => [a] -> [a] -> Bool
- hasAny :: Eq a => [a] -> [a] -> Bool
- addToAL :: Eq key => [(key, elt)] -> key -> elt -> [(key, elt)]
- delFromAL :: Eq key => [(key, a)] -> key -> [(key, a)]
- flipAL :: (Eq key, Eq val) => [(key, val)] -> [(val, [key])]
- keysAL :: [(key, a)] -> [key]
- valuesAL :: [(a, value)] -> [value]
- hasKeyAL :: Eq a => a -> [(a, b)] -> Bool
- strFromAL :: (Show a, Show b) => [(a, b)] -> String
- strToAL :: (Read a, Read b) => String -> [(a, b)]
- split :: Eq a => [a] -> [a] -> [[a]]
- join :: [a] -> [[a]] -> [a]
- replace :: Eq a => [a] -> [a] -> [a] -> [a]
- genericJoin :: Show a => String -> [a] -> String
- takeWhileList :: ([a] -> Bool) -> [a] -> [a]
- dropWhileList :: ([a] -> Bool) -> [a] -> [a]
- spanList :: ([a] -> Bool) -> [a] -> ([a], [a])
- breakList :: ([a] -> Bool) -> [a] -> ([a], [a])
- newtype WholeFunc a b = WholeFunc ([a] -> (WholeFunc a b, [a], [b]))
- wholeMap :: WholeFunc a b -> [a] -> [b]
- fixedWidth :: [Int] -> WholeFunc a [a]
- grab :: Int -> State [a] [a]
- countElem :: Eq a => a -> [a] -> Int
- elemRIndex :: Eq a => a -> [a] -> Maybe Int
- alwaysElemRIndex :: Eq a => a -> [a] -> Int
- seqList :: [a] -> [a]
- subIndex :: Eq a => [a] -> [a] -> Maybe Int
- uniq :: Eq a => [a] -> [a]
Merge two sorted lists into a single, sorted whole.
merge [1,3,5] [1,2,4,6] -> [1,1,2,3,4,5,6]
QuickCheck test property:
prop_merge xs ys = merge (sort xs) (sort ys) == sort (xs ++ ys) where types = xs :: [Int]
Merge two sorted lists using into a single, sorted whole, allowing the programmer to specify the comparison function.
QuickCheck test property:
prop_mergeBy xs ys = mergeBy cmp (sortBy cmp xs) (sortBy cmp ys) == sortBy cmp (xs ++ ys) where types = xs :: [ (Int, Int) ] cmp (x1,_) (x2,_) = compare x1 x2
Returns true if the given list starts with the specified elements; false otherwise. (This is an alias for Data.List.isPrefixOf.)
startswith "He" "Hello" -> True
Returns true if the given list ends with the specified elements; false otherwise. (This is an alias for Data.List.isSuffixOf.)
endswith "lo" "Hello" -> True
Returns true if the given parameter is a sublist of the given list; false otherwise.
contains "Haskell" "I really like Haskell." -> True contains "Haskell" "OCaml is great." -> False
This function was submitted to GHC and was applied as
isInfixOf. This function therefore is deprecated and will
be removed in future versions.
Returns true if the given list contains any of the elements in the search list.
Association List Utilities
Adds the specified (key, value) pair to the given list, removing any existing pair with the same key already present.
Removes all (key, value) pairs from the given list where the key matches the given one.
Flips an association list. Converts (key1, val), (key2, val) pairs to (val, [key1, key2]).
Returns the keys that comprise the (key, value) pairs of the given AL.
Returns the values the comprise the (key, value) pairs of the given AL.
Association List Conversions
Converts an association list to a string. The string will have one pair per line, with the key and value both represented as a Haskell string.
This function is designed to work with [(String, String)] association lists, but may work with other types as well.
Given a delimiter and a list (or string), split into components.
split "," "foo,bar,,baz," -> ["foo", "bar", "", "baz", ""]
split "ba" ",foo,bar,,baz," -> [",foo,","r,,","z,"]
Given a delimiter and a list of items (or strings), join the items by using the delimiter.
join "|" ["foo", "bar", "baz"] -> "foo|bar|baz"
Given a list and a replacement list, replaces each occurance of the search list with the replacement list in the operation list.
replace "," "." "127,0,0,1" -> "127.0.0.1"
This could logically be thought of as:
replace old new l = join new . split old $ l
join, but works with a list of anything showable, converting
it to a String.
genericJoin ", " [1, 2, 3, 4] -> "1, 2, 3, 4" genericJoin "|" ["foo", "bar", "baz"] -> "\"foo\"|\"bar\"|\"baz\""
Similar to Data.List.takeWhile, takes elements while the func is true. The function is given the remainder of the list to examine.
Similar to Data.List.dropWhile, drops elements while the func is true. The function is given the remainder of the list to examine.
Similar to Data.List.span, but performs the test on the entire remaining list instead of just one element.
spanList p xs is the same as
(takeWhileList p xs, dropWhileList p xs)
Similar to Data.List.break, but performs the test on the entire remaining list instead of just one element.
This is an enhanced version of the concatMap or map functions in Data.List.
Unlike those functions, this one:
- Can consume a varying number of elements from the input list during each iteration
- Can arbitrarily decide when to stop processing data
- Can return a varying number of elements to insert into the output list
- Can actually switch processing functions mid-stream
- Is not even restricted to processing the input list intact
The function used by wholeMap, of type
WholeFunc, is repeatedly called
with the input list. The function returns three things: the function
to call for the next iteration (if any), what remains of the input list,
and the list of output elements generated during this iteration. The return
wholeMap is the concatenation of the output element lists from
A parser designed to process fixed-width input fields. Use it with
The Int list passed to this function is the list of the field widths desired from the input. The result is a list of those widths, if possible. If any of the input remains after processing this list, it is added on as the final element in the result list. If the input is less than the sum of the requested widths, then the result list will be short the appropriate number of elements, and its final element may be shorter than requested.
wholeMap (fixedWidth [1, 2, 3]) "1234567890" --> ["1","23","456","7890"] wholeMap (fixedWidth (repeat 2)) "123456789" --> ["12","34","56","78","9"] wholeMap (fixedWidth ) "123456789" --> ["123456789"] wholeMap (fixedWidth [5, 3, 6, 1]) "Hello, This is a test." --> ["Hello",", T","his is"," ","a test."]
Fixed-Width and State Monad Utilities
Helps you pick out fixed-width components from a list.
conv :: String -> (String,String) conv = runState $ do f3 <- grab 3 n2 <- grab 2 return $ f3 ++ "," ++ n2 main = print $ conv "TestIng"
Returns a count of the number of times the given element occured in the given list.
Returns the rightmost index of the given element in the given list.
Like elemRIndex, but returns -1 if there is nothing found.
Similar to Data.List.elemIndex. Instead of looking for one element in a list, this function looks for the first occurance of a sublist in the list, and returns the index of the first element of that occurance. If there is no such list, returns Nothing.
If the list to look for is the empty list, will return Just 0 regardless of the content of the list to search.
subIndex "foo" "asdfoobar" -> Just 3 subIndex "foo"  -> Nothing subIndex ""  -> Just 0 subIndex "" "asdf" -> Just 0 subIndex "test" "asdftestbartest" -> Just 4 subIndex [(1::Int), 2] [0, 5, 3, 2, 1, 2, 4] -> Just 4
Given a list, returns a new list with all duplicate elements removed. For example:
uniq "Mississippi" -> "Misp"
You should not rely on this function necessarily preserving order, though the current implementation happens to.
This function is not compatible with infinite lists.
This is presently an alias for Data.List.nub