{- arch-tag: List utilities main file
Copyright (C) 2004-2006 John Goerzen <jgoerzen@complete.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-}{- |
Module : Data.List.Utils
Copyright : Copyright (C) 2004-2006 John Goerzen
License : GNU GPL, version 2 or above
Maintainer : John Goerzen <jgoerzen@complete.org>
Stability : provisional
Portability: portable
This module provides various helpful utilities for dealing with lists.
Written by John Goerzen, jgoerzen\@complete.org
-}moduleData.List.Utils(-- * Mergingmerge,mergeBy,-- * Testsstartswith,endswith,contains,hasAny,-- * Association List Utilities{- | These functions are designed to augment the
association list functions in "Data.List" and
provide an interface similar to "Data.FiniteMap" or
"Data.Map"
for association lists. -}addToAL,delFromAL,flipAL,keysAL,valuesAL,hasKeyAL,-- ** Association List ConversionsstrFromAL,strToAL,-- * Conversionssplit,join,replace,genericJoin,takeWhileList,dropWhileList,spanList,breakList,-- ** Advanced ConversionsWholeFunc(..),wholeMap,fixedWidth,-- * Fixed-Width and State Monad Utilitiesgrab,-- * MiscellaneouscountElem,elemRIndex,alwaysElemRIndex,seqList,subIndex,uniq-- -- * Sub-List Selection-- sub,)whereimportData.List(intersperse,concat,isPrefixOf,isSuffixOf,elemIndices,elemIndex,elemIndices,tails,find,findIndex,isInfixOf,nub)importControl.Monad.State(State,get,put)importData.Maybe(isJust){- | Merge two sorted lists into a single, sorted whole.
Example:
> 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::(Orda)=>[a]->[a]->[a]merge=mergeBy(compare){- | 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
-}mergeBy::(a->a->Ordering)->[a]->[a]->[a]mergeBycmp[]ys=ysmergeBycmpxs[]=xsmergeBycmp(allx@(x:xs))(ally@(y:ys))-- Ordering derives Eq, Ord, so the comparison below is valid.-- Explanation left as an exercise for the reader.-- Someone please put this code out of its misery.|(x`cmp`y)<=EQ=x:mergeBycmpxsally|otherwise=y:mergeBycmpallxys{- | Returns true if the given list starts with the specified elements;
false otherwise. (This is an alias for "Data.List.isPrefixOf".)
Example:
> startswith "He" "Hello" -> True
-}startswith::Eqa=>[a]->[a]->Boolstartswith=isPrefixOf{- | Returns true if the given list ends with the specified elements;
false otherwise. (This is an alias for "Data.List.isSuffixOf".)
Example:
> endswith "lo" "Hello" -> True
-}endswith::Eqa=>[a]->[a]->Boolendswith=isSuffixOf{- | Returns true if the given list contains any of the elements in the search
list. -}hasAny::Eqa=>[a]-- ^ List of elements to look for->[a]-- ^ List to search->Bool-- ^ ResulthasAny[]_=False-- An empty search list: always falsehasAny_[]=False-- An empty list to scan: always falsehasAnysearch(x:xs)=ifx`elem`searchthenTrueelsehasAnysearchxs{- | Similar to Data.List.takeWhile, takes elements while the func is true.
The function is given the remainder of the list to examine. -}takeWhileList::([a]->Bool)->[a]->[a]takeWhileList_[]=[]takeWhileListfunclist@(x:xs)=iffunclistthenx:takeWhileListfuncxselse[]{- | Similar to Data.List.dropWhile, drops elements while the func is true.
The function is given the remainder of the list to examine. -}dropWhileList::([a]->Bool)->[a]->[a]dropWhileList_[]=[]dropWhileListfunclist@(x:xs)=iffunclistthendropWhileListfuncxselselist{- | 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)@
-}spanList::([a]->Bool)->[a]->([a],[a])spanList_[]=([],[])spanListfunclist@(x:xs)=iffunclistthen(x:ys,zs)else([],list)where(ys,zs)=spanListfuncxs{- | Similar to Data.List.break, but performs the test on the entire remaining
list instead of just one element.
-}breakList::([a]->Bool)->[a]->([a],[a])breakListfunc=spanList(not.func){- | Given a delimiter and a list (or string), split into components.
Example:
> split "," "foo,bar,,baz," -> ["foo", "bar", "", "baz", ""]
> split "ba" ",foo,bar,,baz," -> [",foo,","r,,","z,"]
-}split::Eqa=>[a]->[a]->[[a]]split_[]=[]splitdelimstr=let(firstline,remainder)=breakList(startswithdelim)strinfirstline:caseremainderof[]->[]x->ifx==delimthen[]:[]elsesplitdelim(drop(lengthdelim)x){- | Given a list and a replacement list, replaces each occurance of the search
list with the replacement list in the operation list.
Example:
>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
-}replace::Eqa=>[a]->[a]->[a]->[a]replaceoldnewl=joinnew.splitold$l{- | Given a delimiter and a list of items (or strings), join the items
by using the delimiter.
Example:
> join "|" ["foo", "bar", "baz"] -> "foo|bar|baz"
-}join::[a]->[[a]]->[a]joindeliml=concat(interspersedeliml){- | Like 'join', but works with a list of anything showable, converting
it to a String.
Examples:
> genericJoin ", " [1, 2, 3, 4] -> "1, 2, 3, 4"
> genericJoin "|" ["foo", "bar", "baz"] -> "\"foo\"|\"bar\"|\"baz\""
-}genericJoin::Showa=>String->[a]->StringgenericJoindeliml=joindelim(mapshowl){-# DEPRECATED contains "Use Data.List.isInfixOf, will be removed in MissingH 1.1.0" #-}{- | Returns true if the given parameter is a sublist of the given list;
false otherwise.
Example:
> contains "Haskell" "I really like Haskell." -> True
> contains "Haskell" "OCaml is great." -> False
This function was submitted to GHC and was applied as
'Data.List.isInfixOf'. This function therefore is deprecated and will
be removed in future versions.
-}contains::Eqa=>[a]->[a]->Boolcontains=isInfixOf-- above function submitted to GHC as Data.List.isInfixOf on 8/31/2006{- | Adds the specified (key, value) pair to the given list, removing any
existing pair with the same key already present. -}addToAL::Eqkey=>[(key,elt)]->key->elt->[(key,elt)]addToALlkeyvalue=(key,value):delFromALlkey{- | Removes all (key, value) pairs from the given list where the key
matches the given one. -}delFromAL::Eqkey=>[(key,a)]->key->[(key,a)]delFromALlkey=filter(\a->(fsta)/=key)l{- | Returns the keys that comprise the (key, value) pairs of the given AL.
Same as:
>map fst
-}keysAL::[(key,a)]->[key]keysAL=mapfst{- | Returns the values the comprise the (key, value) pairs of the given
AL.
Same as:
>map snd
-}valuesAL::[(a,value)]->[value]valuesAL=mapsnd{- | Indicates whether or not the given key is in the AL. -}hasKeyAL::Eqa=>a->[(a,b)]->BoolhasKeyALkeylist=elemkey(keysALlist){- | Flips an association list. Converts (key1, val), (key2, val) pairs
to (val, [key1, key2]). -}flipAL::(Eqkey,Eqval)=>[(key,val)]->[(val,[key])]flipALoldl=letworker::(Eqkey,Eqval)=>[(key,val)]->[(val,[key])]->[(val,[key])]worker[]accum=accumworker((k,v):xs)accum=caselookupvaccumofNothing->workerxs((v,[k]):accum)Justy->workerxs(addToALaccumv(k:y))inworkeroldl[]{- | 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. -}strFromAL::(Showa,Showb)=>[(a,b)]->StringstrFromALinp=letworker(key,val)=showkey++","++showvalinunlines.mapworker$inp{- | The inverse of 'strFromAL', this function reads a string and outputs the
appropriate association list.
Like 'strFromAL', this is designed to work with [(String, String)] association
lists but may also work with other objects with simple representations.
-}strToAL::(Reada,Readb)=>String->[(a,b)]strToALinp=letworkerline=casereadslineof[(key,remainder)]->caseremainderof',':valstr->(key,readvalstr)_->error"Data.List.Utils.strToAL: Parse error on value"_->error"Data.List.Utils.strToAL: Parse error on key"inmapworker(linesinp){- FIXME TODO: sub -}{- | Returns a count of the number of times the given element occured in the
given list. -}countElem::Eqa=>a->[a]->IntcountElemi=length.filter(i==){- | Returns the rightmost index of the given element in the
given list. -}elemRIndex::Eqa=>a->[a]->MaybeIntelemRIndexiteml=casereverse$elemIndicesitemlof[]->Nothing(x:_)->Justx{- | Like elemRIndex, but returns -1 if there is nothing
found. -}alwaysElemRIndex::Eqa=>a->[a]->IntalwaysElemRIndexitemlist=caseelemRIndexitemlistofNothing->-1Justx->x{- | Forces the evaluation of the entire list. -}seqList::[a]->[a]seqList[]=[]seqListlist@(x:xs)=seq(seqListxs)list---------------------------------------------------- Advanced Conversions--------------------------------------------------{- | The type used for functions for 'wholeMap'. See 'wholeMap' for details.
-}newtypeWholeFuncab=WholeFunc([a]->(WholeFuncab,[a],[b])){- | 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
value of 'wholeMap' is the concatenation of the output element lists from
all iterations.
Processing stops when the remaining input list is empty. An example
of a 'WholeFunc' is 'fixedWidth'. -}wholeMap::WholeFuncab->[a]->[b]wholeMap_[]=[]-- Empty input, empty output.wholeMap(WholeFuncfunc)inplist=let(nextfunc,nextlist,output)=funcinplistinoutput++wholeMapnextfuncnextlist{- | A parser designed to process fixed-width input fields. Use it with
'wholeMap'.
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.
Examples:
>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."]
-}fixedWidth::[Int]->WholeFunca[a]fixedWidthlen=WholeFunc(fixedWidthFunclen)where-- Empty input: Empty output, stopfixedWidthFunc_[]=((fixedWidth[]),[],[])-- Empty length: Stop here.fixedWidthFunc[]x=((fixedWidth[]),[],[x])-- Stuff to process: Do it.fixedWidthFunc(len:lenxs)input=(fixedWidthlenxs,next,[this])where(this,next)=splitAtleninput{- | Helps you pick out fixed-width components from a list.
Example:
>conv :: String -> (String,String)
>conv = runState $
> do f3 <- grab 3
> n2 <- grab 2
> return $ f3 ++ "," ++ n2
>
>main = print $ conv "TestIng"
Prints:
>("Tes,tI","ng")
-}grab::Int->State[a][a]grabcount=dog<-get(x,g')<-return$splitAtcountgputg'returnx{- | 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.
Examples:
>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
-}subIndex::Eqa=>[a]->[a]->MaybeIntsubIndexsubstrstr=findIndex(isPrefixOfsubstr)(tailsstr){- | 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
-}uniq::Eqa=>[a]->[a]uniq=nub----- same as--uniq (x:xs) = x : [y | y <- uniq xs, y /= x]