>>> mapAccumL (\a b -> (a + b, a)) 0 [1..10] (55,[0,1,3,6,10,15,21,28,36,45])
>>> mapAccumL (\a b -> (a <> show b, a)) "0" [1..5] ("012345",["0","01","012","0123","01234"])
>>> mapAccumL (\acc x -> (acc + x,acc + 1)) 0 (1 :> 2 :> 3 :> 4 :> Nil) (10,1 :> 2 :> 4 :> 7 :> Nil)"mapAccumL f acc xs" corresponds to the following circuit layout:
>>> take 1 (snd (mapAccumL (\_ x -> (undefined, x)) undefined ('a' : undefined))) "a"
mapAccumL f acc0 (x1 :< x2 :< ...) = let (acc1, y1) = f acc0 x1 in let (acc2, y2) = f acc1 x2 in ... y1 :< y2 :< ...If you are looking how to traverse with a state, look no further.
mapAccumL ≡ mapAccumLOf traversemapAccumLOf accumulates State from left to right.
mapAccumLOf :: Iso s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) mapAccumLOf :: Lens s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) mapAccumLOf :: Traversal s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t)
mapAccumLOf :: LensLike (State acc) s t a b -> (acc -> a -> (acc, b)) -> acc -> s -> (acc, t) mapAccumLOf l f acc0 s = swap (runState (l (a -> state (acc -> swap (f acc a))) s) acc0)