vector icon indicating copy to clipboard operation
vector copied to clipboard

add generic traverse method, a la Data.Traversable

Open cpeikert opened this issue 10 years ago • 11 comments

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:

traverse :: (Applicative f, Vector v a, Vector v b) => (a -> f b) -> v a -> f (v b)

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 requires Monad m and Vector v (m a), which will typically not hold for unboxed vectors.

cpeikert avatar Jan 23 '15 05:01 cpeikert

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)

cartazio avatar Jan 23 '15 05:01 cartazio

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.

dolio avatar Jan 23 '15 06:01 dolio

I think it might look like this. Feel free to disregard the lens import, I was just using it for testing.

http://lpaste.net/119041

glguy avatar Jan 23 '15 08:01 glguy

It would also be very useful to have mapAccumL, mapAccumR, mapAccumLM, mapAccumRM, etc.

yongqli avatar Jan 23 '15 09:01 yongqli

Cant we define the stream / bundle version efficiently of traverse efficiently though? On Jan 23, 2015 4:27 AM, "yongqli" [email protected] 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.

cartazio avatar Jan 23 '15 13:01 cartazio

Without contributing anything useful so far here, I'd benefit a lot from the mapAccum* functions for vectors.

nh2 avatar Jul 22 '15 16:07 nh2

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.

ttuegel avatar Jan 30 '16 18:01 ttuegel

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?

Daniel-Diaz avatar May 18 '16 17:05 Daniel-Diaz

@dolio now that primMonad is stackable, can we do something for mutable vectors that partially addresses this?

cartazio avatar Jul 24 '16 19:07 cartazio

BUMP

cartazio avatar Feb 03 '20 03:02 cartazio

this and some related stuff will be in the next minor and major releases both

cartazio avatar Feb 03 '20 03:02 cartazio