How about mapAccumL
/ mapAccumR
?
tzipWith :: Traversable t => (a -> b -> c) -> [a] -> t b -> Maybe (t c)
tzipWith f xs = sequenceA . snd . mapAccumL pair xs
where pair [] y = ([], Nothing)
pair (x:xs) y = (xs, Just (f x y))
tzip :: Traversable t => [a] -> t b -> Maybe (t (a, b))
tzip = tzipWith (,)
ghci> tzip [1..] [4,5,6]
Just [(1,4),(2,5),(3,6)]
ghci> tzip [1,2] [4,5,6]
Nothing
- mapAccum
, , , . , . , , State
( ST
), Traversable t
.