packages feed

deferred-folds 0.3.0.1 → 0.4

raw patch · 5 files changed

+200/−200 lines, 5 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- DeferredFolds.FoldlMView: FoldlMView :: (forall output. (output -> input -> m output) -> output -> m output) -> FoldlMView m input
- DeferredFolds.FoldlMView: fold :: Fold input output -> FoldlMView Identity input -> output
- DeferredFolds.FoldlMView: foldM :: Monad m => FoldM m input output -> FoldlMView m input -> m output
- DeferredFolds.FoldlMView: foldable :: (Monad m, Foldable foldable) => foldable a -> FoldlMView m a
- DeferredFolds.FoldlMView: foldl' :: (output -> input -> output) -> output -> FoldlMView Identity input -> output
- DeferredFolds.FoldlMView: foldlM' :: Monad m => (output -> input -> m output) -> output -> FoldlMView m input -> m output
- DeferredFolds.FoldlMView: instance GHC.Base.Functor m => GHC.Base.Functor (DeferredFolds.FoldlMView.FoldlMView m)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => Data.Semigroup.Semigroup (DeferredFolds.FoldlMView.FoldlMView m a)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => GHC.Base.Alternative (DeferredFolds.FoldlMView.FoldlMView m)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => GHC.Base.Applicative (DeferredFolds.FoldlMView.FoldlMView m)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => GHC.Base.Monad (DeferredFolds.FoldlMView.FoldlMView m)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => GHC.Base.MonadPlus (DeferredFolds.FoldlMView.FoldlMView m)
- DeferredFolds.FoldlMView: instance GHC.Base.Monad m => GHC.Base.Monoid (DeferredFolds.FoldlMView.FoldlMView m a)
- DeferredFolds.FoldlMView: intsInRange :: Monad m => Int -> Int -> FoldlMView m Int
- DeferredFolds.FoldlMView: newtype FoldlMView m input
- DeferredFolds.FoldlView: FoldlView :: (forall output. (output -> input -> output) -> output -> output) -> FoldlView input
- DeferredFolds.FoldlView: fold :: Fold input output -> FoldlView input -> output
- DeferredFolds.FoldlView: foldable :: Foldable foldable => foldable a -> FoldlView a
- DeferredFolds.FoldlView: foldl' :: (output -> input -> output) -> output -> FoldlView input -> output
- DeferredFolds.FoldlView: instance Data.Semigroup.Semigroup (DeferredFolds.FoldlView.FoldlView a)
- DeferredFolds.FoldlView: instance GHC.Base.Alternative DeferredFolds.FoldlView.FoldlView
- DeferredFolds.FoldlView: instance GHC.Base.Applicative DeferredFolds.FoldlView.FoldlView
- DeferredFolds.FoldlView: instance GHC.Base.Functor DeferredFolds.FoldlView.FoldlView
- DeferredFolds.FoldlView: instance GHC.Base.Monad DeferredFolds.FoldlView.FoldlView
- DeferredFolds.FoldlView: instance GHC.Base.MonadPlus DeferredFolds.FoldlView.FoldlView
- DeferredFolds.FoldlView: instance GHC.Base.Monoid (DeferredFolds.FoldlView.FoldlView a)
- DeferredFolds.FoldlView: intsInRange :: Int -> Int -> FoldlView Int
- DeferredFolds.FoldlView: newtype FoldlView input
+ DeferredFolds.Unfold: Unfold :: (forall output. (output -> input -> output) -> output -> output) -> Unfold input
+ DeferredFolds.Unfold: fold :: Fold input output -> Unfold input -> output
+ DeferredFolds.Unfold: foldable :: Foldable foldable => foldable a -> Unfold a
+ DeferredFolds.Unfold: foldl' :: (output -> input -> output) -> output -> Unfold input -> output
+ DeferredFolds.Unfold: instance Data.Semigroup.Semigroup (DeferredFolds.Unfold.Unfold a)
+ DeferredFolds.Unfold: instance GHC.Base.Alternative DeferredFolds.Unfold.Unfold
+ DeferredFolds.Unfold: instance GHC.Base.Applicative DeferredFolds.Unfold.Unfold
+ DeferredFolds.Unfold: instance GHC.Base.Functor DeferredFolds.Unfold.Unfold
+ DeferredFolds.Unfold: instance GHC.Base.Monad DeferredFolds.Unfold.Unfold
+ DeferredFolds.Unfold: instance GHC.Base.MonadPlus DeferredFolds.Unfold.Unfold
+ DeferredFolds.Unfold: instance GHC.Base.Monoid (DeferredFolds.Unfold.Unfold a)
+ DeferredFolds.Unfold: intsInRange :: Int -> Int -> Unfold Int
+ DeferredFolds.Unfold: newtype Unfold input
+ DeferredFolds.UnfoldM: UnfoldM :: (forall output. (output -> input -> m output) -> output -> m output) -> UnfoldM m input
+ DeferredFolds.UnfoldM: fold :: Fold input output -> UnfoldM Identity input -> output
+ DeferredFolds.UnfoldM: foldM :: Monad m => FoldM m input output -> UnfoldM m input -> m output
+ DeferredFolds.UnfoldM: foldable :: (Monad m, Foldable foldable) => foldable a -> UnfoldM m a
+ DeferredFolds.UnfoldM: foldl' :: (output -> input -> output) -> output -> UnfoldM Identity input -> output
+ DeferredFolds.UnfoldM: foldlM' :: Monad m => (output -> input -> m output) -> output -> UnfoldM m input -> m output
+ DeferredFolds.UnfoldM: instance GHC.Base.Functor m => GHC.Base.Functor (DeferredFolds.UnfoldM.UnfoldM m)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => Data.Semigroup.Semigroup (DeferredFolds.UnfoldM.UnfoldM m a)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => GHC.Base.Alternative (DeferredFolds.UnfoldM.UnfoldM m)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => GHC.Base.Applicative (DeferredFolds.UnfoldM.UnfoldM m)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => GHC.Base.Monad (DeferredFolds.UnfoldM.UnfoldM m)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => GHC.Base.MonadPlus (DeferredFolds.UnfoldM.UnfoldM m)
+ DeferredFolds.UnfoldM: instance GHC.Base.Monad m => GHC.Base.Monoid (DeferredFolds.UnfoldM.UnfoldM m a)
+ DeferredFolds.UnfoldM: intsInRange :: Monad m => Int -> Int -> UnfoldM m Int
+ DeferredFolds.UnfoldM: newtype UnfoldM m input

Files

deferred-folds.cabal view
@@ -1,7 +1,7 @@ name:   deferred-folds version:-  0.3.0.1+  0.4 category:   Folding synopsis:@@ -39,8 +39,8 @@   default-language:     Haskell2010   exposed-modules:-    DeferredFolds.FoldlView-    DeferredFolds.FoldlMView+    DeferredFolds.Unfold+    DeferredFolds.UnfoldM   other-modules:     DeferredFolds.Prelude   build-depends:
− library/DeferredFolds/FoldlMView.hs
@@ -1,94 +0,0 @@-module DeferredFolds.FoldlMView-where--import DeferredFolds.Prelude hiding (foldl')-import qualified DeferredFolds.Prelude as A---{-|-A monadic variation of "DeferredFolds.FoldlView"--}-newtype FoldlMView m input =-  FoldlMView (forall output. (output -> input -> m output) -> output -> m output)--deriving instance Functor m => Functor (FoldlMView m)--instance Monad m => Applicative (FoldlMView m) where-  pure x =-    FoldlMView (\ step init -> step init x)-  (<*>) = ap--instance Monad m => Alternative (FoldlMView m) where-  empty =-    FoldlMView (const return)-  {-# INLINE (<|>) #-}-  (<|>) (FoldlMView left) (FoldlMView right) =-    FoldlMView (\ step init -> left step init >>= right step)--instance Monad m => Monad (FoldlMView m) where-  return = pure-  (>>=) (FoldlMView left) rightK =-    FoldlMView $ \ step init ->-    let-      newStep output x =-        case rightK x of-          FoldlMView right ->-            right step output-      in left newStep init--instance Monad m => MonadPlus (FoldlMView m) where-  mzero = empty-  mplus = (<|>)--instance Monad m => Semigroup (FoldlMView m a) where-  (<>) = (<|>)--instance Monad m => Monoid (FoldlMView m a) where-  mempty = empty-  mappend = (<>)--{-| Perform a strict left fold -}-{-# INLINE foldl' #-}-foldl' :: (output -> input -> output) -> output -> FoldlMView Identity input -> output-foldl' step init (FoldlMView run) =-  runIdentity (run identityStep init)-  where-    identityStep state input = return (step state input)--{-| Perform a monadic strict left fold -}-{-# INLINE foldlM' #-}-foldlM' :: Monad m => (output -> input -> m output) -> output -> FoldlMView m input -> m output-foldlM' step init (FoldlMView run) =-  run step init--{-| Apply a Gonzalez fold -}-{-# INLINE fold #-}-fold :: Fold input output -> FoldlMView Identity input -> output-fold (Fold step init extract) = extract . foldl' step init--{-| Apply a monadic Gonzalez fold -}-{-# INLINE foldM #-}-foldM :: Monad m => FoldM m input output -> FoldlMView m input -> m output-foldM (FoldM step init extract) view =-  do-    initialState <- init-    finalState <- foldlM' step initialState view-    extract finalState--{-| Construct from any foldable -}-{-# INLINE foldable #-}-foldable :: (Monad m, Foldable foldable) => foldable a -> FoldlMView m a-foldable foldable = FoldlMView (\ step init -> A.foldlM step init foldable)--{-| Ints in the specified inclusive range -}-intsInRange :: Monad m => Int -> Int -> FoldlMView m Int-intsInRange from to =-  FoldlMView $ \ step init ->-  let-    loop !state int =-      if int <= to-        then do-          newState <- step state int-          loop newState (succ int)-        else return state-    in loop init from
− library/DeferredFolds/FoldlView.hs
@@ -1,103 +0,0 @@-module DeferredFolds.FoldlView-where--import DeferredFolds.Prelude-import qualified DeferredFolds.Prelude as A---{-|-A projection on data, which only knows how to execute a strict left-fold.--It is a monad and a monoid, and is very useful for-efficiently aggregating the projections on data intended for left-folding,-since its concatenation (`<>`) has complexity of @O(1)@.--[Intuition]--The intuition of what this abstraction is all about can be derived from lists.--Let's consider the `Data.List.foldl'` function for lists:-->foldl' :: (b -> a -> b) -> b -> [a] -> b--If we reverse its parameters we get-->foldl' :: [a] -> (b -> a -> b) -> b -> b--Which in Haskell is essentially the same as-->foldl' :: [a] -> (forall b. (b -> a -> b) -> b -> b)--We can isolate that part into an abstraction:-->newtype FoldlView a = FoldlView (forall b. (b -> a -> b) -> b -> b)--Then we get to this simple morphism:-->foldl' :: [a] -> FoldlView a---}-newtype FoldlView input =-  FoldlView (forall output. (output -> input -> output) -> output -> output)--deriving instance Functor FoldlView--instance Applicative FoldlView where-  pure x =-    FoldlView (\ step init -> step init x)-  (<*>) = ap--instance Alternative FoldlView where-  empty =-    FoldlView (const id)-  {-# INLINE (<|>) #-}-  (<|>) (FoldlView left) (FoldlView right) =-    FoldlView (\ step init -> right step (left step init))--instance Monad FoldlView where-  return = pure-  (>>=) (FoldlView left) rightK =-    FoldlView $ \ step init ->-    let-      newStep output x =-        case rightK x of-          FoldlView right ->-            right step output-      in left newStep init--instance MonadPlus FoldlView where-  mzero = empty-  mplus = (<|>)--instance Semigroup (FoldlView a) where-  (<>) = (<|>)--instance Monoid (FoldlView a) where-  mempty = empty-  mappend = (<>)--{-| Perform a strict left fold -}-{-# INLINE foldl' #-}-foldl' :: (output -> input -> output) -> output -> FoldlView input -> output-foldl' step init (FoldlView run) = run step init--{-| Apply a Gonzalez fold -}-{-# INLINE fold #-}-fold :: Fold input output -> FoldlView input -> output-fold (Fold step init extract) (FoldlView run) = extract (run step init)--{-| Construct from any foldable -}-{-# INLINE foldable #-}-foldable :: Foldable foldable => foldable a -> FoldlView a-foldable foldable = FoldlView (\ step init -> A.foldl' step init foldable)--{-| Ints in the specified inclusive range -}-intsInRange :: Int -> Int -> FoldlView Int-intsInRange from to =-  FoldlView $ \ step init ->-  let-    loop !state int =-      if int <= to-        then loop (step state int) (succ int)-        else state-    in loop init from
+ library/DeferredFolds/Unfold.hs view
@@ -0,0 +1,103 @@+module DeferredFolds.Unfold+where++import DeferredFolds.Prelude+import qualified DeferredFolds.Prelude as A+++{-|+A projection on data, which only knows how to execute a strict left-fold.++It is a monad and a monoid, and is very useful for+efficiently aggregating the projections on data intended for left-folding,+since its concatenation (`<>`) has complexity of @O(1)@.++[Intuition]++The intuition of what this abstraction is all about can be derived from lists.++Let's consider the `Data.List.foldl'` function for lists:++>foldl' :: (b -> a -> b) -> b -> [a] -> b++If we reverse its parameters we get++>foldl' :: [a] -> (b -> a -> b) -> b -> b++Which in Haskell is essentially the same as++>foldl' :: [a] -> (forall b. (b -> a -> b) -> b -> b)++We can isolate that part into an abstraction:++>newtype Unfold a = Unfold (forall b. (b -> a -> b) -> b -> b)++Then we get to this simple morphism:++>foldl' :: [a] -> Unfold a++-}+newtype Unfold input =+  Unfold (forall output. (output -> input -> output) -> output -> output)++deriving instance Functor Unfold++instance Applicative Unfold where+  pure x =+    Unfold (\ step init -> step init x)+  (<*>) = ap++instance Alternative Unfold where+  empty =+    Unfold (const id)+  {-# INLINE (<|>) #-}+  (<|>) (Unfold left) (Unfold right) =+    Unfold (\ step init -> right step (left step init))++instance Monad Unfold where+  return = pure+  (>>=) (Unfold left) rightK =+    Unfold $ \ step init ->+    let+      newStep output x =+        case rightK x of+          Unfold right ->+            right step output+      in left newStep init++instance MonadPlus Unfold where+  mzero = empty+  mplus = (<|>)++instance Semigroup (Unfold a) where+  (<>) = (<|>)++instance Monoid (Unfold a) where+  mempty = empty+  mappend = (<>)++{-| Perform a strict left fold -}+{-# INLINE foldl' #-}+foldl' :: (output -> input -> output) -> output -> Unfold input -> output+foldl' step init (Unfold run) = run step init++{-| Apply a Gonzalez fold -}+{-# INLINE fold #-}+fold :: Fold input output -> Unfold input -> output+fold (Fold step init extract) (Unfold run) = extract (run step init)++{-| Construct from any foldable -}+{-# INLINE foldable #-}+foldable :: Foldable foldable => foldable a -> Unfold a+foldable foldable = Unfold (\ step init -> A.foldl' step init foldable)++{-| Ints in the specified inclusive range -}+intsInRange :: Int -> Int -> Unfold Int+intsInRange from to =+  Unfold $ \ step init ->+  let+    loop !state int =+      if int <= to+        then loop (step state int) (succ int)+        else state+    in loop init from
+ library/DeferredFolds/UnfoldM.hs view
@@ -0,0 +1,94 @@+module DeferredFolds.UnfoldM+where++import DeferredFolds.Prelude hiding (foldl')+import qualified DeferredFolds.Prelude as A+++{-|+A monadic variation of "DeferredFolds.Unfold"+-}+newtype UnfoldM m input =+  UnfoldM (forall output. (output -> input -> m output) -> output -> m output)++deriving instance Functor m => Functor (UnfoldM m)++instance Monad m => Applicative (UnfoldM m) where+  pure x =+    UnfoldM (\ step init -> step init x)+  (<*>) = ap++instance Monad m => Alternative (UnfoldM m) where+  empty =+    UnfoldM (const return)+  {-# INLINE (<|>) #-}+  (<|>) (UnfoldM left) (UnfoldM right) =+    UnfoldM (\ step init -> left step init >>= right step)++instance Monad m => Monad (UnfoldM m) where+  return = pure+  (>>=) (UnfoldM left) rightK =+    UnfoldM $ \ step init ->+    let+      newStep output x =+        case rightK x of+          UnfoldM right ->+            right step output+      in left newStep init++instance Monad m => MonadPlus (UnfoldM m) where+  mzero = empty+  mplus = (<|>)++instance Monad m => Semigroup (UnfoldM m a) where+  (<>) = (<|>)++instance Monad m => Monoid (UnfoldM m a) where+  mempty = empty+  mappend = (<>)++{-| Perform a strict left fold -}+{-# INLINE foldl' #-}+foldl' :: (output -> input -> output) -> output -> UnfoldM Identity input -> output+foldl' step init (UnfoldM run) =+  runIdentity (run identityStep init)+  where+    identityStep state input = return (step state input)++{-| Perform a monadic strict left fold -}+{-# INLINE foldlM' #-}+foldlM' :: Monad m => (output -> input -> m output) -> output -> UnfoldM m input -> m output+foldlM' step init (UnfoldM run) =+  run step init++{-| Apply a Gonzalez fold -}+{-# INLINE fold #-}+fold :: Fold input output -> UnfoldM Identity input -> output+fold (Fold step init extract) = extract . foldl' step init++{-| Apply a monadic Gonzalez fold -}+{-# INLINE foldM #-}+foldM :: Monad m => FoldM m input output -> UnfoldM m input -> m output+foldM (FoldM step init extract) view =+  do+    initialState <- init+    finalState <- foldlM' step initialState view+    extract finalState++{-| Construct from any foldable -}+{-# INLINE foldable #-}+foldable :: (Monad m, Foldable foldable) => foldable a -> UnfoldM m a+foldable foldable = UnfoldM (\ step init -> A.foldlM step init foldable)++{-| Ints in the specified inclusive range -}+intsInRange :: Monad m => Int -> Int -> UnfoldM m Int+intsInRange from to =+  UnfoldM $ \ step init ->+  let+    loop !state int =+      if int <= to+        then do+          newState <- step state int+          loop newState (succ int)+        else return state+    in loop init from