Copyright | Copyright (C) 2007 John Goerzen |
---|---|
License | BSD3 |
Maintainer | David Fox <dsf@seereason.com>, Andreas Abel |
Stability | stable |
Portability | portable |
Safe Haskell | None |
Language | Haskell2010 |
Data.ListLike.Base
Description
Generic operations over list-like structures
Written by John Goerzen, jgoerzen@complete.org
Synopsis
- class (IsList full, item ~ Item full, 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
- uncons :: full -> Maybe (item, full)
- 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 => 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 :: full -> item
- minimum :: 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
- dropWhileEnd :: (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 :: full -> full -> Bool
- isSuffixOf :: full -> full -> Bool
- isInfixOf :: full -> full -> Bool
- stripPrefix :: full -> full -> Maybe full
- stripSuffix :: full -> full -> Maybe full
- elem :: item -> full -> Bool
- notElem :: 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 :: 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 :: (Applicative m, ListLike fullinp (m item)) => fullinp -> m full
- mapM :: (Applicative m, ListLike full' item') => (item -> m item') -> full -> m full'
- rigidMapM :: Monad m => (item -> m item) -> full -> m full
- nub :: full -> full
- delete :: item -> full -> full
- deleteFirsts :: full -> full -> full
- union :: full -> full -> full
- intersect :: full -> full -> full
- sort :: full -> full
- insert :: 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 :: (item -> item -> Ordering) -> full -> full
- insertBy :: (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
- type ListOps full = ListLike full (Item full)
- toList :: IsList l => l -> [Item l]
- fromList :: IsList l => [Item l] -> l
- class ListLike full item => InfiniteListLike full item | full -> item where
- 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
- sequence_ :: (Monad m, FoldableLL full (m item)) => full -> m ()
Documentation
class (IsList full, item ~ Item full, FoldableLL full item, Monoid full) => ListLike full item | full -> item where #
The class implementing list-like functions.
It is worth noting that types such as Map
can be instances of
ListLike
. Due to their specific ways of operating, they may not behave
in the expected way in some cases. For instance, cons
may not increase
the size of a map if the key you have given is already in the map; it will
just replace the value already there.
Implementators must define at least:
- singleton
- head
- tail
- null or genericLength
Minimal complete definition
singleton, uncons, null | singleton, uncons, genericLength | singleton, head, tail, null | singleton, head, tail, genericLength
Methods
The empty list
Creates a single-element list out of an element
cons :: item -> full -> full #
Like (:) for lists: adds an element to the beginning of a list
snoc :: full -> item -> full #
Adds an element to the *end* of a ListLike
.
append :: full -> full -> full #
Combines two lists. Like (++).
Extracts the first element of a ListLike
.
uncons :: full -> Maybe (item, full) #
Extract head and tail, return Nothing if empty
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
.
map :: ListLike full' item' => (item -> item') -> full -> full' #
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 :: (item -> item) -> full -> full #
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.
intersperse :: item -> full -> full #
Add an item between each element in the structure
concat :: ListLike full' full => full' -> full #
Flatten the structure.
concatMap :: ListLike full' item' => (item -> full') -> full -> full' #
Map a function over the items and concatenate the results.
See also rigidConcatMap
.
rigidConcatMap :: (item -> full) -> full -> full #
Like concatMap
, but without the possibility of changing
the type of the item. This can have performance benefits
for some things such as ByteString.
any :: (item -> Bool) -> full -> Bool #
True if any items satisfy the function
all :: (item -> Bool) -> full -> Bool #
True if all items satisfy the function
The maximum value of the list
The minimum value of the list
replicate :: Int -> item -> full #
Generate a structure with the specified length with every element
set to the item passed in. See also genericReplicate
Takes the first n elements of the list. See also genericTake
.
Drops the first n elements of the list. See also genericDrop
splitAt :: Int -> full -> (full, full) #
Equivalent to (
. See also take
n xs, drop
n xs)genericSplitAt
.
takeWhile :: (item -> Bool) -> full -> full #
Returns all elements at start of list that satisfy the function.
dropWhile :: (item -> Bool) -> full -> full #
Drops all elements from the start of the list that satisfy the function.
dropWhileEnd :: (item -> Bool) -> full -> full #
Drops all elements from the end of the list that satisfy the function.
span :: (item -> Bool) -> full -> (full, full) #
break :: (item -> Bool) -> full -> (full, full) #
group :: (ListLike full' full, Eq item) => full -> full' #
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
.
inits :: ListLike full' full => full -> full' #
All initial segments of the list, shortest first
tails :: ListLike full' full => full -> full' #
All final segnemts, longest first
isPrefixOf :: full -> full -> Bool #
True when the first list is at the beginning of the second.
isSuffixOf :: full -> full -> Bool #
True when the first list is at the beginning of the second.
isInfixOf :: full -> full -> Bool #
True when the first list is wholly containted within the second
stripPrefix :: full -> full -> Maybe full #
Remove a prefix from a listlike if possible
stripSuffix :: full -> full -> Maybe full #
Remove a suffix from a listlike if possible
elem :: item -> full -> Bool #
True if the item occurs in the list
notElem :: item -> full -> Bool #
True if the item does not occur in the list
find :: (item -> Bool) -> full -> Maybe item #
Take a function and return the first matching element, or Nothing if there is no such element.
filter :: (item -> Bool) -> full -> full #
Returns only the elements that satisfy the function.
partition :: (item -> Bool) -> full -> (full, full) #
Returns the lists that do and do not satisfy the function.
Same as (
filter
p xs, filter
(not
. p) xs)
index :: full -> Int -> item #
The element at 0-based index i. Raises an exception if i is out of bounds. Like (!!) for lists.
elemIndex :: item -> full -> Maybe Int #
Returns the index of the element, if it exists.
elemIndices :: (Eq item, ListLike result Int) => item -> full -> result #
Returns the indices of the matching elements. See also
findIndices
findIndex :: (item -> Bool) -> full -> Maybe Int #
Take a function and return the index of the first matching element, or Nothing if no element matches
findIndices :: ListLike result Int => (item -> Bool) -> full -> result #
Returns the indices of all elements satisfying the function
sequence :: (Applicative m, ListLike fullinp (m item)) => fullinp -> m full #
Evaluate each action in the sequence and collect the results
mapM :: (Applicative m, ListLike full' item') => (item -> m item') -> full -> m full' #
rigidMapM :: Monad m => (item -> m item) -> full -> m full #
Like mapM
, but without the possibility of changing the type
of the item. This can have performance benefits with some types.
Removes duplicate elements from the list. See also nubBy
delete :: item -> full -> full #
Removes the first instance of the element from the list.
See also deleteBy
deleteFirsts :: full -> full -> full #
List difference. Removes from the first list the first instance
of each element of the second list. See (\\)
and deleteFirstsBy
union :: full -> full -> full #
List union: the set of elements that occur in either list.
Duplicate elements in the first list will remain duplicate.
See also unionBy
.
intersect :: full -> full -> full #
List intersection: the set of elements that occur in both lists.
See also intersectBy
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
.
insert :: item -> full -> full #
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
.
Converts the structure to a list. This is logically equivolent
to fromListLike
, but may have a more optimized implementation.
These two functions are now retired in favor of the methods of
IsList, but they are retained here because some instances still
use this implementation.
Generates the structure from a list.
fromListLike :: ListLike full' item => full -> full' #
Converts one ListLike to another. See also toList'
.
Default implementation is fromListLike = map id
nubBy :: (item -> item -> Bool) -> full -> full #
Generic version of nub
deleteBy :: (item -> item -> Bool) -> item -> full -> full #
Generic version of deleteBy
deleteFirstsBy :: (item -> item -> Bool) -> full -> full -> full #
Generic version of deleteFirsts
unionBy :: (item -> item -> Bool) -> full -> full -> full #
Generic version of union
intersectBy :: (item -> item -> Bool) -> full -> full -> full #
Generic version of intersect
groupBy :: (ListLike full' full, Eq item) => (item -> item -> Bool) -> full -> full' #
Generic version of group
.
sortBy :: (item -> item -> Ordering) -> full -> full #
Sort function taking a custom comparison function
insertBy :: (item -> item -> Ordering) -> item -> full -> full #
Like insert
, but with a custom comparison function
genericLength :: Num a => full -> a #
Length of the list
genericTake :: Integral a => a -> full -> full #
Generic version of take
genericDrop :: Integral a => a -> full -> full #
Generic version of drop
genericSplitAt :: Integral a => a -> full -> (full, full) #
Generic version of splitAt
genericReplicate :: Integral a => a -> item -> full #
Generic version of replicate
Instances
toList :: IsList l => l -> [Item l] #
The toList
function extracts a list of Item l
from the structure l
.
It should satisfy fromList . toList = id.
fromList :: IsList l => [Item l] -> l #
The fromList
function constructs the structure l
from the given
list of Item l
class ListLike full item => InfiniteListLike full item | full -> item where #
An extension to ListLike
for those data types that are capable
of dealing with infinite lists. Some ListLike
functions are capable
of working with finite or infinite lists. The functions here require
infinite list capability in order to work at all.
Minimal complete definition
Nothing
Methods
iterate :: (item -> item) -> item -> full #
An infinite list of repeated calls of the function to args
An infinite list where each element is the same
Converts a finite list into a circular one
Instances
InfiniteListLike (FMList a) a # | |
InfiniteListLike [a] a # | |
zip :: (ListLike full item, ListLike fullb itemb, ListLike result (item, itemb)) => full -> fullb -> result #
Takes two lists and returns a list of corresponding pairs.