Skip to content

Instantly share code, notes, and snippets.

@qnikst
Created February 18, 2012 15:58
Show Gist options
  • Save qnikst/1859929 to your computer and use it in GitHub Desktop.
Save qnikst/1859929 to your computer and use it in GitHub Desktop.
cut -f analog
import Data.Char (ord)
import Data.Conduit
import Data.Conduit.Binary (sourceHandle, sinkHandle)
import qualified Data.Conduit.Binary as CB
import qualified Data.Conduit.List as CL
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.Lazy as SL
import System.Environment (getArgs)
import System.IO
import Control.Monad (liftM)
main = do
nums <- liftM (map read) getArgs :: IO [Int]
runResourceT $ sourceHandle stdin
$= CB.lines -- CB.lines
$= CL.map ({-# SCC "map" #-} \l ->
let cols = (0:S8.elemIndices '\t' l)
substr [] _ = S.empty
substr (x1:x2:_) 1 = S.drop (x1) (S.take x2 l)
substr (x:xs) k = substr xs (k-1)
in foldl (\p n -> p `S.append` (substr cols n)) S.empty nums
)
$= CL.concatMap (\v -> [v,nl])
$$ sinkHandle stdout
where
tab = S8.singleton '\t'
nl = S8.singleton '\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment