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 --- examples/vector-map.hs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 examples/vector-map.hs (limited to 'examples') diff --git a/examples/vector-map.hs b/examples/vector-map.hs new file mode 100644 index 0000000..d778358 --- /dev/null +++ b/examples/vector-map.hs @@ -0,0 +1,42 @@ +-- use of vectorMapM +-- + +------------------------------------------- + +import Data.Packed.Vector +import Numeric.LinearAlgebra.Interface + +import Control.Monad.State +import Control.Monad.Trans + +------------------------------------------- + +-- an instance of MonadIO, a monad transformer +type VectorMonadT = StateT Int IO + +v :: Vector Int +v = fromList $ take 10 [0..] + +test1 :: Vector Int -> IO (Vector Int) +test1 = do + mapVectorM (\x -> do + putStr $ (show) x ++ " " + return (x + 1)) + +-- we can have an arbitrary monad AND do IO +addInitialM :: Vector Int -> VectorMonadT () +addInitialM = mapVectorM_ (\x -> do + i <- get + liftIO $ putStr $ (show $ x + i) ++ " " + put $ x + i + ) + +------------------------------------------- +main = do + v' <- test1 v + putStrLn "" + putStrLn $ show v' + evalStateT (addInitialM v) 1 + putStrLn "" + return () + -- cgit v1.2.3