diff options
author | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-09 17:57:31 +0000 |
---|---|---|
committer | Vivian McPhail <haskell.vivian.mcphail@gmail.com> | 2010-07-09 17:57:31 +0000 |
commit | 238a01e0c3b51569c28ede4259cdf33bf18eb94f (patch) | |
tree | 06e24c47b4c114f698b92d106202a97ceaa25b82 /lib/Data/Packed/Internal/Vector.hs | |
parent | c4531ceaa524b1f8a84dcdf3456d7a7b2831f902 (diff) |
vectorMapM, vectorMapM_
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 06db806..6d39c6e 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -18,6 +18,7 @@ module Data.Packed.Internal.Vector ( | |||
18 | fromList, toList, (|>), | 18 | fromList, toList, (|>), |
19 | join, (@>), safe, at, at', subVector, takesV, | 19 | join, (@>), safe, at, at', subVector, takesV, |
20 | mapVector, zipVector, unzipVectorWith, | 20 | mapVector, zipVector, unzipVectorWith, |
21 | mapVectorM, mapVectorM_, | ||
21 | foldVector, foldVectorG, foldLoop, | 22 | foldVector, foldVectorG, foldLoop, |
22 | createVector, vec, | 23 | createVector, vec, |
23 | asComplex, asReal, | 24 | asComplex, asReal, |
@@ -35,6 +36,7 @@ import Foreign.C.String | |||
35 | import Foreign.C.Types(CInt,CChar) | 36 | import Foreign.C.Types(CInt,CChar) |
36 | import Data.Complex | 37 | import Data.Complex |
37 | import Control.Monad(when) | 38 | import Control.Monad(when) |
39 | import Control.Monad.Trans | ||
38 | 40 | ||
39 | #if __GLASGOW_HASKELL__ >= 605 | 41 | #if __GLASGOW_HASKELL__ >= 605 |
40 | import GHC.ForeignPtr (mallocPlainForeignPtrBytes) | 42 | import GHC.ForeignPtr (mallocPlainForeignPtrBytes) |
@@ -357,6 +359,39 @@ foldVectorG f s0 v = foldLoop g s0 (dim v) | |||
357 | 359 | ||
358 | ------------------------------------------------------------------- | 360 | ------------------------------------------------------------------- |
359 | 361 | ||
362 | -- | monadic map over Vectors | ||
363 | mapVectorM :: (Storable a, Storable b, MonadIO m) => (a -> m b) -> Vector a -> m (Vector b) | ||
364 | mapVectorM f v = do | ||
365 | w <- liftIO $ createVector (dim v) | ||
366 | mapVectorM' f v w (dim v -1) | ||
367 | return w | ||
368 | where mapVectorM' f' v' w' 0 = do | ||
369 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 | ||
370 | y <- f' x | ||
371 | liftIO $ unsafeWith w' $ \q -> pokeElemOff q 0 y | ||
372 | mapVectorM' f' v' w' !k = do | ||
373 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k | ||
374 | y <- f' x | ||
375 | liftIO $ unsafeWith w' $ \q -> pokeElemOff q k y | ||
376 | mapVectorM' f' v' w' (k-1) | ||
377 | {-# INLINE mapVectorM #-} | ||
378 | |||
379 | -- | monadic map over Vectors | ||
380 | mapVectorM_ :: (Storable a, MonadIO m) => (a -> m ()) -> Vector a -> m () | ||
381 | mapVectorM_ f v = do | ||
382 | mapVectorM' f v (dim v -1) | ||
383 | where mapVectorM' f' v' 0 = do | ||
384 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p 0 | ||
385 | f' x | ||
386 | mapVectorM' f' v' !k = do | ||
387 | x <- liftIO $ unsafeWith v' $ \p -> peekElemOff p k | ||
388 | _ <- f' x | ||
389 | mapVectorM' f' v' (k-1) | ||
390 | {-# INLINE mapVectorM_ #-} | ||
391 | |||
392 | ------------------------------------------------------------------- | ||
393 | |||
394 | |||
360 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). | 395 | -- | Loads a vector from an ASCII file (the number of elements must be known in advance). |
361 | fscanfVector :: FilePath -> Int -> IO (Vector Double) | 396 | fscanfVector :: FilePath -> Int -> IO (Vector Double) |
362 | fscanfVector filename n = do | 397 | fscanfVector filename n = do |
@@ -398,3 +433,4 @@ fwriteVector filename v = do | |||
398 | free charname | 433 | free charname |
399 | 434 | ||
400 | foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV | 435 | foreign import ccall "vector_fwrite" gsl_vector_fwrite :: Ptr CChar -> TV |
436 | |||