Skip to content

Enrich mutable API for vectors #334

@Shimuuar

Description

@Shimuuar

There were quite a few proposal to enrich API of mutable vectors: #326, #203, #64, #175. Tris issue tries to collect all proposals from them in one nice list (and adds few on top of them)

Folds & lookup

  • foldlM :: (b -> a -> m b) -> b -> v s a -> m b
  • foldrM :: (a -> b -> m b) -> b -> v s a -> m b
  • mapM_ :: (a -> m ()) -> v s a -> m () / forM_
  • imapM_ :: (Int -> a -> m ()) -> v s a -> m () / forM_
  • ifoldlM / ifoldrM
  • elemIndex

I'm not sure that foldlM_ & friends are worth adding. It's easy to ignore return result of fold anyway: void, _ <- .... It however could be useful to add pure variants:

  • foldl :: (b -> a -> b) -> b -> v s a -> m b
  • foldr :: (a -> b -> b) -> b -> v s a -> m b

They come with performance bonus. We can perform entire fold in the ST monad and only lift to m at last moment. This way GHC only have to use bind from ST monad and it's good at optimizing it

In place mutation

Naming could be slightly confusing here. Should in place map be called mapM or mapM_. Underscore usually means throwing away results of function and mapM builds new structure. I'm slightly lean towards mapM since it's in-place map

  • mapM :: (a -> m a) -> v s a -> m () / forM
  • map :: (a -> a) -> v s a -> m () / for
  • imap :: (Int -> a -> a) -> v s a -> m () / ifor
  • imapM :: (Int -> a -> m a) -> v s a -> m () / iforM
  • modifyM :: v s a -> (a -> m a) -> Int -> m ()
  • previousPermutation :: (PrimMonad m, Ord e, MVector v e) => v (PrimState m) e -> m Bool (to complement nextPermutation)
  • in-place reverse

Creation

  • generate :: Int -> (Int -> a) -> m (v s a)
  • generateM :: Int -> (Int -> m a) -> m (v s a)

Comments? Objections? Proposals?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions