on master help sign in
Add fromHandle/toHandle
commitcde56cf6a44a64adf4cbb5097328f4eb2e4da183 authorHarendra Kumar <harendra.kumar@gmail.com> dateSat 28 Oct 2017, 17:09 (9 years ago) parente7d60a8
1 file changed+39−12
src/Asyncly/Prelude.hs+39−12
src/Asyncly/Prelude.hs+39−12
@@ -21,6 +21,7 @@ module Asyncly.Prelude
2121 (
2222 -- * Construction
2323 unfoldr
24+ , fromHandle
2425
2526 -- * Elimination
2627 , foldr
@@ -28,6 +29,7 @@ module Asyncly.Prelude
2829 , foldl
2930 , foldlM
3031 , uncons
32+ , toHandle
3133
3234 -- * Special folds
3335 , toList
@@ -46,12 +48,41 @@ module Asyncly.Prelude
4648where
4749
4850import Control.Monad (liftM)
51+import Control.Monad.IO.Class (MonadIO(..))
4952import Data.Semigroup (Semigroup(..))
5053import Prelude hiding (filter, drop, dropWhile, take,
5154 takeWhile, zipWith, foldr, foldl)
55+import qualified System.IO as IO
56+
5257import Asyncly.Core
5358import Asyncly.Streams
5459
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+
5586------------------------------------------------------------------------------
5687-- Elimination
5788------------------------------------------------------------------------------
@@ -111,19 +142,15 @@ uncons m =
111142 yield a (Just x) = return (Just (a, (fromStream x)))
112143 in (runStream (toStream m)) Nothing stop yield
113144
114−------------------------------------------------------------------------------
115−-- Construction
116−------------------------------------------------------------------------------
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
145+-- | Write a stream of Strings to an IO Handle.
146+toHandle :: (Streaming t, MonadIO m) => IO.Handle -> t m String -> m ()
147+toHandle h m = go (toStream m)
121148 where
122− go s = Stream $ \_ stp yld -> do
123− mayb <- step s
124− case mayb of
125− Nothing -> stp
126− Just (a, b) -> yld a (Just (go b))
149+ go m1 =
150+ let stop = return ()
151+ yield a Nothing = liftIO (IO.hPutStrLn h a)
152+ yield a (Just x) = liftIO (IO.hPutStrLn h a) >> go x
153+ in (runStream m1) Nothing stop yield
127154
128155------------------------------------------------------------------------------
129156-- Special folds