@@ -21,6 +21,7 @@ module Asyncly.Prelude
22 -- * Construction
22 -- * Construction
25 -- * Elimination
26 -- * Elimination
@@ -28,6 +29,7 @@ module Asyncly.Prelude
32 -- * Special folds
34 -- * Special folds
@@ -46,12 +48,41 @@ module Asyncly.Prelude
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
52import Asyncly.Core
57import Asyncly.Core
53import Asyncly.Streams
58import Asyncly.Streams
60+------------------------------------------------------------------------------
62+------------------------------------------------------------------------------
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
68+ go s = Stream $ \_ stp yld -> do
72+ Just (a, b) -> yld a (Just (go b))
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
78+ go = Stream $ \_ stp yld -> do
79+ eof <- liftIO $ IO.hIsEOF h
83+ str <- liftIO $ IO.hGetLine h
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
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)
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
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
128------------------------------------------------------------------------------
155------------------------------------------------------------------------------
129-- Special folds
156-- Special folds