Skip to content

Commit 2f2175b

Browse files
chessaiandrewthad
authored andcommitted
re-introduce readFile, writeFile
1 parent 721e235 commit 2f2175b

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/Streaming/Prelude.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ module Streaming.Prelude (
7070
, stdinLn
7171
, readLn
7272
, fromHandle
73+
, readFile
7374
, iterate
7475
, iterateM
7576
, repeat
@@ -91,6 +92,7 @@ module Streaming.Prelude (
9192
, mapM_
9293
, print
9394
, toHandle
95+
, writeFile
9496
, effects
9597
, erase
9698
, drained
@@ -2365,7 +2367,33 @@ stdoutLn = loop
23652367
Right () -> loop rest
23662368
{-# INLINABLE stdoutLn #-}
23672369

2370+
{-| Read the lines of a file, using a function of the type: \'@'Stream' ('Of' 'String') 'IO' () -> 'IO' a@\'
2371+
to turn the stream into a value of type \''IO' a\'.
23682372
2373+
>>> S.writeFile "lines.txt" $ S.take 2 S.stdinLn
2374+
hello<Enter>
2375+
world<Enter>
2376+
>>> S.print $ S.readFile "lines.txt"
2377+
"hello"
2378+
"world"
2379+
2380+
-}
2381+
readFile :: FilePath -> (Stream (Of String) IO () -> IO a) -> IO a
2382+
readFile f s = IO.withFile f IO.ReadMode $ \h -> s (fromHandle h)
2383+
2384+
{-| Write a series of 'String's as lines to a file.
2385+
2386+
>>> S.writeFile "lines.txt" $ S.take 2 S.stdinLn
2387+
hello<Enter>
2388+
world<Enter>
2389+
2390+
>>> S.stdoutLn $ S.readFile "lines.txt"
2391+
hello
2392+
world
2393+
2394+
-}
2395+
writeFile :: FilePath -> Stream (Of String) IO r -> IO r
2396+
writeFile f = IO.withFile f IO.WriteMode . flip toHandle
23692397

23702398
{-| Write 'String's to 'IO.stdout' using 'putStrLn'
23712399

0 commit comments

Comments
 (0)