From c104df60266d5e0bc94e5b0a7eedc1d949975fc1 Mon Sep 17 00:00:00 2001 From: Vivian McPhail Date: Tue, 13 Jul 2010 22:59:22 +0000 Subject: fix mapVectorM(_) and add example --- lib/Data/Packed/Internal/Vector.hs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'lib/Data') diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 7dd1289..ec2bf3c 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs @@ -365,30 +365,32 @@ foldVectorG f s0 v = foldLoop g s0 (dim v) mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b) mapVectorM f v = do w <- liftIO $ createVector (dim v) - mapVectorM' f v w (dim v -1) + mapVectorM' f v w 0 (dim v -1) return w - where mapVectorM' f' v' w' 0 = do - x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 - y <- f' x - liftIO $ unsafeWith w' $ \q -> pokeElemOff q 0 y - mapVectorM' f' v' w' !k = do - x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k - y <- f' x - liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y - mapVectorM' f' v' w' (k-1) + where mapVectorM' f' v' w' !k !t + | k == t = do + x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k + y <- f' x + liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y + | otherwise = do + x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k + y <- f' x + liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y + mapVectorM' f' v' w' (k+1) t {-# INLINE mapVectorM #-} -- | monadic map over Vectors mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m () mapVectorM_ f v = do - mapVectorM' f v (dim v -1) - where mapVectorM' f' v' 0 = do - x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 - f' x - mapVectorM' f' v' !k = do - x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k - _ <- f' x - mapVectorM' f' v' (k-1) + mapVectorM' f v 0 (dim v -1) + where mapVectorM' f' v' !k !t + | k == t = do + x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k + f' x + | otherwise = do + x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k + _ <- f' x + mapVectorM' f' v' (k+1) t {-# INLINE mapVectorM_ #-} ------------------------------------------------------------------- -- cgit v1.2.3