diff options
Diffstat (limited to 'examples/vector-map.hs')
-rw-r--r-- | examples/vector-map.hs | 42 |
1 files changed, 42 insertions, 0 deletions
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 @@ | |||
1 | -- use of vectorMapM | ||
2 | -- | ||
3 | |||
4 | ------------------------------------------- | ||
5 | |||
6 | import Data.Packed.Vector | ||
7 | import Numeric.LinearAlgebra.Interface | ||
8 | |||
9 | import Control.Monad.State | ||
10 | import Control.Monad.Trans | ||
11 | |||
12 | ------------------------------------------- | ||
13 | |||
14 | -- an instance of MonadIO, a monad transformer | ||
15 | type VectorMonadT = StateT Int IO | ||
16 | |||
17 | v :: Vector Int | ||
18 | v = fromList $ take 10 [0..] | ||
19 | |||
20 | test1 :: Vector Int -> IO (Vector Int) | ||
21 | test1 = do | ||
22 | mapVectorM (\x -> do | ||
23 | putStr $ (show) x ++ " " | ||
24 | return (x + 1)) | ||
25 | |||
26 | -- we can have an arbitrary monad AND do IO | ||
27 | addInitialM :: Vector Int -> VectorMonadT () | ||
28 | addInitialM = mapVectorM_ (\x -> do | ||
29 | i <- get | ||
30 | liftIO $ putStr $ (show $ x + i) ++ " " | ||
31 | put $ x + i | ||
32 | ) | ||
33 | |||
34 | ------------------------------------------- | ||
35 | main = do | ||
36 | v' <- test1 v | ||
37 | putStrLn "" | ||
38 | putStrLn $ show v' | ||
39 | evalStateT (addInitialM v) 1 | ||
40 | putStrLn "" | ||
41 | return () | ||
42 | |||