foldl-transduce 0.4.0.0 → 0.4.0.1
raw patch · 2 files changed
+74/−55 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Control.Foldl.Transduce: getMoore :: Moore a b u -> Cofree ((->) u) (ReifiedTransduction' a b u)
- Control.Foldl.Transduce: getMooreM :: MooreM m a b u -> Cofree ((->) u) (ReifiedTransductionM' m a b u)
- Control.Foldl.Transduce: getTransduction' :: ReifiedTransduction' a b r -> Transduction' a b r
- Control.Foldl.Transduce: getTransductionM' :: ReifiedTransductionM' m a b r -> TransductionM' m a b r
- Control.Foldl.Transduce: instance (Functor m, Monad m) => Bifunctor (TransducerM m i)
- Control.Foldl.Transduce: instance (m ~ m', Monad m') => ToTransductionsM' m (MooreM m')
- Control.Foldl.Transduce: instance (m ~ m', Monad m') => ToTransductionsM' m (ReifiedTransductionM' m')
- Control.Foldl.Transduce: instance (m ~ m', Monad m') => ToTransductionsM' m (TransducerM m')
- Control.Foldl.Transduce: instance Bifunctor (Transducer i)
- Control.Foldl.Transduce: instance Comonad (Fold a)
- Control.Foldl.Transduce: instance Comonad (Transducer i o)
- Control.Foldl.Transduce: instance Extend (Fold a)
- Control.Foldl.Transduce: instance Extend (Transducer i o)
- Control.Foldl.Transduce: instance Functor (Transducer i o)
- Control.Foldl.Transduce: instance Monad m => Extend (FoldM m a)
- Control.Foldl.Transduce: instance Monad m => Extend (TransducerM m i o)
- Control.Foldl.Transduce: instance Monad m => Functor (TransducerM m i o)
- Control.Foldl.Transduce: instance Monad m => ToFoldM m Fold
- Control.Foldl.Transduce: instance Monad m => ToTransducerM m Transducer
- Control.Foldl.Transduce: instance Monad m => ToTransductionsM' m Transducer
- Control.Foldl.Transduce: instance ToFold (FoldM Identity)
- Control.Foldl.Transduce: instance ToFold Fold
- Control.Foldl.Transduce: instance ToTransducer (TransducerM Identity)
- Control.Foldl.Transduce: instance ToTransducer Transducer
- Control.Foldl.Transduce: instance ToTransductions' Moore
- Control.Foldl.Transduce: instance ToTransductions' ReifiedTransduction'
- Control.Foldl.Transduce: instance ToTransductions' Transducer
- Control.Foldl.Transduce: instance m ~ m' => ToFoldM m (FoldM m')
- Control.Foldl.Transduce: instance m ~ m' => ToTransducerM m (TransducerM m')
+ Control.Foldl.Transduce: [getMooreM] :: MooreM m a b u -> Cofree ((->) u) (ReifiedTransductionM' m a b u)
+ Control.Foldl.Transduce: [getMoore] :: Moore a b u -> Cofree ((->) u) (ReifiedTransduction' a b u)
+ Control.Foldl.Transduce: [getTransduction'] :: ReifiedTransduction' a b r -> Transduction' a b r
+ Control.Foldl.Transduce: [getTransductionM'] :: ReifiedTransductionM' m a b r -> TransductionM' m a b r
+ Control.Foldl.Transduce: instance (GHC.Base.Functor m, GHC.Base.Monad m) => Data.Bifunctor.Bifunctor (Control.Foldl.Transduce.TransducerM m i)
+ Control.Foldl.Transduce: instance (m ~ m') => Control.Foldl.Transduce.ToFoldM m (Control.Foldl.FoldM m')
+ Control.Foldl.Transduce: instance (m ~ m') => Control.Foldl.Transduce.ToTransducerM m (Control.Foldl.Transduce.TransducerM m')
+ Control.Foldl.Transduce: instance (m ~ m', GHC.Base.Monad m') => Control.Foldl.Transduce.ToTransductionsM' m (Control.Foldl.Transduce.MooreM m')
+ Control.Foldl.Transduce: instance (m ~ m', GHC.Base.Monad m') => Control.Foldl.Transduce.ToTransductionsM' m (Control.Foldl.Transduce.ReifiedTransductionM' m')
+ Control.Foldl.Transduce: instance (m ~ m', GHC.Base.Monad m') => Control.Foldl.Transduce.ToTransductionsM' m (Control.Foldl.Transduce.TransducerM m')
+ Control.Foldl.Transduce: instance Control.Comonad.Comonad (Control.Foldl.Transduce.Transducer i o)
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToFold (Control.Foldl.FoldM Data.Functor.Identity.Identity)
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToFold Control.Foldl.Fold
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToTransducer (Control.Foldl.Transduce.TransducerM Data.Functor.Identity.Identity)
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToTransducer Control.Foldl.Transduce.Transducer
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToTransductions' Control.Foldl.Transduce.Moore
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToTransductions' Control.Foldl.Transduce.ReifiedTransduction'
+ Control.Foldl.Transduce: instance Control.Foldl.Transduce.ToTransductions' Control.Foldl.Transduce.Transducer
+ Control.Foldl.Transduce: instance Data.Bifunctor.Bifunctor (Control.Foldl.Transduce.Transducer i)
+ Control.Foldl.Transduce: instance Data.Functor.Extend.Extend (Control.Foldl.Fold a)
+ Control.Foldl.Transduce: instance Data.Functor.Extend.Extend (Control.Foldl.Transduce.Transducer i o)
+ Control.Foldl.Transduce: instance GHC.Base.Functor (Control.Foldl.Transduce.Transducer i o)
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => Control.Foldl.Transduce.ToFoldM m Control.Foldl.Fold
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => Control.Foldl.Transduce.ToTransducerM m Control.Foldl.Transduce.Transducer
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => Control.Foldl.Transduce.ToTransductionsM' m Control.Foldl.Transduce.Transducer
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => Data.Functor.Extend.Extend (Control.Foldl.FoldM m a)
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => Data.Functor.Extend.Extend (Control.Foldl.Transduce.TransducerM m i o)
+ Control.Foldl.Transduce: instance GHC.Base.Monad m => GHC.Base.Functor (Control.Foldl.Transduce.TransducerM m i o)
- Control.Foldl.Transduce: groups' :: (ToTransducer s, ToFold f, ToTransductions' t) => s a b r -> t b c u -> f u v -> Transduction' a c (r, v)
+ Control.Foldl.Transduce: groups' :: (ToTransducer s, ToTransductions' t, ToFold f) => s a b r -> t b c u -> f u v -> Transduction' a c (r, v)
- Control.Foldl.Transduce: groupsM' :: (Monad m, ToTransducerM m s, ToFoldM m f, ToTransductionsM' m t) => s a b r -> t b c u -> f u v -> TransductionM' m a c (r, v)
+ Control.Foldl.Transduce: groupsM' :: (Monad m, ToTransducerM m s, ToTransductionsM' m t, ToFoldM m f) => s a b r -> t b c u -> f u v -> TransductionM' m a c (r, v)
- Control.Foldl.Transduce: transduceK :: Monad m => (i -> m [o]) -> TransductionM m i o
+ Control.Foldl.Transduce: transduceK :: (Monad m) => (i -> m [o]) -> TransductionM m i o
Files
- foldl-transduce.cabal +1/−1
- src/Control/Foldl/Transduce.hs +73/−54
foldl-transduce.cabal view
@@ -1,5 +1,5 @@ Name: foldl-transduce-Version: 0.4.0.0+Version: 0.4.0.1 Cabal-Version: >=1.8.0.2 Build-Type: Simple License: BSD3
src/Control/Foldl/Transduce.hs view
@@ -149,7 +149,7 @@ -} type Transduction' a b r = forall x. Fold b x -> Fold a (r,x) -{-| Helper for storing a 'ReifiedTransduction' safely on a container. +{-| Helper for storing a 'ReifiedTransduction'' safely on a container. -} newtype ReifiedTransduction' a b r = ReifiedTransduction' { getTransduction' :: Transduction' a b r }@@ -177,8 +177,8 @@ current step. 'Transducer's that do not perform grouping never return anything other than @[]@ here. In effect, they treat the whole stream as a single group. - The extraction function returns the 'Transducer's own result value, as- well as any pending output.+ The extraction function returns the 'Transducer's own result value, along with any+ pending output. -} data Transducer i o r = forall x. Transducer (x -> i -> (x,[o],[[o]])) x (x -> (r,[o],[[o]]))@@ -640,11 +640,9 @@ "<aa><bb><cc><dd>" >>> :{ - let - transducers = Moore $ flip C.unfold 0 $ \i -> (,)- (reify (transduce (surround (show i) []))) - (const (succ i))- in L.fold (groups (chunksOf 2) transducers L.list) "aabbccdd"+ let transductions = Moore (C.unfold (\i ->+ (reify (transduce (surround (show i) [])), \_ -> succ i)) 0)+ in L.fold (groups (chunksOf 2) transductions L.list) "aabbccdd" :} "0aa1bb2cc3dd" -}@@ -655,6 +653,14 @@ groups splitter transductions oldfold = fmap snd (groups' splitter transductions L.mconcat oldfold) +{-| Use a different 'Transduction' for the first detected group. ++>>> :{ + let drop n = bisect (splitAt n) ignore (reify id)+ in L.fold (drop 2 L.list) "aabbccdd"+ :}+"bbccdd"+-} bisect :: (ToTransducer s, ToTransductions' h, ToTransductions' t) => s a b r -- ^ 'Transducer' working as a splitter. -> h b c () -- ^ Machine to process the first group@@ -670,52 +676,58 @@ >>> :{ - let - transductions = reify' $ - \f -> transduce (surround "<" ">") (liftA2 (,) L.list f)- in L.fold (groups' (chunksOf 2) transductions L.list L.list) "aabbccdd"+ let transductions = + reify' (\f -> transduce (surround "<" ">") ((,) <$> L.list <*> f))+ in L.fold (groups' (chunksOf 2) transductions L.list L.list) "aabbccdd" :} (((),["<aa>","<bb>","<cc>","<dd>"]),"<aa><bb><cc><dd>") -}-groups' :: (ToTransducer s, ToFold f, ToTransductions' t)+groups' :: (ToTransducer s, ToTransductions' t, ToFold f) => s a b r -- ^ 'Transducer' working as a splitter. -> t b c u -- ^ machine that eats @u@ values and spits transductions -> f u v -- ^ auxiliary 'Fold' that aggregates the @u@ values produced for each group -> Transduction' a c (r,v) -groups' (toTransducer -> Transducer sstep sbegin sdone) (toTransductions' -> Moore (ReifiedTransduction' t0 :< somemachine)) (toFold -> somesummarizer) somefold =- Fold step (Quartet sbegin somesummarizer (t0 (duplicated somefold)) somemachine) done +groups' (toTransducer -> Transducer sstep sbegin sdone) + (toTransductions' -> Moore (ReifiedTransduction' t0 :< somemachine)) + (toFold -> Fold astep abegin adone) + somefold + =+ Fold step (Quartet sbegin somemachine abegin (t0 (duplicated somefold))) done where - step (Quartet sstate summarizer innerfold machine) i =+ step (Quartet sstate machine astate innerfold) i = let - (sstate', oldSplit, newSplits) = sstep sstate i- (summarizer',innerfold',machine') = + (sstate',oldSplit,newSplits) = sstep sstate i+ (machine',astate',innerfold') = foldl' step'- (summarizer, feed innerfold oldSplit,machine) + (machine,astate,feed innerfold oldSplit) newSplits in- Quartet sstate' summarizer' innerfold' machine'+ Quartet sstate' machine' astate' innerfold' - step' (summarizer_,innerfold_,machine_) somesplit = + done (Quartet sstate machine astate innerfold) = + let + (s,oldSplit,newSplits) = sdone sstate+ (_,astate',innerfold') = + foldl' + step'+ (machine,astate,feed innerfold oldSplit) + newSplits+ (u,finalfold) = extract innerfold'+ in ((s,adone (astep astate' u)),extract finalfold)++ step' (machine_,astate,innerfold_) somesplit = let (u,resetted,nextmachine) = reset machine_ innerfold_- in (L.fold (duplicated summarizer_) [u], feed resetted somesplit,nextmachine)+ in (nextmachine,astep astate u,feed resetted somesplit) feed = L.fold . duplicated+ reset machine (Fold _ fstate fdone) = let (u,nextfold) = fdone fstate ReifiedTransduction' t1 :< nextmachine = machine u in (u,t1 (duplicated nextfold),nextmachine)- done (Quartet sstate summarizer innerfold machine) = - let - (s,oldSplit,newSplits) = sdone sstate- (summarizer',innerfold',_) = - foldl' - step'- (summarizer,feed innerfold oldSplit,machine) - newSplits- (u,finalfold) = extract innerfold'- in ((s,L.fold summarizer' [u]),extract finalfold) + {-| Monadic version of 'groups'. -}@@ -740,26 +752,42 @@ {-| Monadic version of 'groups''. -}-groupsM' :: (Monad m, ToTransducerM m s, ToFoldM m f, ToTransductionsM' m t) +groupsM' :: (Monad m, ToTransducerM m s, ToTransductionsM' m t, ToFoldM m f) => s a b r -> t b c u -- ^ -> f u v -> TransductionM' m a c (r,v) --groupsM' (toTransducerM -> TransducerM sstep sbegin sdone) (toTransductionsM' -> MooreM (ReifiedTransductionM' t0 :< somemachine)) (toFoldM -> somesummarizer) somefold =- FoldM step (sbegin >>= \x -> return (Quartet x somesummarizer (t0 (duplicated somefold)) somemachine)) done +groupsM' (toTransducerM -> TransducerM sstep sbegin sdone) + (toTransductionsM' -> MooreM (ReifiedTransductionM' t0 :< somemachine)) + (toFoldM -> FoldM astep abegin adone) + somefold + =+ FoldM step + (do sbegin' <- sbegin+ abegin' <- abegin+ return (Quartet sbegin' somemachine abegin' (t0 (duplicated somefold))))+ done where- step (Quartet sstate summarizer innerfold machine) i = do- (sstate', oldSplit, newSplits) <- sstep sstate i + step (Quartet sstate machine astate innerfold) i = do+ (sstate',oldSplit, newSplits) <- sstep sstate i innerfold' <- feed innerfold oldSplit- (summarizer',innerfold'',machine') <- foldlM step' (summarizer,innerfold',machine) newSplits- return $! Quartet sstate' summarizer' innerfold'' machine'+ (machine',astate',innerfold'') <- foldlM step' (machine,astate,innerfold') newSplits+ return $! Quartet sstate' machine' astate' innerfold'' - step' = \(summarizer,innerfold,machine) is -> do+ done (Quartet sstate machine astate innerfold) = do+ (s,oldSplit,newSplits) <- sdone sstate+ innerfold' <- feed innerfold oldSplit+ (_,astate',innerfold'') <- foldlM step' (machine,astate,innerfold') newSplits+ (u,finalfold) <- L.foldM innerfold'' []+ v <- adone =<< astep astate' u+ r <- L.foldM finalfold []+ return ((s,v),r)++ step' (machine,astate,innerfold) is = do (u,innerfold',machine') <- reset machine innerfold - summarizer' <- L.foldM (duplicated summarizer) [u]+ astate' <- astep astate u innerfold'' <- feed innerfold' is- return $! (summarizer',innerfold'',machine') + return $! (machine',astate',innerfold'') feed = L.foldM . duplicated @@ -769,15 +797,6 @@ ReifiedTransductionM' t1 :< nextmachine = machine u return (u,t1 (duplicated nextfold),nextmachine) - done (Quartet sstate summarizer innerfold machine) = do- (s,oldSplit,newSplits) <- sdone sstate- innerfold' <- feed innerfold oldSplit- (summarizer',innerfold'',_) <- foldlM step' (summarizer,innerfold',machine) newSplits- (u,finalfold) <- L.foldM innerfold'' []- v <- L.foldM summarizer' [u]- r <- L.foldM finalfold []- return ((s,v),r)- {-| Summarizes each of the groups demarcated by the 'Transducer' using a 'Fold'. @@ -787,7 +806,7 @@ [6,15,7] -} folds :: (ToTransducer t, ToFold f) - => t a b s -- ^+ => t a b s -- ^ 'Transducer' working as a splitter. -> f b c -> Transduction a c folds splitter (toFold -> f) = groups splitter (fmap (const ()) (condense f))@@ -798,7 +817,7 @@ ((),[6,15,7]) -} folds' :: (ToTransducer t, ToFold f) - => t a b s -- ^+ => t a b s -- ^ 'Transducer' working as a splitter. -> f b c -> Transduction' a c s folds' splitter (toFold -> innerfold) somefold =