|
| Data.ListLike | | Portability | portable | | Stability | provisional | | Maintainer | John Goerzen <jgoerzen@complete.org> |
|
|
|
|
|
| Description |
Generic operations over list-like structures
Written by John Goerzen, jgoerzen@complete.org
Please start with the introduction at Data.ListLike#intro.
|
|
| Synopsis |
|
| empty :: ListLike full item => full | | | singleton :: ListLike full item => item -> full | | | cons :: ListLike full item => item -> full -> full | | | snoc :: ListLike full item => full -> item -> full | | | append :: ListLike full item => full -> full -> full | | | head :: ListLike full item => full -> item | | | last :: ListLike full item => full -> item | | | tail :: ListLike full item => full -> full | | | init :: ListLike full item => full -> full | | | null :: ListLike full item => full -> Bool | | | length :: ListLike full item => full -> Int | | | map :: (ListLike full item, ListLike full' item') => (item -> item') -> full -> full' | | | rigidMap :: ListLike full item => (item -> item) -> full -> full | | | reverse :: ListLike full item => full -> full | | | intersperse :: ListLike full item => item -> full -> full | | | toList :: ListLike full item => full -> [item] | | | fromList :: ListLike full item => [item] -> full | | | fromListLike :: (ListLike full item, ListLike full' item) => full -> full' | | | foldl :: FoldableLL full item => (a -> item -> a) -> a -> full -> a | | | foldl' :: FoldableLL full item => (a -> item -> a) -> a -> full -> a | | | foldl1 :: FoldableLL full item => (item -> item -> item) -> full -> item | | | foldr :: FoldableLL full item => (item -> b -> b) -> b -> full -> b | | | foldr' :: FoldableLL full item => (item -> b -> b) -> b -> full -> b | | | foldr1 :: FoldableLL full item => (item -> item -> item) -> full -> item | | | concat :: (ListLike full item, ListLike full' full, Monoid full) => full' -> full | | | concatMap :: (ListLike full item, ListLike full' item') => (item -> full') -> full -> full' | | | rigidConcatMap :: ListLike full item => (item -> full) -> full -> full | | | and :: ListLike full Bool => full -> Bool | | | or :: ListLike full Bool => full -> Bool | | | any :: ListLike full item => (item -> Bool) -> full -> Bool | | | all :: ListLike full item => (item -> Bool) -> full -> Bool | | | sum :: (Num a, ListLike full a) => full -> a | | | product :: (Num a, ListLike full a) => full -> a | | | maximum :: (ListLike full item, Ord item) => full -> item | | | minimum :: (ListLike full item, Ord item) => full -> item | | | fold :: (FoldableLL full item, Monoid item) => full -> item | | | foldMap :: (FoldableLL full item, Monoid m) => (item -> m) -> full -> m | | | iterate :: InfiniteListLike full item => (item -> item) -> item -> full | | | repeat :: InfiniteListLike full item => item -> full | | | replicate :: ListLike full item => Int -> item -> full | | | cycle :: InfiniteListLike full item => full -> full | | | take :: ListLike full item => Int -> full -> full | | | drop :: ListLike full item => Int -> full -> full | | | splitAt :: ListLike full item => Int -> full -> (full, full) | | | takeWhile :: ListLike full item => (item -> Bool) -> full -> full | | | dropWhile :: ListLike full item => (item -> Bool) -> full -> full | | | span :: ListLike full item => (item -> Bool) -> full -> (full, full) | | | break :: ListLike full item => (item -> Bool) -> full -> (full, full) | | | group :: (ListLike full item, ListLike full' full, Eq item) => full -> full' | | | inits :: (ListLike full item, ListLike full' full) => full -> full' | | | tails :: (ListLike full item, ListLike full' full) => full -> full' | | | isPrefixOf :: (ListLike full item, Eq item) => full -> full -> Bool | | | isSuffixOf :: (ListLike full item, Eq item) => full -> full -> Bool | | | isInfixOf :: (ListLike full item, Eq item) => full -> full -> Bool | | | elem :: (ListLike full item, Eq item) => item -> full -> Bool | | | notElem :: (ListLike full item, Eq item) => item -> full -> Bool | | | find :: ListLike full item => (item -> Bool) -> full -> Maybe item | | | filter :: ListLike full item => (item -> Bool) -> full -> full | | | partition :: ListLike full item => (item -> Bool) -> full -> (full, full) | | | index :: ListLike full item => full -> Int -> item | | | elemIndex :: (ListLike full item, Eq item) => item -> full -> Maybe Int | | | elemIndices :: (ListLike full item, Eq item, ListLike result Int) => item -> full -> result | | | findIndex :: ListLike full item => (item -> Bool) -> full -> Maybe Int | | | findIndices :: (ListLike full item, ListLike result Int) => (item -> Bool) -> full -> result | | | zip :: (ListLike full item, ListLike fullb itemb, ListLike result (item, itemb)) => full -> fullb -> result | | | zipWith :: (ListLike full item, ListLike fullb itemb, ListLike result resultitem) => (item -> itemb -> resultitem) -> full -> fullb -> result | | | unzip :: (ListLike full (itema, itemb), ListLike ra itema, ListLike rb itemb) => full -> (ra, rb) | | | sequence :: (ListLike full item, Monad m, ListLike fullinp (m item)) => fullinp -> m full | | | sequence_ :: (Monad m, ListLike mfull (m item)) => mfull -> m () | | | mapM :: (ListLike full item, Monad m, ListLike full' item') => (item -> m item') -> full -> m full' | | | rigidMapM :: (ListLike full item, Monad m) => (item -> m item) -> full -> m full | | | mapM_ :: (ListLike full item, Monad m) => (item -> m b) -> full -> m () | | | class ListLike full item => ListLikeIO full item | full -> item where | | | | toString :: StringLike s => s -> String | | | fromString :: StringLike s => String -> s | | | lines :: (StringLike s, ListLike full s) => s -> full | | | words :: (StringLike s, ListLike full s) => s -> full | | | nub :: (ListLike full item, Eq item) => full -> full | | | delete :: (ListLike full item, Eq item) => item -> full -> full | | | deleteFirsts :: (ListLike full item, Eq item) => full -> full -> full | | | union :: (ListLike full item, Eq item) => full -> full -> full | | | intersect :: (ListLike full item, Eq item) => full -> full -> full | | | sort :: (ListLike full item, Ord item) => full -> full | | | insert :: (ListLike full item, Ord item) => item -> full -> full | | | nubBy :: ListLike full item => (item -> item -> Bool) -> full -> full | | | deleteBy :: ListLike full item => (item -> item -> Bool) -> item -> full -> full | | | deleteFirstsBy :: ListLike full item => (item -> item -> Bool) -> full -> full -> full | | | unionBy :: ListLike full item => (item -> item -> Bool) -> full -> full -> full | | | intersectBy :: ListLike full item => (item -> item -> Bool) -> full -> full -> full | | | groupBy :: (ListLike full item, ListLike full' full, Eq item) => (item -> item -> Bool) -> full -> full' | | | sortBy :: (ListLike full item, Ord item) => (item -> item -> Ordering) -> full -> full | | | insertBy :: (ListLike full item, Ord item) => (item -> item -> Ordering) -> item -> full -> full | | | genericLength :: (ListLike full item, Num a) => full -> a | | | genericTake :: (ListLike full item, Integral a) => a -> full -> full | | | genericDrop :: (ListLike full item, Integral a) => a -> full -> full | | | genericSplitAt :: (ListLike full item, Integral a) => a -> full -> (full, full) | | | genericReplicate :: (ListLike full item, Integral a) => a -> item -> full | | | class (FoldableLL full item, Monoid full) => ListLike full item | full -> item where | | empty :: full | | singleton :: item -> full | | cons :: item -> full -> full | | snoc :: full -> item -> full | | append :: full -> full -> full | | head :: full -> item | | last :: full -> item | | tail :: full -> full | | init :: full -> full | | null :: full -> Bool | | length :: full -> Int | | map :: ListLike full' item' => (item -> item') -> full -> full' | | rigidMap :: (item -> item) -> full -> full | | reverse :: full -> full | | intersperse :: item -> full -> full | | concat :: (ListLike full' full, Monoid full) => full' -> full | | concatMap :: ListLike full' item' => (item -> full') -> full -> full' | | rigidConcatMap :: (item -> full) -> full -> full | | any :: (item -> Bool) -> full -> Bool | | all :: (item -> Bool) -> full -> Bool | | maximum :: Ord item => full -> item | | minimum :: Ord item => full -> item | | replicate :: Int -> item -> full | | take :: Int -> full -> full | | drop :: Int -> full -> full | | splitAt :: Int -> full -> (full, full) | | takeWhile :: (item -> Bool) -> full -> full | | dropWhile :: (item -> Bool) -> full -> full | | span :: (item -> Bool) -> full -> (full, full) | | break :: (item -> Bool) -> full -> (full, full) | | group :: (ListLike full' full, Eq item) => full -> full' | | inits :: ListLike full' full => full -> full' | | tails :: ListLike full' full => full -> full' | | isPrefixOf :: Eq item => full -> full -> Bool | | isSuffixOf :: Eq item => full -> full -> Bool | | isInfixOf :: Eq item => full -> full -> Bool | | elem :: Eq item => item -> full -> Bool | | notElem :: Eq item => item -> full -> Bool | | find :: (item -> Bool) -> full -> Maybe item | | filter :: (item -> Bool) -> full -> full | | partition :: (item -> Bool) -> full -> (full, full) | | index :: full -> Int -> item | | elemIndex :: Eq item => item -> full -> Maybe Int | | elemIndices :: (Eq item, ListLike result Int) => item -> full -> result | | findIndex :: (item -> Bool) -> full -> Maybe Int | | findIndices :: ListLike result Int => (item -> Bool) -> full -> result | | sequence :: (Monad m, ListLike fullinp (m item)) => fullinp -> m full | | mapM :: (Monad m, ListLike full' item') => (item -> m item') -> full -> m full' | | rigidMapM :: Monad m => (item -> m item) -> full -> m full | | mapM_ :: Monad m => (item -> m b) -> full -> m () | | nub :: Eq item => full -> full | | delete :: Eq item => item -> full -> full | | deleteFirsts :: Eq item => full -> full -> full | | union :: Eq item => full -> full -> full | | intersect :: Eq item => full -> full -> full | | sort :: Ord item => full -> full | | insert :: Ord item => item -> full -> full | | toList :: full -> [item] | | fromList :: [item] -> full | | fromListLike :: ListLike full' item => full -> full' | | nubBy :: (item -> item -> Bool) -> full -> full | | deleteBy :: (item -> item -> Bool) -> item -> full -> full | | deleteFirstsBy :: (item -> item -> Bool) -> full -> full -> full | | unionBy :: (item -> item -> Bool) -> full -> full -> full | | intersectBy :: (item -> item -> Bool) -> full -> full -> full | | groupBy :: (ListLike full' full, Eq item) => (item -> item -> Bool) -> full -> full' | | sortBy :: Ord item => (item -> item -> Ordering) -> full -> full | | insertBy :: Ord item => (item -> item -> Ordering) -> item -> full -> full | | genericLength :: Num a => full -> a | | genericTake :: Integral a => a -> full -> full | | genericDrop :: Integral a => a -> full -> full | | genericSplitAt :: Integral a => a -> full -> (full, full) | | genericReplicate :: Integral a => a -> item -> full |
| | | class FoldableLL full item | full -> item where | | foldl :: (a -> item -> a) -> a -> full -> a | | foldl' :: (a -> item -> a) -> a -> full -> a | | foldl1 :: (item -> item -> item) -> full -> item | | foldr :: (item -> b -> b) -> b -> full -> b | | foldr' :: (item -> b -> b) -> b -> full -> b | | foldr1 :: (item -> item -> item) -> full -> item |
| | | class StringLike s where | | | | class ListLike full item => InfiniteListLike full item | full -> item where | |
|
|
|
| Introduction
|
|
Welcome to ListLike.
This module provides abstractions over typical list operations.
It is designed to let you freely interchange different ways to represent
sequences of data. It works with lists, various types of ByteStrings,
and much more.
In this module, you'll find generic versions of most of the functions
you're used to using in the Prelude, Data.List, and System.IO.
They carry the
same names, too. Therefore, you'll want to be careful how you import
the module. I suggest using:
import qualified ListLike as LL
Then, you can use LL.fold, LL.map, etc. to get the generic version of
the functions you want. Alternatively, you can hide the other versions
from Prelude and import specific generic functions from here, such as:
import Prelude hiding (map)
import ListLike (map)
The module Data.ListLike actually simply re-exports the items found
in a number of its sub-modules. If you want a smaller subset of
Data.ListLike, look at the documentation for its sub-modules and import
the relevant one.
In most cases, functions here can act as drop-in replacements for their
list-specific counterparts. They will use the same underlying implementations
for lists, so there should be no performance difference.
You can make your own types instances of ListLike as well. For more
details, see the notes for the ListLike typeclass.
|
|
| Creation & Basic Functions
|
|
|
| The empty list
|
|
|
| Creates a single-element list out of an element
|
|
|
| Like (:) for lists: adds an element to the beginning of a list
|
|
|
| Adds an element to the *end* of a ListLike.
|
|
|
| Combines two lists. Like (++).
|
|
|
| Extracts the first element of a ListLike.
|
|
|
| Extracts the last element of a ListLike.
|
|
|
| Gives all elements after the head.
|
|
|
| All elements of the list except the last one. See also inits.
|
|
|
| Tests whether the list is empty.
|
|
|
| Length of the list. See also genericLength.
|
|
| List transformations
|
|
|
| Apply a function to each element, returning any other
valid ListLike. rigidMap will always be at least
as fast, if not faster, than this function and is recommended
if it will work for your purposes. See also mapM.
|
|
| rigidMap :: ListLike full item => (item -> item) -> full -> full | Source |
|
| Like map, but without the possibility of changing the type of
the item. This can have performance benefits for things such as
ByteStrings, since it will let the ByteString use its native
low-level map implementation.
|
|
|
| Reverse the elements in a list.
|
|
|
| Add an item between each element in the structure
|
|
| Conversions
|
|
|
| Converts the structure to a list. This is logically equivolent
to fromListLike, but may have a more optimized implementation.
|
|
|
| Generates the structure from a list.
|
|
|
| Converts one ListLike to another. See also toList.
Default implementation is fromListLike = map id
|
|
| Reducing lists (folds), from FoldableLL
|
|
|
| Left-associative fold
|
|
|
| Strict version of foldl.
|
|
|
| A variant of foldl with no base case. Requires at least 1
list element.
|
|
|
| Right-associative fold
|
|
|
| Strict version of foldr
|
|
|
| Like foldr, but with no starting value
|
|
| Special folds
|
|
|
| Flatten the structure.
|
|
|
| Map a function over the items and concatenate the results.
See also rigidConcatMap.
|
|
| rigidConcatMap :: ListLike full item => (item -> full) -> full -> full | Source |
|
| Like concatMap, but without the possibility of changing
the type of the item. This can have performance benefits
for some things such as ByteString.
|
|
|
| Returns True if all elements are True
|
|
|
| Returns True if any element is True
|
|
|
| True if any items satisfy the function
|
|
|
| True if all items satisfy the function
|
|
|
| The sum of the list
|
|
|
| The product of the list
|
|
|
| The maximum value of the list
|
|
|
| The minimum value of the list
|
|
|
| Combine the elements of a structure using a monoid.
fold = foldMap id
|
|
|
| Map each element to a monoid, then combine the results
|
|
| Building lists
|
|
| Scans
|
|
| Accumulating maps
|
|
| Infinite lists
|
|
|
| An infinite list of repeated calls of the function to args
|
|
|
| An infinite list where each element is the same
|
|
|
| Generate a structure with the specified length with every element
set to the item passed in. See also genericReplicate
|
|
|
| Converts a finite list into a circular one
|
|
| Unfolding
|
|
| Sublists
|
|
| Extracting sublists
|
|
|
| Takes the first n elements of the list. See also genericTake.
|
|
|
| Drops the first n elements of the list. See also genericDrop
|
|
|
| Equivalent to (take n xs, drop n xs). See also genericSplitAt.
|
|
|
| Returns all elements at start of list that satisfy the function.
|
|
|
| Drops all elements form the start of the list that satisfy the
function.
|
|
|
| The equivalent of (takeWhile f xs, dropWhile f xs)
|
|
|
| The equivalent of span (not . f)
|
|
|
| Split a list into sublists, each which contains equal arguments.
For order-preserving types, concatenating these sublists will produce
the original list. See also groupBy.
|
|
|
| All initial segments of the list, shortest first
|
|
|
| All final segnemts, longest first
|
|
| Predicates
|
|
|
| True when the first list is at the beginning of the second.
|
|
|
| True when the first list is at the beginning of the second.
|
|
|
| True when the first list is wholly containted within the second
|
|
| Searching lists
|
|
| Searching by equality
|
|
|
| True if the item occurs in the list
|
|
|
| True if the item does not occur in the list
|
|
| Searching with a predicate
|
|
|
| Take a function and return the first matching element, or Nothing
if there is no such element.
|
|
|
| Returns only the elements that satisfy the function.
|
|
|
| Returns the lists that do and do not satisfy the function.
Same as (filter p xs, filter (not . p) xs)
|
|
| Indexing lists
|
|
|
| The element at 0-based index i. Raises an exception if i is out
of bounds. Like (!!) for lists.
|
|
|
| Returns the index of the element, if it exists.
|
|
|
| Returns the indices of the matching elements. See also
findIndices
|
|
|
| Take a function and return the index of the first matching element,
or Nothing if no element matches
|
|
|
| Returns the indices of all elements satisfying the function
|
|
| Zipping and unzipping lists
|
|
|
| Takes two lists and returns a list of corresponding pairs.
|
|
|
| Takes two lists and combines them with a custom combining function
|
|
|
| Converts a list of pairs into two separate lists of elements
|
|
| Monadic Operations
|
|
|
| Evaluate each action in the sequence and collect the results
|
|
|
| Evaluate each action, ignoring the results
|
|
|
A map in monad space. Same as sequence . map
See also rigidMapM
|
|
|
| Like mapM, but without the possibility of changing the type
of the item. This can have performance benefits with some types.
|
|
|
| A map in monad space, discarding results. Same as
sequence_ . map
|
|
| Input and Output
|
|
| class ListLike full item => ListLikeIO full item | full -> item where | Source |
|
An extension to ListLike for those data types that support
I/O. These functions mirror those in System.IO for the most part. They
also share the same names; see the comments in Data.ListLike for help
importing them.
Note that some types may not be capable of lazy reading or writing.
Therefore, the usual semantics of System.IO functions regarding laziness
may or may not be available from a particular implementation.
Minimal complete definition:
- hGetLine
- hGetContents
- hGet
- hGetNonBlocking
- hPutStr
| | | Methods | | | | | | | | | | | | | | | | | | | | | | interact :: (full -> full) -> IO () | Source |
| | | | | | |
| | Instances | |
|
|
| Special lists
|
|
| Strings
|
|
|
| Converts the structure to a String
|
|
|
| Converts a String to a list
|
|
|
| Breaks a string into a list of strings
|
|
|
| Breaks a string into a list of words
|
|
| "Set" operations
|
|
|
| Removes duplicate elements from the list. See also nubBy
|
|
|
| Removes the first instance of the element from the list.
See also deleteBy
|
|
|
| List difference. Removes from the first list the first instance
of each element of the second list. See '(\)' and deleteFirstsBy
|
|
|
| List union: the set of elements that occur in either list.
Duplicate elements in the first list will remain duplicate.
See also unionBy.
|
|
|
| List intersection: the set of elements that occur in both lists.
See also intersectBy
|
|
| Ordered lists
|
|
|
| Sorts the list. On data types that do not preserve ordering,
or enforce their own ordering, the result may not be what
you expect. See also sortBy.
|
|
|
| Inserts the element at the last place where it is still less than or
equal to the next element. On data types that do not preserve
ordering, or enforce their own ordering, the result may not
be what you expect. On types such as maps, this may result in
changing an existing item. See also insertBy.
|
|
| Generalized functions
|
|
| The "By" operations
|
|
| User-supplied equality (replacing an Eq context)
|
|
|
| Generic version of nub
|
|
|
| Generic version of deleteBy
|
|
| deleteFirstsBy :: ListLike full item => (item -> item -> Bool) -> full -> full -> full | Source |
|
| Generic version of deleteFirsts
|
|
|
| Generic version of union
|
|
|
| Generic version of intersect
|
|
|
| Gen |