packages feed

ListLike 4.5.1 → 4.6

raw patch · 7 files changed

+40/−2 lines, 7 filesdep +semigroupsdep ~basedep ~containersPVP ok

version bump matches the API change (PVP)

Dependencies added: semigroups

Dependency ranges changed: base, containers

API changes (from Hackage documentation)

- Data.ListLike.Instances: instance Data.ListLike.Base.ListLike (Data.Sequence.Seq a) a
- Data.ListLike.Instances: instance Data.ListLike.FoldableLL.FoldableLL (Data.Sequence.Seq a) a
- Data.ListLike.Instances: instance Data.ListLike.IO.ListLikeIO (Data.Sequence.Seq GHC.Types.Char) GHC.Types.Char
- Data.ListLike.Instances: instance Data.ListLike.String.StringLike (Data.Sequence.Seq GHC.Types.Char)
+ Data.ListLike.CharString: instance Data.Semigroup.Semigroup Data.ListLike.CharString.CharString
+ Data.ListLike.CharString: instance Data.Semigroup.Semigroup Data.ListLike.CharString.CharStringLazy
+ Data.ListLike.Chars: instance Data.Semigroup.Semigroup Data.ListLike.Chars.Chars
+ Data.ListLike.Instances: instance (GHC.Real.Integral i, GHC.Arr.Ix i) => Data.Semigroup.Semigroup (GHC.Arr.Array i e)
+ Data.ListLike.Instances: instance Data.ListLike.Base.ListLike (Data.Sequence.Internal.Seq a) a
+ Data.ListLike.Instances: instance Data.ListLike.FoldableLL.FoldableLL (Data.Sequence.Internal.Seq a) a
+ Data.ListLike.Instances: instance Data.ListLike.IO.ListLikeIO (Data.Sequence.Internal.Seq GHC.Types.Char) GHC.Types.Char
+ Data.ListLike.Instances: instance Data.ListLike.String.StringLike (Data.Sequence.Internal.Seq GHC.Types.Char)
+ Data.ListLike.UTF8: instance Data.Semigroup.Semigroup (Data.String.UTF8.UTF8 Data.ByteString.Internal.ByteString)
+ Data.ListLike.UTF8: instance Data.Semigroup.Semigroup (Data.String.UTF8.UTF8 Data.ByteString.Lazy.Internal.ByteString)
+ Data.ListLike.Utils: intercalate :: (ListLike a item, ListLike b a) => a -> b -> a
- Data.ListLike: class FoldableLL full item | full -> item where foldl' f a xs = foldr f' id xs a where f' x k z = k $! f z x foldl1 f xs = fromMaybe (error "fold1: empty structure") (foldl mf Nothing xs) where mf Nothing y = Just y mf (Just x) y = Just (f x y) foldr' f a xs = foldl f' id xs a where f' k x z = k $! f x z foldr1 f xs = fromMaybe (error "foldr1: empty structure") (foldr mf Nothing xs) where mf x Nothing = Just x mf x (Just y) = Just (f x y)
+ Data.ListLike: class FoldableLL full item | full -> item
- Data.ListLike: class (ListLike full item) => InfiniteListLike full item | full -> item where iterate f x = cons x (iterate f (f x)) repeat x = xs where xs = cons x xs cycle xs | null xs = error "ListLike.cycle: empty list" | otherwise = xs' where xs' = append xs xs'
+ Data.ListLike: class (ListLike full item) => InfiniteListLike full item | full -> item
- Data.ListLike: class (FoldableLL full item, Monoid full) => ListLike full item | full -> item where empty = mempty cons item l = append (singleton item) l snoc l item = append l (singleton item) append = mappend head = maybe (error "head") fst . uncons uncons x = if null x then Nothing else Just (head x, tail x) last l = case genericLength l of { (0 :: Integer) -> error "Called last on empty list" 1 -> head l _ -> last (tail l) } tail = maybe (error "tail") snd . uncons init l | null l = error "init: empty list" | null xs = empty | otherwise = cons (head l) (init xs) where xs = tail l null x = genericLength x == (0 :: Integer) length = genericLength map func inp | null inp = empty | otherwise = cons (func (head inp)) (map func (tail inp)) rigidMap = map reverse l = rev l empty where rev rl a | null rl = a | otherwise = rev (tail rl) (cons (head rl) a) intersperse sep l | null l = empty | null xs = singleton x | otherwise = cons x (cons sep (intersperse sep xs)) where x = head l xs = tail l concat = fold concatMap = foldMap rigidConcatMap = concatMap any p = getAny . foldMap (Any . p) all p = getAll . foldMap (All . p) maximum = foldr1 max minimum = foldr1 min replicate = genericReplicate take = genericTake drop = genericDrop splitAt = genericSplitAt takeWhile func l | null l = empty | func x = cons x (takeWhile func (tail l)) | otherwise = empty where x = head l dropWhile func l | null l = empty | func (head l) = dropWhile func (tail l) | otherwise = l dropWhileEnd func = foldr (\ x xs -> if func x && null xs then empty else cons x xs) empty span func l | null l = (empty, empty) | func x = (cons x ys, zs) | otherwise = (empty, l) where (ys, zs) = span func (tail l) x = head l break p = span (not . p) group = groupBy (==) inits l | null l = singleton empty | otherwise = append (singleton empty) (map (cons (head l)) theinits) where theinits = asTypeOf (inits (tail l)) [l] tails l | null l = singleton empty | otherwise = cons l (tails (tail l)) isPrefixOf needle haystack | null needle = True | null haystack = False | otherwise = (head needle) == (head haystack) && isPrefixOf (tail needle) (tail haystack) isSuffixOf needle haystack = isPrefixOf (reverse needle) (reverse haystack) isInfixOf needle haystack = any (isPrefixOf needle) thetails where thetails = asTypeOf (tails haystack) [haystack] stripPrefix xs ys = if xs `isPrefixOf` ys then Just $ drop (length xs) ys else Nothing stripSuffix xs ys = if xs `isSuffixOf` ys then Just $ take (length ys - length xs) ys else Nothing elem i = any (== i) notElem i = all (/= i) find f l = case findIndex f l of { Nothing -> Nothing Just x -> Just (index l x) } filter func l | null l = empty | func (head l) = cons (head l) (filter func (tail l)) | otherwise = filter func (tail l) partition p xs = (filter p xs, filter (not . p) xs) index l i | null l = error "index: index not found" | i < 0 = error "index: index must be >= 0" | i == 0 = head l | otherwise = index (tail l) (i - 1) elemIndex e l = findIndex (== e) l elemIndices i l = findIndices (== i) l findIndex f = listToMaybe . findIndices f findIndices p xs = map snd $ filter (p . fst) $ thezips where thezips = asTypeOf (zip xs [0 .. ]) [(head xs, 0 :: Int)] sequence l = foldr func (return empty) l where func litem results = do { x <- litem; xs <- results; return (cons x xs) } mapM func l = sequence mapresult where mapresult = asTypeOf (map func l) [] rigidMapM = mapM nub = nubBy (==) delete = deleteBy (==) deleteFirsts = foldl (flip delete) union = unionBy (==) intersect = intersectBy (==) sort = sortBy compare insert = insertBy compare toList = fromListLike fromList [] = empty fromList (x : xs) = cons x (fromList xs) fromListLike = map id nubBy f l = nubBy' l (empty :: full) where nubBy' ys xs | null ys = empty | any (f (head ys)) xs = nubBy' (tail ys) xs | otherwise = let y = head ys in cons y (nubBy' (tail ys) (cons y xs)) deleteBy func i l | null l = empty | otherwise = if func i (head l) then tail l else cons (head l) (deleteBy func i (tail l)) deleteFirstsBy func = foldl (flip (deleteBy func)) unionBy func x y = append x $ foldl (flip (deleteBy func)) (nubBy func y) x intersectBy func xs ys = filter (\ x -> any (func x) ys) xs groupBy eq l | null l = empty | otherwise = cons (cons x ys) (groupBy eq zs) where (ys, zs) = span (eq x) xs x = head l xs = tail l sortBy cmp = foldr (insertBy cmp) empty insertBy cmp x ys | null ys = singleton x | otherwise = case cmp x (head ys) of { GT -> cons (head ys) (insertBy cmp x (tail ys)) _ -> cons x ys } genericLength l = calclen 0 l where calclen !accum cl = if null cl then accum else calclen (accum + 1) (tail cl) genericTake n l | n <= 0 = empty | null l = empty | otherwise = cons (head l) (genericTake (n - 1) (tail l)) genericDrop n l | n <= 0 = l | null l = l | otherwise = genericDrop (n - 1) (tail l) genericSplitAt n l = (genericTake n l, genericDrop n l) genericReplicate count x | count <= 0 = empty | otherwise = map (\ _ -> x) [1 .. count]
+ Data.ListLike: class (FoldableLL full item, Monoid full) => ListLike full item | full -> item
- Data.ListLike: class (ListLike full item) => ListLikeIO full item | full -> item where hPutStrLn fp x = do { hPutStr fp x; hPutStrLn fp "" } getLine = hGetLine stdin getContents = hGetContents stdin putStr = hPutStr stdout putStrLn = hPutStrLn stdout interact func = do { c <- getContents; putStr (func c) } readFile fn = do { fp <- openFile fn ReadMode; hGetContents fp } writeFile fn x = do { fp <- openFile fn WriteMode; hPutStr fp x; hClose fp } appendFile fn x = do { fp <- openFile fn AppendMode; hPutStr fp x; hClose fp }
+ Data.ListLike: class (ListLike full item) => ListLikeIO full item | full -> item
- Data.ListLike: class StringLike s where lines = myLines words = myWords unlines = myUnlines unwords = myUnwords
+ Data.ListLike: class StringLike s
- Data.ListLike: concatMap :: (ListLike full item, ListLike full' item') => (item -> full') -> full -> full'
+ Data.ListLike: concatMap :: (ListLike full item, (ListLike full' item')) => (item -> full') -> full -> full'
- Data.ListLike: findIndices :: (ListLike full item, ListLike result Int) => (item -> Bool) -> full -> result
+ Data.ListLike: findIndices :: (ListLike full item, (ListLike result Int)) => (item -> Bool) -> full -> result
- Data.ListLike: inits :: (ListLike full item, ListLike full' full) => full -> full'
+ Data.ListLike: inits :: (ListLike full item, (ListLike full' full)) => full -> full'
- Data.ListLike: lines :: (StringLike s, ListLike full s) => s -> full
+ Data.ListLike: lines :: (StringLike s, (ListLike full s)) => s -> full
- Data.ListLike.Base: class (ListLike full item) => InfiniteListLike full item | full -> item where iterate f x = cons x (iterate f (f x)) repeat x = xs where xs = cons x xs cycle xs | null xs = error "ListLike.cycle: empty list" | otherwise = xs' where xs' = append xs xs'
+ Data.ListLike.Base: class (ListLike full item) => InfiniteListLike full item | full -> item
- Data.ListLike.Base: class (FoldableLL full item, Monoid full) => ListLike full item | full -> item where empty = mempty cons item l = append (singleton item) l snoc l item = append l (singleton item) append = mappend head = maybe (error "head") fst . uncons uncons x = if null x then Nothing else Just (head x, tail x) last l = case genericLength l of { (0 :: Integer) -> error "Called last on empty list" 1 -> head l _ -> last (tail l) } tail = maybe (error "tail") snd . uncons init l | null l = error "init: empty list" | null xs = empty | otherwise = cons (head l) (init xs) where xs = tail l null x = genericLength x == (0 :: Integer) length = genericLength map func inp | null inp = empty | otherwise = cons (func (head inp)) (map func (tail inp)) rigidMap = map reverse l = rev l empty where rev rl a | null rl = a | otherwise = rev (tail rl) (cons (head rl) a) intersperse sep l | null l = empty | null xs = singleton x | otherwise = cons x (cons sep (intersperse sep xs)) where x = head l xs = tail l concat = fold concatMap = foldMap rigidConcatMap = concatMap any p = getAny . foldMap (Any . p) all p = getAll . foldMap (All . p) maximum = foldr1 max minimum = foldr1 min replicate = genericReplicate take = genericTake drop = genericDrop splitAt = genericSplitAt takeWhile func l | null l = empty | func x = cons x (takeWhile func (tail l)) | otherwise = empty where x = head l dropWhile func l | null l = empty | func (head l) = dropWhile func (tail l) | otherwise = l dropWhileEnd func = foldr (\ x xs -> if func x && null xs then empty else cons x xs) empty span func l | null l = (empty, empty) | func x = (cons x ys, zs) | otherwise = (empty, l) where (ys, zs) = span func (tail l) x = head l break p = span (not . p) group = groupBy (==) inits l | null l = singleton empty | otherwise = append (singleton empty) (map (cons (head l)) theinits) where theinits = asTypeOf (inits (tail l)) [l] tails l | null l = singleton empty | otherwise = cons l (tails (tail l)) isPrefixOf needle haystack | null needle = True | null haystack = False | otherwise = (head needle) == (head haystack) && isPrefixOf (tail needle) (tail haystack) isSuffixOf needle haystack = isPrefixOf (reverse needle) (reverse haystack) isInfixOf needle haystack = any (isPrefixOf needle) thetails where thetails = asTypeOf (tails haystack) [haystack] stripPrefix xs ys = if xs `isPrefixOf` ys then Just $ drop (length xs) ys else Nothing stripSuffix xs ys = if xs `isSuffixOf` ys then Just $ take (length ys - length xs) ys else Nothing elem i = any (== i) notElem i = all (/= i) find f l = case findIndex f l of { Nothing -> Nothing Just x -> Just (index l x) } filter func l | null l = empty | func (head l) = cons (head l) (filter func (tail l)) | otherwise = filter func (tail l) partition p xs = (filter p xs, filter (not . p) xs) index l i | null l = error "index: index not found" | i < 0 = error "index: index must be >= 0" | i == 0 = head l | otherwise = index (tail l) (i - 1) elemIndex e l = findIndex (== e) l elemIndices i l = findIndices (== i) l findIndex f = listToMaybe . findIndices f findIndices p xs = map snd $ filter (p . fst) $ thezips where thezips = asTypeOf (zip xs [0 .. ]) [(head xs, 0 :: Int)] sequence l = foldr func (return empty) l where func litem results = do { x <- litem; xs <- results; return (cons x xs) } mapM func l = sequence mapresult where mapresult = asTypeOf (map func l) [] rigidMapM = mapM nub = nubBy (==) delete = deleteBy (==) deleteFirsts = foldl (flip delete) union = unionBy (==) intersect = intersectBy (==) sort = sortBy compare insert = insertBy compare toList = fromListLike fromList [] = empty fromList (x : xs) = cons x (fromList xs) fromListLike = map id nubBy f l = nubBy' l (empty :: full) where nubBy' ys xs | null ys = empty | any (f (head ys)) xs = nubBy' (tail ys) xs | otherwise = let y = head ys in cons y (nubBy' (tail ys) (cons y xs)) deleteBy func i l | null l = empty | otherwise = if func i (head l) then tail l else cons (head l) (deleteBy func i (tail l)) deleteFirstsBy func = foldl (flip (deleteBy func)) unionBy func x y = append x $ foldl (flip (deleteBy func)) (nubBy func y) x intersectBy func xs ys = filter (\ x -> any (func x) ys) xs groupBy eq l | null l = empty | otherwise = cons (cons x ys) (groupBy eq zs) where (ys, zs) = span (eq x) xs x = head l xs = tail l sortBy cmp = foldr (insertBy cmp) empty insertBy cmp x ys | null ys = singleton x | otherwise = case cmp x (head ys) of { GT -> cons (head ys) (insertBy cmp x (tail ys)) _ -> cons x ys } genericLength l = calclen 0 l where calclen !accum cl = if null cl then accum else calclen (accum + 1) (tail cl) genericTake n l | n <= 0 = empty | null l = empty | otherwise = cons (head l) (genericTake (n - 1) (tail l)) genericDrop n l | n <= 0 = l | null l = l | otherwise = genericDrop (n - 1) (tail l) genericSplitAt n l = (genericTake n l, genericDrop n l) genericReplicate count x | count <= 0 = empty | otherwise = map (\ _ -> x) [1 .. count]
+ Data.ListLike.Base: class (FoldableLL full item, Monoid full) => ListLike full item | full -> item
- Data.ListLike.Base: concatMap :: (ListLike full item, ListLike full' item') => (item -> full') -> full -> full'
+ Data.ListLike.Base: concatMap :: (ListLike full item, (ListLike full' item')) => (item -> full') -> full -> full'
- Data.ListLike.Base: findIndices :: (ListLike full item, ListLike result Int) => (item -> Bool) -> full -> result
+ Data.ListLike.Base: findIndices :: (ListLike full item, (ListLike result Int)) => (item -> Bool) -> full -> result
- Data.ListLike.Base: inits :: (ListLike full item, ListLike full' full) => full -> full'
+ Data.ListLike.Base: inits :: (ListLike full item, (ListLike full' full)) => full -> full'
- Data.ListLike.FoldableLL: class FoldableLL full item | full -> item where foldl' f a xs = foldr f' id xs a where f' x k z = k $! f z x foldl1 f xs = fromMaybe (error "fold1: empty structure") (foldl mf Nothing xs) where mf Nothing y = Just y mf (Just x) y = Just (f x y) foldr' f a xs = foldl f' id xs a where f' k x z = k $! f x z foldr1 f xs = fromMaybe (error "foldr1: empty structure") (foldr mf Nothing xs) where mf x Nothing = Just x mf x (Just y) = Just (f x y)
+ Data.ListLike.FoldableLL: class FoldableLL full item | full -> item
- Data.ListLike.IO: class (ListLike full item) => ListLikeIO full item | full -> item where hPutStrLn fp x = do { hPutStr fp x; hPutStrLn fp "" } getLine = hGetLine stdin getContents = hGetContents stdin putStr = hPutStr stdout putStrLn = hPutStrLn stdout interact func = do { c <- getContents; putStr (func c) } readFile fn = do { fp <- openFile fn ReadMode; hGetContents fp } writeFile fn x = do { fp <- openFile fn WriteMode; hPutStr fp x; hClose fp } appendFile fn x = do { fp <- openFile fn AppendMode; hPutStr fp x; hClose fp }
+ Data.ListLike.IO: class (ListLike full item) => ListLikeIO full item | full -> item
- Data.ListLike.String: class StringLike s where lines = myLines words = myWords unlines = myUnlines unwords = myUnwords
+ Data.ListLike.String: class StringLike s
- Data.ListLike.String: lines :: (StringLike s, ListLike full s) => s -> full
+ Data.ListLike.String: lines :: (StringLike s, (ListLike full s)) => s -> full

Files

ListLike.cabal view
@@ -1,5 +1,5 @@ Name: ListLike-Version: 4.5.1+Version: 4.6 License: BSD3 Maintainer: John Lato <jwlato@gmail.com> Author: John Goerzen@@ -60,6 +60,9 @@                 ,utf8-string                 ,deepseq +  If !impl(ghc >= 8.4)+    Build-Depends: semigroups >= 0.16 && < 0.19+ Test-suite listlike-tests   GHC-Options: -O2   Hs-source-dirs: testsrc@@ -80,6 +83,8 @@                   ,text                   ,vector                   ,utf8-string+  If !impl(ghc >= 8.4)+    Build-Depends: semigroups >= 0.16 && < 0.19  source-repository head   type:     git
src/Data/ListLike/CharString.hs view
@@ -49,6 +49,7 @@ import           Data.ListLike.FoldableLL import           Data.Int import           Data.Monoid+import           Data.Semigroup (Semigroup(..)) import qualified Data.ByteString.Char8 as BS import qualified Data.ByteString.Lazy.Char8 as BSL import qualified System.IO as IO@@ -63,6 +64,9 @@ newtype CharString = CS { unCS :: BS.ByteString }   deriving (Read, Show, Eq, Ord) +instance Semigroup CharString where+  (<>) = mappend+ instance Monoid CharString where   mempty = CS mempty   mappend l r = CS $ mappend (unCS l) (unCS r)@@ -177,6 +181,9 @@ --   this allows for ListLike instances with Char elements. newtype CharStringLazy = CSL { unCSL :: BSL.ByteString }   deriving (Read, Show, Eq, Ord)++instance Semigroup CharStringLazy where+  (<>) = mappend  instance Monoid CharStringLazy where   mempty = CSL mempty
src/Data/ListLike/Chars.hs view
@@ -14,6 +14,7 @@ import           Control.DeepSeq import           Control.Monad import           Data.String as String (IsString(fromString))+import           Data.Semigroup (Semigroup(..)) import qualified Data.Text.Lazy as T import qualified Data.Text.Lazy.IO as TI import qualified Data.Text.Lazy.Builder as Builder@@ -33,6 +34,9 @@ builder (B x) = x builder (T s) = Builder.fromLazyText s {-# INLINE builder #-}++instance Semigroup Chars where+  (<>) = mappend  instance Monoid Chars where     mempty = B mempty
src/Data/ListLike/Instances.hs view
@@ -52,6 +52,7 @@ import           Data.Int import           Data.Maybe (fromMaybe) import           Data.Monoid+import           Data.Semigroup (Semigroup(..)) import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as BSC import qualified Data.Foldable as F@@ -338,6 +339,9 @@     foldr = F.foldr     foldr1 = F.foldr1     foldr' = F.foldr'++instance (Integral i, Ix i) => Semigroup (A.Array i e) where+  (<>) = mappend  instance (Integral i, Ix i) => Monoid (A.Array i e) where     mempty = A.listArray (0, -1) []
src/Data/ListLike/UTF8.hs view
@@ -29,6 +29,7 @@ import Data.ListLike.String (StringLike(..)) import Data.Maybe (fromMaybe) import Data.Monoid (Monoid(..))+import Data.Semigroup (Semigroup(..)) import Data.String (IsString(fromString)) import Data.String.UTF8 (UTF8, UTF8Bytes) import qualified Data.String.UTF8 as UTF8@@ -144,6 +145,9 @@     toString = UTF8.toString     fromString = UTF8.fromString +instance Semigroup (UTF8 BS.ByteString) where+  (<>) = mappend+ instance Monoid (UTF8 BS.ByteString) where     mempty = UTF8.fromString []     mappend a b = UTF8.fromRep (mappend (UTF8.toRep a) (UTF8.toRep b))@@ -250,6 +254,9 @@     -- readFile = BSL.readFile     -- writeFile = BSL.writeFile     -- appendFile = BSL.appendFile++instance Semigroup (UTF8 BSL.ByteString) where+  (<>) = mappend  instance StringLike (UTF8 BSL.ByteString) where     toString = UTF8.toString
src/Data/ListLike/Utils.hs view
@@ -26,7 +26,8 @@ -}  module Data.ListLike.Utils-    (and, or, sum, product, zip, zipWith, unzip, sequence_, toMonadPlus, list+    (and, or, sum, product, zip, zipWith, unzip, sequence_, toMonadPlus, list,+     intercalate     ) where import Prelude hiding (length, head, last, null, tail, map, filter, concat,                         any, lookup, init, all, foldl, foldr, foldl1, foldr1,@@ -74,3 +75,10 @@ -- | List-like destructor (like Data.Maybe.maybe) list :: ListLike full a => b -> (a -> full -> b) -> full -> b list d f = maybe d (uncurry f) . toMonadPlus++-- | 'intercalate' @xs xss@ is equivalent to @('concat' ('intersperse'+-- xs xss))@.  It inserts the list @xs@ in between the lists in @xss@+-- and concatenates the result.+intercalate :: (ListLike a item, ListLike b a)+            => a -> b -> a+intercalate x = concat . intersperse x
testsrc/TestInfrastructure.hs view
@@ -29,6 +29,7 @@ import qualified Data.Array as A import qualified Data.DList as DL import qualified Data.FMList as FM+import qualified Data.Semigroup as Sem import qualified Data.Sequence as S import qualified Data.Foldable as F import qualified Data.Text as T@@ -265,6 +266,8 @@     foldr1 f (MyList x) = foldr1 f x     foldl1 f (MyList x) = foldl1 f x +instance Sem.Semigroup (MyList a) where+  (<>) = mappend instance Monoid (MyList a) where     mempty = MyList []     mappend (MyList x) (MyList y) = MyList (x ++ y)