Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@siers
Created June 28, 2015 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siers/01306a361c22f2de0122 to your computer and use it in GitHub Desktop.
Save siers/01306a361c22f2de0122 to your computer and use it in GitHub Desktop.
Extracts paragraphs to lines in files to allow generic operations be used
demo % cat file
1. Chox4ez9
Oov5iedeiF
ea7eapheeR
2. tu0ahTho
sheit5Kash
aiHiewae1i
3. Gau2Jesu
feic6zohNo
eimoo7aeQu
4. yaiN0oga
Thu0uafee4
Reebu1mooy
5. Qua3Eece
nuLeexaph8
sithiXuu5a
demo % paramap shuf < file
2. tu0ahTho
sheit5Kash
aiHiewae1i
1. Chox4ez9
Oov5iedeiF
ea7eapheeR
3. Gau2Jesu
feic6zohNo
eimoo7aeQu
5. Qua3Eece
nuLeexaph8
sithiXuu5a
4. yaiN0oga
Thu0uafee4
Reebu1mooy%
{-# LANGUAGE OverloadedStrings #-}
{- cabal install process-extras string -}
import Prelude ()
import Control.Applicative
import Control.Monad
import Data.Function
import Data.Functor
import Data.List
import Data.List.Split
import Data.Maybe
import qualified Data.ByteString as B
import System.Environment
import System.Process.ByteString
import GHC.Base
snd3 (_,b,_) = b
bsplitOn bs = map B.pack . splitOn (B.unpack bs) . B.unpack
strip = rs . rs
where rs = B.reverse . B.dropWhile (== 10)
respace from to = B.intercalate to . bsplitOn from
nullify :: B.ByteString -> B.ByteString
nullify = respace "\n" "\0"
denullify :: B.ByteString -> B.ByteString
denullify = respace "\0" "\n"
unpara pre = B.intercalate "\n" . map (nullify . pre) . bsplitOn "\n\n"
para = B.intercalate "\n\n" . map denullify . bsplitOn "\n"
process :: B.ByteString -> IO B.ByteString
process input = getArgs >>= (\as -> readProcessWithExitCode
(head as) (tail as) input) >>= return . snd3
main = B.putStr . para . strip =<< process . flip B.append "\n" . unpara strip =<< B.getContents
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment