We could fuse two traversals over the list xs
in the expression
(map f xs, map g xs)
like so
unzip (map (\x -> (f x, g x)) xs)
Is there any reasearch on performing this kind of fusion automatically?
(There's a risk to create a space leak here if one of the returned lists is consumed before the other. I'm more interested in preventing the extra traversal over xs
than saving space.)
Edit: I'm actually not looking to apply the fusion to actual in-memory Haskell lists, where this transformation might not make sense depending on if the unzip
can be fused with its consumer(s). I have a setting where I know unzip
can fuse (see "FlumeJava: easy, efficient data-parallel pipelines").