fmlist 0.4 → 0.5
raw patch · 2 files changed
+56/−30 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.FMList: unfoldl :: (b -> Maybe (b, a)) -> b -> FMList a
+ Data.FMList: pair :: a -> a -> FMList a
+ Data.FMList: unfold :: (Foldable t) => (b -> t (Either b a)) -> b -> FMList a
- Data.FMList: flatten :: FMList (FMList a) -> FMList a
+ Data.FMList: flatten :: (Foldable t) => FMList (t a) -> FMList a
Files
- Data/FMList.hs +55/−29
- fmlist.cabal +1/−1
Data/FMList.hs view
@@ -8,8 +8,23 @@ -- Stability : experimental -- Portability : portable ----- FoldMap lists: lists represented by their foldMap function.+-- FoldMap lists: lists represented by their 'foldMap' function. --+-- Examples:+--+-- > -- A right-infinite list+-- > c = 1 `cons` c+--+-- > -- A left-infinite list+-- > d = d `snoc` 2+--+-- > -- A middle-infinite list ??+-- > e = c `append` d+--+-- > *> head e+-- > 1+-- > *> last e+-- > 2 ----------------------------------------------------------------------------- {-# LANGUAGE RankNTypes #-} @@ -23,9 +38,9 @@ , singleton , cons , snoc+ , pair , append - , toList , fromList , fromFoldable @@ -41,6 +56,7 @@ , reverse -- * Folding+ , toList , flatten , filter , take@@ -54,14 +70,14 @@ -- * Unfolding , iterate , repeat+ , unfold , unfoldr- , unfoldl ) where import Prelude ( (.), ($), ($!), flip, const, id, error- , Maybe(..), maybe+ , Maybe(..), maybe, Either(..) , Bool(..), (||), not , Ord(..), Num(..), Int , Show(..), String, (++)@@ -74,26 +90,13 @@ import Control.Applicative --- | FMList is a foldMap function wrapped up in a newtype.--- Examples:------ > -- A right-infinite list--- > c = 1 `cons` c------ > -- A left-infinite list--- > d = d `snoc` 2------ > -- A middle-infinite list ??--- > e = c `append` d+-- | 'FMList' is a 'foldMap' function wrapped up in a newtype. ----- > *> head e--- > 1--- > *> last e--- > 2--- newtype FMList a = FM { unFM :: forall b . Monoid b => (a -> b) -> b } --- | Transform transforms a list by changing the map function that is passed to foldMap.+-- | The function 'transform' transforms a list by changing +-- the map function that is passed to 'foldMap'.+-- -- It has the following property: -- -- @transform a . transform b = transform (b . a)@@@ -128,6 +131,9 @@ snoc :: FMList a -> a -> FMList a snoc l x = FM $ \f -> unFM l f `mappend` f x +pair :: a -> a -> FMList a+pair l r = FM $ \f -> f l `mappend` f r+ append :: FMList a -> FMList a -> FMList a -- append l r = FM $ \f -> unFM l f `mappend` unFM r f append l r = FM $ unFM l `mappend` unFM r@@ -166,7 +172,7 @@ reverse l = FM $ \f -> getDual $ unFM l (Dual . f) -flatten :: FMList (FMList a) -> FMList a+flatten :: Foldable t => FMList (t a) -> FMList a flatten = transform foldMap filter :: (a -> Bool) -> FMList a -> FMList a@@ -204,15 +210,35 @@ repeat :: a -> FMList a repeat x = xs where xs = x `cons` xs +-- | 'unfoldr' builds an 'FMList' from a seed value from left to right.+-- The function takes the element and returns 'Nothing'+-- if it is done producing the list or returns 'Just' @(a,b)@, in which+-- case, @a@ is a appended to the result and @b@ is used as the next+-- seed value in a recursive call.+--+-- A simple use of 'unfoldr':+--+-- > *> unfoldr (\b -> if b == 0 then Nothing else Just (b, b-1)) 10+-- > fromList [10,9,8,7,6,5,4,3,2,1]+-- unfoldr :: (b -> Maybe (a, b)) -> b -> FMList a-unfoldr pf b = FM $ \f -> u f mempty (pf b) where- u _ acc Nothing = acc- u f acc (Just (a, b')) = u f (acc `mappend` f a) (pf b')+unfoldr g = unfold (maybe empty (\(a, b) -> Right a `pair` Left b) . g) -unfoldl :: (b -> Maybe (b, a)) -> b -> FMList a-unfoldl pf b = FM $ \f -> u f mempty (pf b) where- u _ acc Nothing = acc- u f acc (Just (b', a)) = u f (f a `mappend` acc) (pf b')+-- | 'unfold' builds a list from a seed value.+-- The function takes the seed and returns an 'FMList' +-- (or any other 'Foldable' instance) of values.+-- If the value is 'Right' @a@, then @a@ is appended to the result, and if the+-- value is 'Left' @b@, then @b@ is used as seed value in a recursive call.+--+-- A simple use of 'unfold' (simulating unfoldl):+--+-- > *> unfold (\b -> if b == 0 then empty else Left (b-1) `pair` Right b) 10+-- > fromList [1,2,3,4,5,6,7,8,9,10]+--+unfold :: Foldable t => (b -> t (Either b a)) -> b -> FMList a+unfold g b = FM $ \f -> foldMap (u f) (g b) where+ u f (Left b) = foldMap (u f) (g b)+ u f (Right a) = f a instance Functor FMList where
fmlist.cabal view
@@ -1,5 +1,5 @@ name: fmlist-version: 0.4+version: 0.5 synopsis: FoldMap lists description: FoldMap lists are lists represented by their foldMap function.