Skip to content

Commit

Permalink
simplified that 'outputEither' nonses with a typeclass.
Browse files Browse the repository at this point in the history
I *was* being too clever.
  • Loading branch information
startling committed Dec 5, 2012
1 parent 25e49d8 commit be16c07
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Partly/Common.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# Language FlexibleInstances #-}
module Partly.Common where
-- base:
import Control.Applicative
Expand Down Expand Up @@ -62,15 +63,25 @@ parseOutput = Output
& metavar "file"
& help "A file to write to; defaults to stdout." )

-- | Output a bytestring or a string, given some 'Output'.
output :: Output -> L.ByteString -> IO ()
output = maybe L.putStr L.writeFile . outFile
class Outputs o where
toFile :: FilePath -> o -> IO ()
toScreen :: o -> IO ()

instance Outputs String where
toFile = writeFile
toScreen = putStrLn

instance Outputs B.ByteString where
toFile = B.writeFile
toScreen = B.putStr

-- | Output a string, given some 'Output'.
outputEither :: Output -> Either String B.ByteString -> IO ()
outputEither = maybe (putStrLn <!> B.putStr)
(\p -> writeFile p <!> B.writeFile p) . outFile
where (f <!> g) e = case e of Left a -> f a; Right b -> g b
instance Outputs L.ByteString where
toFile = L.writeFile
toScreen = L.putStr

-- | Output a bytestring or a string, given some 'Output'.
output :: Outputs o => Output -> o -> IO ()
output = maybe toScreen toFile . outFile

-- | Some options related to how we get input.
data Input = Input
Expand Down

0 comments on commit be16c07

Please sign in to comment.