diff options
Diffstat (limited to 'examples/lines_url.hs')
-rw-r--r-- | examples/lines_url.hs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/examples/lines_url.hs b/examples/lines_url.hs new file mode 100644 index 0000000..b676656 --- /dev/null +++ b/examples/lines_url.hs | |||
@@ -0,0 +1,37 @@ | |||
1 | {-# LANGUAGE OverloadedStrings #-} | ||
2 | -- https://gist.github.com/michaelt/88e1fac12876857deefe | ||
3 | -- following | ||
4 | -- https://gist.github.com/gelisam/c769d186493221d7ebbe and associated controversy. | ||
5 | |||
6 | module Main where | ||
7 | |||
8 | import Prelude hiding (lines) | ||
9 | import Lens.Family | ||
10 | import Pipes | ||
11 | import Pipes.Group | ||
12 | import Pipes.HTTP | ||
13 | import Pipes.Text | ||
14 | import Pipes.Text.Encoding | ||
15 | import Pipes.Text.IO (toHandle,stdout) | ||
16 | import qualified System.IO as IO | ||
17 | import Data.Functor (void) | ||
18 | import qualified Data.Text as T | ||
19 | |||
20 | main = do | ||
21 | req <- parseUrl "https://gist.github.com/gelisam/c769d186493221d7ebbe" | ||
22 | -- "http://www.example.com" | ||
23 | -- "http://www.gutenberg.org/files/10/10-h/10-h.htm" | ||
24 | withManager tlsManagerSettings $ \m -> | ||
25 | withHTTP req m $ \resp -> void $ runEffect $ | ||
26 | number_lines_of (responseBody resp ^. utf8 . lines) >-> toHandle IO.stdout | ||
27 | |||
28 | number_lines_of :: Monad m => FreeT (Producer Text m) m bad -> Producer Text m bad | ||
29 | number_lines_of = number_loop (1 :: Int) where | ||
30 | number_loop n freeProducers = do | ||
31 | freeProducer <- lift $ runFreeT freeProducers | ||
32 | case freeProducer of | ||
33 | Pure badbytes -> do yield $ T.pack "\n" | ||
34 | return badbytes -- these could be inspected ... | ||
35 | Free p -> do yield $ T.pack ("\n" ++ show n ++ " ") | ||
36 | nextFreeProducers <- p | ||
37 | number_loop (n+1) nextFreeProducers | ||