on master help sign in
Add fromHandle/toHandle
cde56cfHarendra Kumar
src/Asyncly/Prelude.hs+39−12
@@ -21,6 +21,7 @@ module Asyncly.Prelude
21 (
21 (
22 -- * Construction
22 -- * Construction
23 unfoldr
23 unfoldr
24+ , fromHandle
24
25
25 -- * Elimination
26 -- * Elimination
26 , foldr
27 , foldr
@@ -28,6 +29,7 @@ module Asyncly.Prelude
28 , foldl
29 , foldl
29 , foldlM
30 , foldlM
30 , uncons
31 , uncons
32+ , toHandle
31
33
32 -- * Special folds
34 -- * Special folds
33 , toList
35 , toList
@@ -46,12 +48,41 @@ module Asyncly.Prelude
46where
48where
47
49
48import Control.Monad (liftM)
50import Control.Monad (liftM)
51+import Control.Monad.IO.Class (MonadIO(..))
49import Data.Semigroup (Semigroup(..))
52import Data.Semigroup (Semigroup(..))
50import Prelude hiding (filter, drop, dropWhile, take,
53import Prelude hiding (filter, drop, dropWhile, take,
51 takeWhile, zipWith, foldr, foldl)
54 takeWhile, zipWith, foldr, foldl)
55+import qualified System.IO as IO
56+
52import Asyncly.Core
57import Asyncly.Core
53import Asyncly.Streams
58import Asyncly.Streams
54
59
60+------------------------------------------------------------------------------
61+-- Construction
62+------------------------------------------------------------------------------
63+
64+-- | Build a Stream by unfolding steps starting from a seed.
65+unfoldr :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
66+unfoldr step = fromStream . go
67+ where
68+ go s = Stream $ \_ stp yld -> do
69+ mayb <- step s
70+ case mayb of
71+ Nothing -> stp
72+ Just (a, b) -> yld a (Just (go b))
73+
74+-- | Read lines from an IO Handle into a stream of Strings.
75+fromHandle :: (MonadIO m, Streaming t) => IO.Handle -> t m String
76+fromHandle h = fromStream $ go
77+ where
78+ go = Stream $ \_ stp yld -> do
79+ eof <- liftIO $ IO.hIsEOF h
80+ if eof
81+ then stp
82+ else do
83+ str <- liftIO $ IO.hGetLine h
84+ yld str (Just go)
85+
55------------------------------------------------------------------------------
86------------------------------------------------------------------------------
56-- Elimination
87-- Elimination
57------------------------------------------------------------------------------
88------------------------------------------------------------------------------
@@ -111,19 +142,15 @@ uncons m =
111 yield a (Just x) = return (Just (a, (fromStream x)))
142 yield a (Just x) = return (Just (a, (fromStream x)))
112 in (runStream (toStream m)) Nothing stop yield
143 in (runStream (toStream m)) Nothing stop yield
113
144
114−------------------------------------------------------------------------------
145+-- | Write a stream of Strings to an IO Handle.
115−-- Construction
146+toHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String -> m ()
116−------------------------------------------------------------------------------
147+toHandle h m = go (toStream m)
117−
118−-- | Build a Stream by unfolding steps starting from a seed.
119−unfoldr :: (Streaming t, Monad m) => (b -> m (Maybe (a, b))) -> b -> t m a
120−unfoldr step = fromStream . go
121 where
148 where
122− go s = Stream $ \_ stp yld -> do
149+ go m1 =
123− mayb <- step s
150+ let stop = return ()
124− case mayb of
151+ yield a Nothing = liftIO (IO.hPutStrLn h a)
125− Nothing -> stp
152+ yield a (Just x) = liftIO (IO.hPutStrLn h a) >> go x
126− Just (a, b) -> yld a (Just (go b))
153+ in (runStream m1) Nothing stop yield
127
154
128------------------------------------------------------------------------------
155------------------------------------------------------------------------------
129-- Special folds
156-- Special folds