Skip to content

Commit 800ca56

Browse files
committed
Make more doctests pass
1 parent a1e6c4b commit 800ca56

2 files changed

Lines changed: 42 additions & 25 deletions

File tree

src/Streaming/Internal.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ import Data.Functor.Sum
9999
import Data.Monoid (Monoid (..))
100100
import Data.Semigroup (Semigroup (..))
101101

102+
-- $setup
103+
-- >>> import Streaming.Prelude as S
104+
102105
{- $stream
103106
104107
The 'Stream' data type is equivalent to @FreeT@ and can represent any effectful

src/Streaming/Prelude.hs

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,19 @@ import Prelude hiding (map, mapM, mapM_, filter, drop, dropWhile, take, mconcat
284284
, last, foldMap)
285285

286286

287+
288+
-- $setup
289+
-- >>> import Control.Applicative
290+
-- >>> import qualified Control.Foldl as L
291+
-- >>> import Data.Bifunctor (first)
292+
-- >>> import Data.Function ((&))
293+
-- >>> import Data.IORef
294+
-- >>> import Data.Vector (Vector)
295+
-- >>> import qualified Streaming.Prelude as S
296+
-- >>> import qualified System.IO
297+
-- >>> import Text.Read (readEither)
298+
299+
287300
-- instance (Eq a) => Eq1 (Of a) where eq1 = (==)
288301
-- instance (Ord a) => Ord1 (Of a) where compare1 = compare
289302
-- instance (Read a) => Read1 (Of a) where readsPrec1 = readsPrec
@@ -460,7 +473,7 @@ break thePred = loop where
460473
and the element that breaks it will be put after the break.
461474
This function is easiest to use with 'Control.Foldl.purely'
462475
463-
>>> rest <- each [1..10] & L.purely S.breakWhen L.sum (>10) & S.print
476+
>>> rest <- each [1..10] & L.purely S.breakWhen L.sum (>10) & S.print
464477
1
465478
2
466479
3
@@ -918,8 +931,8 @@ filterM thePred = loop where
918931
{- $folds
919932
Use these to fold the elements of a 'Stream'.
920933
921-
>>> S.fold_ (+) 0 id $ S.each [1..0]
922-
50
934+
>>> S.fold_ (+) 0 id $ S.each [1..10]
935+
55
923936
924937
The general folds 'fold', fold_', 'foldM' and 'foldM_' are arranged
925938
for use with @Control.Foldl@ 'Control.Foldl.purely' and 'Control.Foldl.impurely'
@@ -1028,7 +1041,7 @@ foldM_ step begin done = fmap (\(a :> _) -> a) . foldM step begin done
10281041
Thus to accumulate the elements of a stream as a vector, together with a random
10291042
element we might write:
10301043
1031-
>>> L.impurely S.foldM (liftA2 (,) L.vector L.random) $ each [1..10::Int] :: IO (Of (U.Vector Int,Maybe Int) ())
1044+
>>> L.impurely S.foldM (liftA2 (,) L.vectorM L.random) $ each [1..10::Int] :: IO (Of (Vector Int, Maybe Int) ())
10321045
([1,2,3,4,5,6,7,8,9,10],Just 9) :> ()
10331046
10341047
-}
@@ -1256,7 +1269,7 @@ last_ = loop Nothing_ where
12561269

12571270
{-| Run a stream, remembering only its length:
12581271
1259-
>>> S.length $ S.each [1..10]
1272+
>>> runIdentity $ S.length_ (S.each [1..10] :: Stream (Of Int) Identity ())
12601273
10
12611274
12621275
-}
@@ -1407,7 +1420,7 @@ foldMap_ f = fold_ (\ !acc a -> mappend acc (f a)) mempty id
14071420

14081421
{-| Fold streamed items into their monoidal sum
14091422
1410-
>>> S.mconcat $ S.take 2 $ S.map (Data.Monoid.Last . Just) (S.stdinLn)
1423+
>>> S.mconcat $ S.take 2 $ S.map (Data.Monoid.Last . Just) S.stdinLn
14111424
first<Enter>
14121425
last<Enter>
14131426
Last {getLast = Just "last"} :> ()
@@ -1648,7 +1661,7 @@ repeat a = loop where loop = Effect (return (Step (a :> loop)))
16481661

16491662
{-| Repeat a monadic action /ad inf./, streaming its results.
16501663
1651-
>>> S.toList $ S.take 2 $ repeatM getLine
1664+
>>> S.toList $ S.take 2 $ repeatM getLine
16521665
one<Enter>
16531666
two<Enter>
16541667
["one","two"]
@@ -1742,13 +1755,13 @@ scan step begin done str = Step (done begin :> loop begin str)
17421755
'FoldM's from @Control.Foldl@ using 'impurely'. Here we yield
17431756
a succession of vectors each recording
17441757
1745-
>>> let v = L.impurely scanM L.vector $ each [1..4::Int] :: Stream (Of (U.Vector Int)) IO ()
1758+
>>> let v = L.impurely scanM L.vectorM $ each [1..4::Int] :: Stream (Of (Vector Int)) IO ()
17461759
>>> S.print v
1747-
fromList []
1748-
fromList [1]
1749-
fromList [1,2]
1750-
fromList [1,2,3]
1751-
fromList [1,2,3,4]
1760+
[]
1761+
[1]
1762+
[1,2]
1763+
[1,2,3]
1764+
[1,2,3,4]
17521765
17531766
-}
17541767
scanM :: Monad m => (x -> a -> m x) -> m x -> (x -> m b) -> Stream (Of a) m r -> Stream (Of b) m r
@@ -1880,15 +1893,16 @@ sum_ = fold_ (+) 0 id
18801893
55 :> ()
18811894
18821895
>>> (n :> rest) <- S.sum $ S.splitAt 3 $ each [1..10]
1883-
>>> print n
1896+
>>> System.IO.print n
18841897
6
18851898
>>> (m :> rest') <- S.sum $ S.splitAt 3 rest
1886-
>>> print m
1899+
>>> System.IO.print m
18871900
15
18881901
>>> S.print rest'
18891902
7
18901903
8
18911904
9
1905+
10
18921906
18931907
-}
18941908
sum :: (Monad m, Num a) => Stream (Of a) m r -> m (Of a r)
@@ -2059,6 +2073,7 @@ toList_ = fold_ (\diff a ls -> diff (a: ls)) id (\diff -> diff [])
20592073
[1,2,3]
20602074
[4,5,6]
20612075
[7,8,9]
2076+
20622077
>>> S.print $ mapped S.toList $ chunksOf 2 $ S.replicateM 4 getLine
20632078
s<Enter>
20642079
t<Enter>
@@ -2094,7 +2109,7 @@ uncons = loop where
20942109
The seed can of course be anything, but this is one natural way
20952110
to consume a @pipes@ 'Pipes.Producer'. Consider:
20962111
2097-
>>> S.stdoutLn $ S.take 2 $ S.unfoldr Pipes.next Pipes.stdinLn
2112+
> S.stdoutLn $ S.take 2 $ S.unfoldr Pipes.next Pipes.stdinLn
20982113
hello<Enter>
20992114
hello
21002115
goodbye<Enter>
@@ -2149,7 +2164,7 @@ untilRight act = Effect loop where
21492164
> with = flip subst
21502165
> subst = flip with
21512166
2152-
>>> with (each [1..3]) (yield . show) & intercalates (yield "--") & S.stdoutLn
2167+
>>> with (each [1..3]) (yield . Prelude.show) & intercalates (yield "--") & S.stdoutLn
21532168
1
21542169
--
21552170
2
@@ -2174,7 +2189,7 @@ with s f = loop s where
21742189
hello
21752190
21762191
>>> S.sum $ do {yield 1; yield 2; yield 3}
2177-
6
2192+
6 :> ()
21782193
21792194
>>> let number = lift (putStrLn "Enter a number:") >> lift readLn >>= yield :: Stream (Of Int) IO ()
21802195
>>> S.toList $ do {number; number; number}
@@ -2312,7 +2327,7 @@ readLn = loop where
23122327
23132328
Terminates on end of input
23142329
2315-
>>> IO.withFile "/usr/share/dict/words" IO.ReadMode $ S.stdoutLn . S.take 3 . S.drop 50000 . S.fromHandle
2330+
>>> IO.withFile "/usr/share/dict/words" IO.ReadMode $ S.stdoutLn . S.take 3 . S.drop 50000 . S.fromHandle
23162331
deflagrator
23172332
deflate
23182333
deflation
@@ -2353,7 +2368,6 @@ hello<Enter>
23532368
"hello"
23542369
world<Enter>
23552370
"world"
2356-
>>>
23572371
23582372
-}
23592373
print :: (MonadIO m, Show a) => Stream (Of a) m r -> m r
@@ -2561,7 +2575,7 @@ sumToCompose x = case x of
25612575
simultaneously, and in constant memory -- as they would be if,
25622576
say, you linked them together with @Control.Fold@:
25632577
2564-
>>> L.impurely S.foldM (liftA3 (\a b c -> (b,c)) (L.sink print) (L.generalize L.sum) (L.generalize L.product)) $ each [1..4]
2578+
>>> L.impurely S.foldM (liftA3 (\a b c -> (b, c)) (L.sink Prelude.print) (L.generalize L.sum) (L.generalize L.product)) $ each [1..4]
25652579
1
25662580
2
25672581
3
@@ -2632,10 +2646,10 @@ two
26322646
With copy, I can do these simultaneously:
26332647
26342648
>>> S.print $ S.stdoutLn $ S.copy $ each ["one","two"]
2635-
one
26362649
"one"
2637-
two
2650+
one
26382651
"two"
2652+
two
26392653
26402654
'copy' should be understood together with 'effects' and is subject to the rules
26412655
@@ -2661,7 +2675,7 @@ two
26612675
using 'Control.Foldl.handles' on an appropriate lens. Some such
26622676
manipulations are simpler and more 'Data.List'-like, using 'copy':
26632677
2664-
>>> L.purely S.fold (liftA2 (,) (L.handles (filtered odd) L.sum) (L.handles (filtered even) L.product)) $ each [1..10]
2678+
>>> L.purely S.fold (liftA2 (,) (L.handles (L.filtered odd) L.sum) (L.handles (L.filtered even) L.product)) $ each [1..10]
26652679
(25,3840) :> ()
26662680
26672681
becomes
@@ -2730,7 +2744,7 @@ duplicate = copy
27302744
This @unzip@ does
27312745
stream, though of course you can spoil this by using e.g. 'toList':
27322746
2733-
>>> let xs = map (\x-> (x,show x)) [1..5::Int]
2747+
>>> let xs = Prelude.map (\x -> (x, Prelude.show x)) [1..5 :: Int]
27342748
27352749
>>> S.toList $ S.toList $ S.unzip (S.each xs)
27362750
["1","2","3","4","5"] :> ([1,2,3,4,5] :> ())

0 commit comments

Comments
 (0)