import Text.CSV
import Data.Either
-- Definicioń de acumuladas
-- ========================
acumuladas :: [Double] -> [Double]
acumuladas = reverse . aux . reverse
where aux [] = []
aux [x] = [x]
aux (x:xs) = conUnDecimal (max x (0.6*x+0.4*y)) : y : ys
where (y:ys) = aux xs
-- conUnDecimal 7.26 == 7.3
-- conUnDecimal 7.24 == 7.2
conUnDecimal :: Double -> Double
conUnDecimal x = fromIntegral (round (10*x)) / 10
-- 1ª definición de notasAcumuladas
-- ================================
notasAcumuladas :: FilePath -> FilePath -> IO ()
notasAcumuladas f1 f2 = do
cs <- readFile f1
writeFile f2 (unlines (map ( acumuladaACadena
. notaAAcumuladas
. listaANota
. cadenaALista
)
(contenidoALineasDeNotas cs)))
-- λ> contenidoALineasDeNotas "juaruigar,3,7,6,3\nevadialop,3,6,7,3\n\n \n"
-- ["juaruigar,3,7,6,3","evadialop,3,6,7,3"]
contenidoALineasDeNotas :: String -> [String]
contenidoALineasDeNotas = filter esLineaDeNotas . lines
where esLineaDeNotas = elem ','
-- cadenaALista "a,b c,d" == ["a","b c","d"]
-- cadenaALista "juaruigar,3,7,6,3" == ["juaruigar","3","7","6","3"]
cadenaALista :: String -> [String]
cadenaALista cs
| tieneComas cs = d : cadenaALista ds
| otherwise = [cs]
where (d,_:ds) = span (/=',') cs
tieneComas = elem ','
-- λ> listaANota ["juaruigar","3","7","6","3"]
-- ("juaruigar",[3.0,7.0,6.0,3.0])
listaANota :: [String] -> (String,[Double])
listaANota (x:xs) = (x,map read xs)
-- λ> notaAAcumuladas ("juaruigar",[3.0,7.0,6.0,3.0])
-- ("juaruigar",[3.0,7.0,6.4,4.4])
notaAAcumuladas :: (String,[Double]) -> (String,[Double])
notaAAcumuladas (x,xs) = (x, acumuladas xs)
-- λ> acumuladaACadena ("juaruigar",[3.0,7.0,6.4,4.4])
-- "juaruigar,3.0,7.0,6.4,4.4"
acumuladaACadena :: (String,[Double]) -> String
acumuladaACadena (x,xs) =
x ++ "," ++ tail (init (show xs))
-- 2ª definición de notasAcumuladas
-- ================================
notasAcumuladas2 :: FilePath -> FilePath -> IO ()
notasAcumuladas2 f1 f2 = do
cs <- readFile f1
let (Right csv) = parseCSV f1 cs
let notas = [xs | xs <- csv, length xs > 1]
writeFile f2 (unlines (map ( acumuladaACadena
. notaAAcumuladas
. listaANota
)
notas))