]> git.immae.eu Git - github/fretlink/text-pipes.git/blame - examples/lines_url.hs
detritus
[github/fretlink/text-pipes.git] / examples / lines_url.hs
CommitLineData
955edd33 1{-# LANGUAGE OverloadedStrings #-}
2-- https://gist.github.com/michaelt/88e1fac12876857deefe
3-- following
4-- https://gist.github.com/gelisam/c769d186493221d7ebbe and associated controversy.
5
6module Main where
7
8import Prelude hiding (lines)
9import Lens.Family
10import Pipes
11import Pipes.Group
12import Pipes.HTTP
13import Pipes.Text
14import Pipes.Text.Encoding
15import Pipes.Text.IO (toHandle,stdout)
16import qualified System.IO as IO
17import Data.Functor (void)
18import qualified Data.Text as T
19
20main = 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 $
5e387e52 26 number_lines_of (responseBody resp ^. utf8 . lines) >-> toHandle IO.stdout
955edd33 27
5e387e52 28number_lines_of :: Monad m => FreeT (Producer Text m) m r -> Producer Text m r
955edd33 29number_lines_of = number_loop (1 :: Int) where
5e387e52 30 number_loop n free = do
31 fproducer <- lift $ runFreeT free
32 case fproducer of
33 Pure badbytes -> do
34 yield $ T.pack "\n"
35 return badbytes -- these could be inspected ...
36 Free p -> do
37 yield $ T.pack ("\n" ++ show n ++ " ")
38 next_free <- p
39 number_loop (n+1) next_free