Skip to content

Add FoldM1 #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- Add [`FoldM1`](https://github.com/Gabriella439/foldl/pull/219) type.
- Remove [unlawful](https://github.com/Gabriella439/foldl/pull/96) `ArrowChoice` instance for `Fold1`

1.4.18
Expand Down
20 changes: 10 additions & 10 deletions src/Control/Foldl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ import qualified Data.Semigroupoid
value of type __b__.
-}
data Fold a b
-- | @Fold @ @ step @ @ initial @ @ extract@
-- | @Fold step initial extract@.
= forall x. Fold (x -> a -> x) x (x -> b)

instance Functor (Fold a) where
Expand Down Expand Up @@ -403,7 +403,7 @@ instance Floating b => Floating (Fold a b) where
results in a monadic value of type __m b__.
-}
data FoldM m a b =
-- | @FoldM @ @ step @ @ initial @ @ extract@
-- | @FoldM step initial extract@.
forall x . FoldM (x -> a -> m x) (m x) (x -> m b)

instance Functor m => Functor (FoldM m a) where
Expand Down Expand Up @@ -1350,8 +1350,8 @@ dropM n (FoldM step begin done) = FoldM step' begin' done'

Any lens, traversal, or prism will type-check as a `Handler`
-}
type Handler a b =
forall x . (b -> Const (Dual (Endo x)) b) -> a -> Const (Dual (Endo x)) a
type Handler s a =
forall x . (a -> Const (Dual (Endo x)) a) -> s -> Const (Dual (Endo x)) s

{-| @(handles t folder)@ transforms the input of a `Fold` using a lens,
traversal, or prism:
Expand Down Expand Up @@ -1381,7 +1381,7 @@ type Handler a b =
>
> handles t (f <*> x) = handles t f <*> handles t x
-}
handles :: Handler a b -> Fold b r -> Fold a r
handles :: Handler s a -> Fold a r -> Fold s r
handles k (Fold step begin done) = Fold step' begin done
where
step' = flip (appEndo . getDual . getConst . k (Const . Dual . Endo . flip step))
Expand All @@ -1402,7 +1402,7 @@ handles k (Fold step begin done) = Fold step' begin done
> Foldl.foldOver folded == Foldl.fold

-}
foldOver :: Handler s a -> Fold a b -> s -> b
foldOver :: Handler s a -> Fold a r -> s -> r
foldOver l (Fold step begin done) =
done . flip appEndo begin . getDual . getConst . l (Const . Dual . Endo . flip step)
{-# INLINABLE foldOver #-}
Expand Down Expand Up @@ -1432,8 +1432,8 @@ instance Monad m => Monoid (EndoM m a) where

Any lens, traversal, or prism will type-check as a `HandlerM`
-}
type HandlerM m a b =
forall x . (b -> Const (Dual (EndoM m x)) b) -> a -> Const (Dual (EndoM m x)) a
type HandlerM m s a =
forall x . (a -> Const (Dual (EndoM m x)) a) -> s -> Const (Dual (EndoM m x)) s

{-| @(handlesM t folder)@ transforms the input of a `FoldM` using a lens,
traversal, or prism:
Expand All @@ -1453,7 +1453,7 @@ type HandlerM m a b =
>
> handlesM t (f <*> x) = handlesM t f <*> handlesM t x
-}
handlesM :: HandlerM m a b -> FoldM m b r -> FoldM m a r
handlesM :: HandlerM m s a -> FoldM m a r -> FoldM m s r
handlesM k (FoldM step begin done) = FoldM step' begin done
where
step' = flip (appEndoM . getDual . getConst . k (Const . Dual . EndoM . flip step))
Expand All @@ -1466,7 +1466,7 @@ handlesM k (FoldM step begin done) = FoldM step' begin done
> Foldl.foldOverM folded == Foldl.foldM

-}
foldOverM :: Monad m => HandlerM m s a -> FoldM m a b -> s -> m b
foldOverM :: Monad m => HandlerM m s a -> FoldM m a r -> s -> m r
foldOverM l (FoldM step begin done) s = do
b <- begin
r <- (flip appEndoM b . getDual . getConst . l (Const . Dual . EndoM . flip step)) s
Expand Down
Loading
Loading