fmlist 0.6 → 0.7
raw patch · 2 files changed
+32/−25 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Data.FMList: foldMapA :: (Foldable t, Applicative f, Monoid m) => (a -> f m) -> t a -> f m
Files
- Data/FMList.hs +31/−24
- fmlist.cabal +1/−1
Data/FMList.hs view
@@ -59,6 +59,8 @@ -- * Folding , toList , flatten+ , foldMapA+ , filter , take , drop@@ -78,13 +80,12 @@ import Prelude ( (.), ($), ($!), flip, const, id, error- , Maybe(..), maybe , Either(..), either , Bool(..), (&&) , Ord(..), Num(..), Int , Show(..), String, (++) )-import qualified Data.List as List+import Data.Maybe (Maybe(..), maybe, fromMaybe, isNothing) import Data.Monoid import Data.Foldable import Data.Traversable@@ -127,24 +128,24 @@ one :: a -> FMList a one x = FM ($ x) -(#) :: FMList a -> FMList a -> FMList a-FM l # FM r = FM (l `mappend` r)+(><) :: FMList a -> FMList a -> FMList a+FM l >< FM r = FM (l `mappend` r) -- exported constructors singleton :: a -> FMList a singleton = one cons :: a -> FMList a -> FMList a-cons x l = one x # l+cons x l = one x >< l snoc :: FMList a -> a -> FMList a-snoc l x = l # one x+snoc l x = l >< one x pair :: a -> a -> FMList a-pair l r = one l # one r+pair l r = one l >< one r append :: FMList a -> FMList a -> FMList a-append = (#)+append = (><) @@ -159,7 +160,7 @@ mhead l = getFirst (unFM l (First . Just)) null :: FMList a -> Bool-null = maybe True (const False) . mhead+null = isNothing . mhead length :: FMList a -> Int length = genericLength@@ -196,13 +197,13 @@ transformCS t s0 l = FM $ \f -> foldr (\e r -> t f e (\a -> mappend a . r)) mempty l s0 take :: (Ord n, Num n) => n -> FMList a -> FMList a-take n = transformCS (\f e c i -> if i > 0 then c (f e) (i-1) else mempty) n+take = transformCS (\f e c i -> if i > 0 then c (f e) (i-1) else mempty) takeWhile :: (a -> Bool) -> FMList a -> FMList a takeWhile p = transformCS (\f e c _ -> if p e then c (f e) True else mempty) True drop :: (Ord n, Num n) => n -> FMList a -> FMList a-drop n = transformCS (\f e c i -> if i <= 0 then c (f e) 0 else c mempty (i-1)) n+drop = transformCS (\f e c i -> if i > 0 then c mempty (i-1) else c (f e) 0) dropWhile :: (a -> Bool) -> FMList a -> FMList a dropWhile p = transformCS (\f e c ok -> if ok && p e then c mempty True else c (f e) False) True@@ -248,12 +249,27 @@ unfold g = transform (\f -> either (foldMap f . unfold g) f) . g +newtype WrapApp f m = WrapApp { unWrapApp :: f m }+instance (Applicative f, Monoid m) => Monoid (WrapApp f m) where+ mempty = WrapApp $ pure mempty+ mappend (WrapApp a) (WrapApp b) = WrapApp $ mappend <$> a <*> b++-- | Map each element of a structure to an action, evaluate these actions from left to right, +-- and concat the monoid results.+foldMapA :: (Foldable t, Applicative f, Monoid m) => (a -> f m) -> t a -> f m+foldMapA f = unWrapApp . foldMap (WrapApp . f)+++ instance Functor FMList where fmap g = transform (. g) instance Foldable FMList where foldMap = flip unFM +instance Traversable FMList where+ traverse f = foldMapA (fmap one . f)+ instance Monad FMList where return = one m >>= g = transform (\f -> foldMap f . g) m@@ -264,28 +280,19 @@ instance Monoid (FMList a) where mempty = nil- mappend = (#)+ mappend = (><) instance MonadPlus FMList where mzero = nil- mplus = (#)+ mplus = (><) instance Alternative FMList where empty = nil- (<|>) = (#)+ (<|>) = (><) instance Show a => Show (FMList a) where show l = "fromList " ++ (show $! toList l) -newtype WrapApp f m = WrapApp { unWrapApp :: f m }-instance (Applicative f, Monoid m) => Monoid (WrapApp f m) where- mempty = WrapApp $ pure mempty- mappend (WrapApp a) (WrapApp b) = WrapApp $ mappend <$> a <*> b--instance Traversable FMList where- traverse f = unWrapApp . foldMap one_f where one_f x = WrapApp $ one <$> f x-- fromJustOrError :: Maybe a -> String -> a-fromJustOrError ma e = maybe (error e) id ma+fromJustOrError ma e = fromMaybe (error e) ma
fmlist.cabal view
@@ -1,5 +1,5 @@ name: fmlist-version: 0.6+version: 0.7 synopsis: FoldMap lists description: FoldMap lists are lists represented by their foldMap function.