You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed a reoccurring help function across several applications that I've written, and I think it would be nice to have these in vector itself:
traverse_ ::
(a -> m b)
-> MVector (PrimState m) a
-> m ()
foldM :: -- Should probably be named foldlM instead as in Data.Foldable, but vector uses foldM elsewhere
(b -> a -> m b)
-> b
-> MVector (PrimState m) a
-> m b
The implementations are both straightforward, and I'm happy to implement this if others find it worthwhile. There is the possibility of a traverse that either (a) mutates the vector in-place or (b) creates a new mutable vector. There's more room to disagree there, so if anyone is interested in that, I think that discussing it on a separate issue would be better.
mapM_ and forM_ instead of traverse_, since the latter name would not be consistent with the pure versions
imapM_ and iforM_
foldlM_ and foldrM_
ifoldlM_ and ifoldrM_
With respect to traverse that does mutation, I think I'd vote for a version that mutates the same vector in place. This of course would make the mapping function to produce elements of the same type:
traverse::
(a->ma)
->MVector (PrimStatem) a->m()
Which means it would be slightly different from traverse, as such maybe we should come up with a good new name for such function.
A version that creates a new vector isn't as useful, since you can already just as easily achieve it with: unsafeFreeze >=> mapM f >=> unsafeThaw. I think there are rewrite rules that will ensure that mapM will get specialized to PrimMonad making it efficient. But, as you said we can discuss this part more in a separate ticket.
Activity
lehins commentedon Jul 20, 2020
I am definitely in favor of all or some these:
mapM_andforM_instead oftraverse_, since the latter name would not be consistent with the pure versionsimapM_andiforM_foldlM_andfoldrM_ifoldlM_andifoldrM_With respect to
traversethat does mutation, I think I'd vote for a version that mutates the same vector in place. This of course would make the mapping function to produce elements of the same type:Which means it would be slightly different from
traverse, as such maybe we should come up with a good new name for such function.A version that creates a new vector isn't as useful, since you can already just as easily achieve it with:
unsafeFreeze >=> mapM f >=> unsafeThaw. I think there are rewrite rules that will ensure thatmapMwill get specialized toPrimMonadmaking it efficient. But, as you said we can discuss this part more in a separate ticket.leftaroundabout commentedon Jul 21, 2020
Agree with the mutable traversal. This should probably be called something like
modifyTraverseortraverseModify.Incidentally, maybe it would also make sense to have
And then, by extrapolation, the traversal would be called
Shimuuar commentedon Jul 21, 2020
+1. We have several issues asking to extend API for mutable vectors
Shimuuar commentedon Oct 21, 2020
Closed in favor of #334