Skip to content

Instantly share code, notes, and snippets.

@zouppen
Created January 29, 2015 21:50
Show Gist options
  • Save zouppen/73f65d941989b1e02ba1 to your computer and use it in GitHub Desktop.
Save zouppen/73f65d941989b1e02ba1 to your computer and use it in GitHub Desktop.
Splits input at word delimeters and outputs a word per line.
-- |Little tool which splits words while you type. Useful when typing
-- live to espeak, for example: runhaskell WordSplitter | espeak
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Attoparsec as A
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import System.IO
main = loop B.empty
loop bs = do
hSetBuffering stdin NoBuffering
hSetBuffering stdout LineBuffering
r <- parseWith (B.hGetSome stdin 1024) word bs
case r of
Done i r -> do
BC.putStrLn r
loop i
e -> error $ show e
word = do
a <- A.takeTill (`B.elem` "\n ")
A.anyWord8
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment