Open cpeikert opened 9 years ago
GOOD IDEA. I think we should totally get this into the 0.11 (we've a few other things we're keen on adding, and this totally makes sense to get into 0.11)
It's actually difficult to be very efficient (I think). The problem is that the efficient way to build a vector is to write to a mutable vector and freeze. But when you are traversing, you have an arbitrary applicative involved blocking your ability to do efficient ST stuff. So, you can only build up ST actions as closures, or use an intermediate list/stream.
You can do better than creating a list first, of course. But it might always be a bit lackluster.
I think it's a good thing to have, though.
I think it might look like this. Feel free to disregard the lens import, I was just using it for testing.
It would also be very useful to have mapAccumL
, mapAccumR
, mapAccumLM
, mapAccumRM
, etc.
Cant we define the stream / bundle version efficiently of traverse efficiently though? On Jan 23, 2015 4:27 AM, "yongqli" notifications@github.com wrote:
It would also be very useful to have mapAccumL, mapAccumR, mapAccumLM, mapAccumRM, etc.
— Reply to this email directly or view it on GitHub https://github.com/haskell/vector/issues/69#issuecomment-71167616.
Without contributing anything useful so far here, I'd benefit a lot from the mapAccum*
functions for vectors.
Cant we define the stream / bundle version efficiently of traverse efficiently though?
Maybe. The trouble is that Stream and Bundle need an underlying monad, but traverse only gives us an applicative. But I think we can get away with a monad transformer that carries the applicative "along for the ride," as if it were some kind of monoidal functor.
I am very interested in having some way of traversing vectors efficiently, especially in the way of the mapAccum*
functions. Is this possible at all?
@dolio now that primMonad is stackable, can we do something for mutable vectors that partially addresses this?
BUMP
this and some related stuff will be in the next minor and major releases both
Unboxed vectors can't be Traversable due to the Unbox constraint on the element type, but it would be very useful if there were an analogous generic traverse method:
Even better would be if it were efficient, i.e., didn't just convert to and from lists (like the Traversable instance for boxed vectors does) -- but I'm not sure if this is even possible.
sequence
comes close, except that it requiresMonad m
andVector v (m a)
, which will typically not hold for unboxed vectors.