Created
January 29, 2015 21:50
-
-
Save zouppen/73f65d941989b1e02ba1 to your computer and use it in GitHub Desktop.
Splits input at word delimeters and outputs a word per line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- |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