Guest User

Untitled

a guest
Aug 21st, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2.  
  3. import qualified Data.Vector as V
  4. import qualified Data.Csv as C
  5. import qualified Data.ByteString.Lazy as B
  6. import System.Environment (getArgs)
  7.  
  8. type BinFunction = Int -> Int
  9. type GroupList = V.Vector Int
  10. type CombFunction = V.Vector Int -> Int
  11. type ExtrFunction = Int -> V.Vector Int
  12. type VectorBinner = V.Vector Int -> Int                    
  13.  
  14. data BinningType = Mod
  15.                  | ModAndDate
  16.                  deriving (Read, Show)
  17.  
  18. modBin :: Int -> BinFunction
  19. modBin m n = mod n m
  20.  
  21. dateBin :: Int -> Int -> BinFunction
  22. dateBin maxDays groupSize d = div (d * groupSize) maxDays
  23.  
  24. modComb :: GroupList -> CombFunction
  25. modComb groupList values = V.sum $ V.zipWith (*) products values
  26.   where products = V.tail $ V.scanr (*) 1 groupList
  27.  
  28. modExtr :: GroupList -> ExtrFunction
  29. modExtr groupList value = V.reverse $ V.fromList $ snd $ V.foldr f (value, []) groupList
  30.   where f x (v, acc) = (mod v x, div v x : acc)
  31.  
  32. binVector :: CombFunction -> V.Vector BinFunction -> VectorBinner
  33. binVector combFunction binFunctions v =
  34.   combFunction $ V.zipWith ($) binFunctions v
  35.  
  36. argHandler :: [String] -> VectorBinner
  37. argHandler args = case read (args !! 0) of
  38.   ModAndDate -> binVector (modComb groupList) modDateBFs
  39.   Mod -> binVector (modComb groupList) $ V.map modBin groupNums
  40.  
  41.   where groupNums :: V.Vector Int
  42.         groupNums = V.fromList $ map read $ tail args
  43.  
  44.         groupList :: GroupList
  45.         groupList = V.map abs groupNums
  46.  
  47.         modDateBFs :: V.Vector BinFunction
  48.         modDateBFs = V.map chooseModDateBF groupNums
  49.  
  50.         chooseModDateBF :: Int -> BinFunction
  51.         chooseModDateBF g | g < 0 = dateBin 2557 (-g)
  52.                           | g > 0 = modBin g
  53.  
  54. main = do args <- getArgs
  55.  
  56.           (Right input) <- fmap (C.decode False) B.getContents
  57.  
  58.           vectorBinner <- fmap argHandler getArgs
  59.  
  60.           B.putStr $ C.encode $ V.map (C.Only . vectorBinner) input
Advertisement
Add Comment
Please, Sign In to add comment