Skip to content

Instantly share code, notes, and snippets.

@danidiaz
Last active August 27, 2017 21:26
Show Gist options
  • Select an option

  • Save danidiaz/7fb8513d6aaa20f6cc48bcdff9f167a2 to your computer and use it in GitHub Desktop.

Select an option

Save danidiaz/7fb8513d6aaa20f6cc48bcdff9f167a2 to your computer and use it in GitHub Desktop.
Yet another pipes text example
name: pipetastic
version: 0.1.0.0
build-type: Simple
cabal-version: >= 2
executable pipetastic
main-is: Main.hs
build-depends: base >= 4.10 && < 4.11 ,
microlens >= 0.4.8.0 ,
text ,
foldl >= 1.3.0 ,
pipes >= 4.3.4 ,
pipes-group >= 1.0.7 ,
pipes-bytestring >= 2.1.6,
pipes-text >= 0.0.2.5
default-language: Haskell2010
module Main where
import Pipes
import qualified Pipes.Prelude as P
import qualified Pipes.Group as PG
import qualified Pipes.ByteString as PB
import qualified Pipes.Text as PT
import Pipes.Text.Encoding (utf8)
import qualified Control.Foldl as L
import qualified Control.Foldl.Text as LT
import Control.Monad (unless)
import Data.Text.Lazy (Text)
import Lens.Micro.Extras (view)
test :: [Text] -> Bool
test _ = True
main :: IO ()
main = do
result <- next -- peek one value
. (\producer -> producer >-> P.dropWhile (not . test))
. L.purely PG.folds L.list -- collect streamed groups of lines
. view (PG.chunksOf 3)
. L.purely PG.folds LT.lazy -- convert streamed text lines to lazy text
. view (utf8 . PT.lines)
$ PB.stdin
case result of
Left leftovers -> -- no failures, potentially a stream of leftovers
do isNull <- P.null leftovers
unless isNull $ print "Decoding error."
Right (e,_) -> -- some group failed
do print "Test failed for group:"
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment