@@ -39,6 +39,7 @@ import Control.Alternative (class Alternative)
39
39
import Control.Monad.Eff.Class (class MonadEff , liftEff )
40
40
import Control.Monad.Trans.Class (class MonadTrans , lift )
41
41
import Control.MonadPlus (class MonadPlus )
42
+ import Control.Monad.Rec.Class as MR
42
43
import Control.MonadZero (class MonadZero )
43
44
import Control.Plus (class Plus )
44
45
@@ -204,12 +205,14 @@ foldl' f = loop where
204
205
g (Just (Tuple a as)) = (f b a) >>= (flip loop as)
205
206
206
207
-- | Fold a list from the left, accumulating the result using the specified function.
207
- foldl :: forall f a b . Monad f => (b -> a -> b ) -> b -> ListT f a -> f b
208
- foldl f = loop where
209
- loop b l = uncons l >>= g
210
- where
211
- g Nothing = pure b
212
- g (Just (Tuple a as)) = loop (f b a) as
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
210
+ where
211
+ loop :: b -> ListT f a -> f (MR.Step { a :: b , b :: ListT f a } b )
212
+ loop accum l' = uncons l' >>= g
213
+ where
214
+ g Nothing = pure (MR.Done accum)
215
+ g (Just (Tuple a as)) = pure (MR.Loop {a: f accum a, b: as})
213
216
214
217
-- | Fold a list from the left, accumulating the list of results using the specified function.
215
218
scanl :: forall f a b . Monad f => (b -> a -> b ) -> b -> ListT f a -> ListT f b
0 commit comments