haskell / vector

An efficient implementation of Int-indexed arrays (both mutable and immutable), with a powerful loop optimisation framework .
Other
366 stars 139 forks source link

add generic traverse method, a la Data.Traversable #69

Open cpeikert opened 9 years ago

cpeikert commented 9 years ago

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.

cartazio commented 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)

dolio commented 9 years ago

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.

glguy commented 9 years ago

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

yongqli commented 9 years ago

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

cartazio commented 9 years ago

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.

nh2 commented 9 years ago

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

ttuegel commented 8 years ago

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.

Daniel-Diaz commented 8 years ago

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?

cartazio commented 8 years ago

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

cartazio commented 4 years ago

BUMP

cartazio commented 4 years ago

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