Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cpeikert opened this issue Jan 23, 2015 · 11 comments
Open

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

cpeikert opened this issue Jan 23, 2015 · 11 comments

Comments

@cpeikert
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

dolio commented Jan 23, 2015

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
Copy link
Member

glguy commented Jan 23, 2015

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
Copy link

yongqli commented Jan 23, 2015

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

@cartazio
Copy link
Contributor

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
#69 (comment).

@nh2
Copy link
Member

nh2 commented Jul 22, 2015

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

@ttuegel
Copy link
Member

ttuegel commented Jan 30, 2016

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
Copy link

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 cartazio added this to the 0.12 milestone Jul 24, 2016
@cartazio
Copy link
Contributor

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

@cartazio
Copy link
Contributor

cartazio commented Feb 3, 2020

BUMP

@cartazio
Copy link
Contributor

cartazio commented Feb 3, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants