Skip to content

Commit 61ef639

Browse files
committed
name TCOd foldl fodlRec
1 parent 5b7e88e commit 61ef639

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/Control/Monad/List/Trans.purs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Control.Monad.List.Trans
99
, dropWhile
1010
, filter
1111
, foldl
12+
, foldlRec
1213
, foldl'
1314
, fromEffect
1415
, head
@@ -205,14 +206,22 @@ foldl' f = loop where
205206
g (Just (Tuple a as)) = (f b a) >>= (flip loop as)
206207

207208
-- | 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
210220
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
213222
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})
216225

217226
-- | Fold a list from the left, accumulating the list of results using the specified function.
218227
scanl :: forall f a b. Monad f => (b -> a -> b) -> b -> ListT f a -> ListT f b

0 commit comments

Comments
 (0)