Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,45 @@
|
|||
import Data.Array (Array(..), (//), bounds, elems, listArray)
|
||||
import Data.List (intercalate)
|
||||
import Control.Monad (when)
|
||||
import Data.Maybe (isJust)
|
||||
|
||||
delimiters :: String
|
||||
delimiters = ",;:"
|
||||
|
||||
fields :: String -> [String]
|
||||
fields [] = []
|
||||
fields xs =
|
||||
let (item, rest) = break (`elem` delimiters) xs
|
||||
(_, next) = break (`notElem` delimiters) rest
|
||||
in item : fields next
|
||||
|
||||
unfields :: Maybe (Array (Int, Int) String) -> [String]
|
||||
unfields Nothing = []
|
||||
unfields (Just a) = every fieldNumber $ elems a
|
||||
where
|
||||
((_, _), (_, fieldNumber)) = bounds a
|
||||
every _ [] = []
|
||||
every n xs =
|
||||
let (y, z) = splitAt n xs
|
||||
in intercalate "," y : every n z
|
||||
|
||||
fieldArray :: [[String]] -> Maybe (Array (Int, Int) String)
|
||||
fieldArray [] = Nothing
|
||||
fieldArray xs =
|
||||
Just $ listArray ((1, 1), (length xs, length $ head xs)) $ concat xs
|
||||
|
||||
fieldsFromFile :: FilePath -> IO (Maybe (Array (Int, Int) String))
|
||||
fieldsFromFile = fmap (fieldArray . map fields . lines) . readFile
|
||||
|
||||
fieldsToFile :: FilePath -> Maybe (Array (Int, Int) String) -> IO ()
|
||||
fieldsToFile f = writeFile f . unlines . unfields
|
||||
|
||||
someChanges :: Maybe (Array (Int, Int) String)
|
||||
-> Maybe (Array (Int, Int) String)
|
||||
someChanges =
|
||||
fmap (// [((1, 1), "changed"), ((3, 4), "altered"), ((5, 2), "modified")])
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
a <- fieldsFromFile "example.txt"
|
||||
when (isJust a) $ fieldsToFile "output.txt" $ someChanges a
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
{-# LANGUAGE FlexibleContexts,
|
||||
TypeFamilies,
|
||||
NoMonomorphismRestriction #-}
|
||||
import Data.List (intercalate)
|
||||
import Data.List.Split (splitOn)
|
||||
import Lens.Micro
|
||||
|
||||
(<$$>) :: (Functor f1, Functor f2) =>
|
||||
(a -> b) -> f1 (f2 a) -> f1 (f2 b)
|
||||
(<$$>) = fmap . fmap
|
||||
|
||||
------------------------------------------------------------
|
||||
-- reading and writing
|
||||
|
||||
newtype CSV = CSV { values :: [[String]] }
|
||||
|
||||
readCSV :: String -> CSV
|
||||
readCSV = CSV . (splitOn "," <$$> lines)
|
||||
|
||||
instance Show CSV where
|
||||
show = unlines . map (intercalate ",") . values
|
||||
|
||||
------------------------------------------------------------
|
||||
-- construction and combination
|
||||
|
||||
mkColumn, mkRow :: [String] -> CSV
|
||||
(<||>), (<==>) :: CSV -> CSV -> CSV
|
||||
|
||||
mkColumn lst = CSV $ sequence [lst]
|
||||
mkRow lst = CSV [lst]
|
||||
|
||||
CSV t1 <||> CSV t2 = CSV $ zipWith (++) t1 t2
|
||||
CSV t1 <==> CSV t2 = CSV $ t1 ++ t2
|
||||
|
||||
------------------------------------------------------------
|
||||
-- access and modification via lenses
|
||||
|
||||
table = lens values (\csv t -> csv {values = t})
|
||||
row i = table . ix i . traverse
|
||||
col i = table . traverse . ix i
|
||||
item i j = table . ix i . ix j
|
||||
|
||||
------------------------------------------------------------
|
||||
|
||||
sample = readCSV "C1, C2, C3, C4, C5\n\
|
||||
\1, 5, 9, 13, 17\n\
|
||||
\2, 6, 10, 14, 18\n\
|
||||
\3, 7, 11, 15, 19\n\
|
||||
\4, 8, 12, 16, 20"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
sampleSum = sample <||> (mkRow ["SUM"] <==> mkColumn sums)
|
||||
where sums = map (show . sum) (read <$$> drop 1 (values sample))
|
||||
Loading…
Add table
Add a link
Reference in a new issue