vector icon indicating copy to clipboard operation
vector copied to clipboard

Add note about sorting to documentation (and possibly a transform function)

Open typesanitizer opened this issue 7 years ago • 5 comments

Continuing from #157 - sorting algorithms are available in vector-algorithm, but it isn't easy to find that information out, depending on how you search. For example, the Stackage Hoogle query for sort :: Ord a => Vector a -> Vector a doesn't come up with the result (because that package doesn't provide a wrapper for immutable vectors).

It would be nice if a note about this could be added to the top-level module Data.Vector, which serves as the "information hub" right now. Something along the lines of:

Note: Sorting algorithms for mutable vectors are available in the vector-algorithms package. If you want to sort an immutable vector, you can use the freeze and thaw functions to wrap calls to sorting functions of your choice.

Personally, I think it would be nice to have a helper function alongside

Data.Vector.Generic.transform 
  :: (PrimMonad m, Vector v a) 
  => (Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)) 
  -> v a
  -> m (v a)
transform f = unsafeFreeze <=< f <=< thaw

as an counterpart to Data.Vector.Generic.Mutable.transform (the signatures aren't exactly analogous though because of the extra m :slightly_frowning_face:), but of course, it isn't strictly necessary, just a nice to have thing.

typesanitizer avatar Dec 13 '18 21:12 typesanitizer

Hrmm. Interesting idea. On the indexing front adding that to vector algorithms might be a better strategy. Though it’s improtant to note that for many workloads,’that extra allocation for the immutable flavor might be a problem.

I’ll have to think about this.

On Thu, Dec 13, 2018 at 4:08 PM Varun Gandhi [email protected] wrote:

Continuing from #157 https://github.com/haskell/vector/issues/157 - sorting algorithms are available in vector-algorithm, but it isn't easy to find that information out, depending on how you search. For example, the Stackage Hoogle query for sort :: Ord a => Vector a -> Vector a https://www.stackage.org/lts-12.22/hoogle?q=sort+%3A%3A+Ord+a+%3D%3E+Vector+a+-%3E+Vector+a doesn't come up with the result (because that package doesn't provide a wrapper for immutable vectors).

It would be nice if a note about this could be added to the top-level module Data.Vector, which serves as the "information hub" right now. Something along the lines of:

Note: Sorting algorithms for mutable vectors are available in the vector-algorithms https://hackage.haskell.org/package/vector-algorithms package. If you want to sort an immutable vector, you can use the freeze https://hackage.haskell.org/package/vector-0.12.0.2/docs/Data-Vector-Generic.html#v:freeze and thaw https://hackage.haskell.org/package/vector-0.12.0.2/docs/Data-Vector-Generic.html#v:thaw functions to wrap calls to sorting functions of your choice.

Personally, I think it would be nice to have a helper function alongside

Data.Vector.Generic.transform :: (PrimMonad m, Vector v a) => (Mutable v (PrimState m) a -> m (Mutable v (PrimState m) a)) -> v a -> m (v a) transform f = unsafeFreeze <=< f <=< thaw

as an counterpart to Data.Vector.Generic.Mutable.transform https://www.stackage.org/haddock/lts-12.9/vector-0.12.0.1/Data-Vector-Generic-Mutable.html#v:transform, but of course, it isn't strictly necessary, just a nice to have thing.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/haskell/vector/issues/232, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAQwnPnlT6PcrFuhWeIW5tbLOxetsYpks5u4sHRgaJpZM4ZSbYK .

cartazio avatar Dec 14 '18 03:12 cartazio

this idea is growing on me, but i still need to think about it :)

cartazio avatar Dec 30 '18 00:12 cartazio

Yes sort of array is well hidden and isn't very useable. Just a datapoint. statistics has helper for sorting: sort :: Vector Double -> Vector Double. In many cases convenience outweight cost of extra allocation or you have to allocate new vector anyway.

Shimuuar avatar Jan 02 '19 15:01 Shimuuar

I'd go a step further and suggest adding a default opinionated and pure sort directly to the vector package, with the documentation for this pure sort directing people to the vector-algorithms package if they are exceedingly performance conscious

Gabriella439 avatar May 17 '19 17:05 Gabriella439

i'll have a look see, i do agree this might be nice, but want it to be well done :)

cartazio avatar May 17 '19 17:05 cartazio