Text processing/1: Difference between revisions
Content added Content deleted
(→{{header|J}}: give Dates to findLongestRuns as left arg) |
(added Haskell) |
||
Line 509: | Line 509: | ||
main bye |
main bye |
||
</lang> |
</lang> |
||
=={{header|Haskell}}== |
|||
<lang Haskell>import Data.List |
|||
import Numeric |
|||
import Control.Arrow |
|||
import Control.Monad |
|||
import Text.Printf |
|||
import System.Environment |
|||
type Date = String |
|||
type Value = Double |
|||
type Flag = Int |
|||
readFlg :: String -> Flag |
|||
readFlg xs@(x:ns) | x=='-' = 0 |
|||
| otherwise = fromEnum $ (>"0")xs |
|||
readNum :: String -> Value |
|||
readNum = fst.head.readFloat |
|||
take2 = map fst.takeWhile(not.null.fst).drop 1.iterate (splitAt 2.snd).(,)[] |
|||
parseData :: [String] -> (Date,[(Value,Flag)]) |
|||
parseData = head &&& map(readNum.head &&& readFlg.last).take2.tail |
|||
sumAccs :: (Date,[(Value,Flag)]) -> (Date, ((Value,Int),[Flag])) |
|||
sumAccs = second (((sum &&& length).concat.uncurry(zipWith(\v f -> [v|f==1])) &&& snd).unzip) |
|||
maxNAseq :: [Flag] -> [(Int,Int)] |
|||
maxNAseq = head.groupBy(\a b -> fst a==fst b).sortBy(flip compare) |
|||
. concat.uncurry(zipWith(\i (r,b)->[(r,i)|b==0])) |
|||
. first(init.scanl(+)0). unzip |
|||
. foldl' (\cs xs -> cs++[(fst &&& id)$(length &&& head)xs]) []. group |
|||
main = do |
|||
file:_ <- getArgs |
|||
f <- readFile file |
|||
let dat :: [(Date,((Value,Int),[Flag]))] |
|||
dat = map (sumAccs. parseData. words).lines $ f |
|||
summ = ((sum *** sum). unzip *** maxNAseq.concat). unzip $ map snd dat |
|||
totalFmt = "\nSummary\t\t accept: %d\t total: %.3f \taverage: %6.3f\n\n" |
|||
lineFmt = "%8s\t accept: %2d\t total: %11.3f \taverage: %6.3f\n" |
|||
maxFmt = "Maximum of %d consecutive false readings, starting on line /%s/ and ending on line /%s/\n" |
|||
-- output statistics |
|||
putStrLn "\nSome lines:\n" |
|||
mapM_ (\(d,((v,n),_)) -> printf lineFmt d n v ((v/fromIntegral n))) $ take 4 $ drop 2200 dat |
|||
(\(t,n) -> printf totalFmt n t (t/fromIntegral n)) $ fst summ |
|||
mapM_ ((\(l, d1,d2) -> printf maxFmt l d1 d2) |
|||
. (\(a,b)-> (a,(fst.(dat!!).(`div`24))b,(fst.(dat!!).(`div`24))(a+b)))) $ snd summ</lang> |
|||
Output: |
|||
<lang Haskell>*Main> :main ["./RC/readings.txt"]</lang> |
|||
<pre>Some lines: |
|||
1996-01-11 accept: 24 total: 437.000 average: 18.208 |
|||
1996-01-12 accept: 24 total: 536.000 average: 22.333 |
|||
1996-01-13 accept: 24 total: 1062.000 average: 44.250 |
|||
1996-01-14 accept: 24 total: 787.000 average: 32.792 |
|||
Summary accept: 129403 total: 1358393.400 average: 10.497 |
|||
Maximum of 589 consecutive false readings, starting on line /1993-02-09/ and ending on line /1993-03-05/</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |