Fusion rules for in-place map
[darcs-mirrors/vector.git] / Data / Vector / MVector / Mut.hs
1 {-# LANGUAGE Rank2Types #-}
2
3 #include "phases.h"
4
5 module Data.Vector.MVector.Mut (
6 Mut(..), run, unstream, map, update
7 ) where
8
9 import qualified Data.Vector.MVector as MVector
10 import Data.Vector.MVector ( MVector )
11
12 import Data.Vector.Stream ( Stream )
13
14 import Prelude hiding ( map )
15
16 data Mut a = Mut (forall m mv. MVector mv m a => m (mv a))
17
18 run :: MVector mv m a => Mut a -> m (mv a)
19 {-# INLINE run #-}
20 run (Mut p) = p
21
22 unstream :: Stream a -> Mut a
23 {-# INLINE_STREAM unstream #-}
24 unstream s = Mut (MVector.unstream s)
25
26 map :: (a -> a) -> Mut a -> Mut a
27 {-# INLINE_STREAM map #-}
28 map f (Mut p) = Mut (do { v <- p; MVector.map f v; return v })
29
30 update :: Mut a -> Stream (Int, a) -> Mut a
31 {-# INLINE_STREAM update #-}
32 update (Mut p) s = Mut (do { v <- p; MVector.update v s; return v })
33