nptools 0.4.1 → 0.5.0
raw patch · 5 files changed
+69/−3 lines, 5 filesdep +directorynew-component:exe:deliver-to-pollboxnew-component:exe:mean
Dependencies added: directory
Files
- LICENSE +1/−1
- deliver-to-pollbox.hs +28/−0
- mean.hs +28/−0
- nptools.cabal +11/−1
- show-non-ascii.hs +1/−1
LICENSE view
@@ -1,4 +1,4 @@-Copyright (c) 2009, Nicolas Pouillard+Copyright (c) 2009-2011, Nicolas Pouillard All rights reserved. Redistribution and use in source and binary forms, with or without
+ deliver-to-pollbox.hs view
@@ -0,0 +1,28 @@+import qualified Data.ByteString.Lazy.Char8 as L8+import Data.Char+import Data.Maybe+import Data.List+import System.Environment+import System.FilePath+import System.IO+import System.Directory++selectDestDir :: L8.ByteString -> FilePath+selectDestDir =+ fromMaybe "no-destdir-header"+ . fmap (L8.unpack . L8.drop (L8.length xdestdir))+ . find (xdestdir `L8.isPrefixOf`)+ . map (L8.map toLower) . L8.lines+ where+ xdestdir = L8.pack "x-destdir: "++main :: IO ()+main = do [arg] <- getArgs+ input <- L8.getContents+ let dir = arg </> selectDestDir input+ createDirectoryIfMissing True dir+ (_,h) <- openTempFile dir "file.log"+ L8.hPut h input+ hClose h++-- vim: ft=haskell
+ mean.hs view
@@ -0,0 +1,28 @@+import System.Environment+import Control.Monad+import Data.Ratio+import Numeric++data P = P !Integer !Integer++mean :: [Integer] -> Rational+mean = divP . foldl f (P 0 0) where+ f (P m s) i = P (i + m) (1 + s)+ divP (P m s) = m % s++showRat :: Rational -> String+showRat x = showFFloat Nothing (fromRat x :: Double) ""++main :: IO ()+main = do+ argc <- length `fmap` getArgs+ when (argc /= 0) . fail . unlines $ ["`mean' does not expect arguments."+ ,"It reads numbers from stdin and finally prints the mean"]+ putStrLn . showRat -- format the output+ . mean -- take the mean+ . map readI -- turns numbers into integers+ . concatMap words -- split words+ . lines -- split lines+ =<< getContents -- reads stdin+ where readI :: String -> Integer+ readI = read
nptools.cabal view
@@ -1,6 +1,6 @@ Name: nptools Cabal-Version: >=1.4-Version: 0.4.1+Version: 0.5.0 License: BSD3 License-File: LICENSE Copyright: (c) Nicolas Pouillard@@ -67,8 +67,18 @@ Build-depends: base>=3&&<5, filepath, SHA, MissingH ghc-options: -Wall -Odph +executable deliver-to-pollbox+ main-is: deliver-to-pollbox.hs+ Build-depends: base>=3&&<5, filepath, bytestring, directory+ ghc-options: -Wall -Odph+ executable summ main-is: summ.hs+ Build-depends: base>=3&&<5+ ghc-options: -Wall -Odph++executable mean+ main-is: mean.hs Build-depends: base>=3&&<5 ghc-options: -Wall -Odph
show-non-ascii.hs view
@@ -19,5 +19,5 @@ main :: IO () main = do putStrLn "Reading from stdin" args <- getArgs- hSetEncoding stdin (if "--utf-8" `elem` args then utf8 else latin1)+ hSetEncoding stdin (if "--latin1" `elem` args then latin1 else utf8) putStrLn =<< unlines . concatMap analyseLine . zip [1..] . lines <$> getContents