@@ -9,6 +9,7 @@ module Control.Monad.List.Trans
9
9
, dropWhile
10
10
, filter
11
11
, foldl
12
+ , foldlRec
12
13
, foldl'
13
14
, fromEffect
14
15
, head
@@ -205,14 +206,22 @@ foldl' f = loop where
205
206
g (Just (Tuple a as)) = (f b a) >>= (flip loop as)
206
207
207
208
-- | Fold a list from the left, accumulating the result using the specified function.
208
- foldl :: forall f a b . MR.MonadRec f => (b -> a -> b ) -> b -> ListT f a -> f b
209
- foldl f b l = (MR .tailRecM2 loop) b l
209
+ foldl :: forall f a b . Monad f => (b -> a -> b ) -> b -> ListT f a -> f b
210
+ foldl f = loop where
211
+ loop b l = uncons l >>= g
212
+ where
213
+ g Nothing = pure b
214
+ g (Just (Tuple a as)) = loop (f b a) as
215
+
216
+ -- | Fold a list from the left, accumulating the result using the specified function.
217
+ -- | Uses tail call optimization.
218
+ foldlRec :: forall f a b . MR.MonadRec f => (b -> a -> b ) -> b -> ListT f a -> f b
219
+ foldlRec f = MR .tailRecM2 loop
210
220
where
211
- loop :: b -> ListT f a -> f (MR.Step { a :: b , b :: ListT f a } b )
212
- loop accum l' = uncons l' >>= g
221
+ loop b l = uncons l >>= g
213
222
where
214
- g Nothing = pure (MR.Done accum )
215
- g (Just (Tuple a as)) = pure (MR.Loop {a: f accum a, b: as})
223
+ g Nothing = pure (MR.Done b )
224
+ g (Just (Tuple a as)) = pure (MR.Loop {a: f b a, b: as})
216
225
217
226
-- | Fold a list from the left, accumulating the list of results using the specified function.
218
227
scanl :: forall f a b . Monad f => (b -> a -> b ) -> b -> ListT f a -> ListT f b
0 commit comments