Created
December 3, 2019 11:57
-
-
Save SergeyStretovich/d509339385c54c59fd201b0259164374 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
import Data.Maybe | |
import qualified Data.Text as T | |
import qualified Data.Text.IO as TIO | |
import Text.Read | |
data Car = Car{year:: Int, mark:: T.Text, model:: T.Text, comment:: T.Text, price:: Float } -- Double | |
-- a b c d e | |
csvS = (T.pack "1997;Ford;E350;ac, abs, moon;3000.00") | |
csvE = (T.pack "1997;Ford;E350;ac, abs, moon;3000.00\n\ | |
\1996; Jeep; Grand Cherokee; MUST SELL! air, moon roof, loaded; 4799.00\n\ | |
\1999;Chevy;Venture \"Extended Edition\"; ; 4900.00") | |
main = do | |
let maybeCars = catMaybes $ map (\x->csvCarParser x) $ T.lines csvE | |
mapM_ (\car->showCar car) maybeCars | |
putStrLn "--" | |
showCar::Car -> IO() | |
showCar cr = putStrLn $ "Car, year: " ++ (show (year cr)) ++"| mark: "++(T.unpack (mark cr))++"| model: "++(T.unpack (model cr))++"| comment: "++(T.unpack (comment cr))++"| price: "++(show (price cr)) | |
generalFunc1 :: T.Text -> (T.Text->Maybe a)-> Maybe ( a,T.Text) | |
generalFunc1 txt convFunc = | |
if (isJust ltVal) == True then Just ( (fromJust ltVal),tail) else Nothing | |
where ltVal = (convFunc taken) | |
tail = (T.drop len txt) | |
len = (T.length taken)+1 | |
taken = T.strip $ T.takeWhile (/=';') txt | |
generalFunc :: T.Text -> (T.Text->Maybe a)-> Maybe ( a,T.Text) | |
generalFunc txt convFunc = | |
if (isJust ltVal) == True then Just ( (fromJust ltVal),(T.drop 1 (snd taken))) else Nothing | |
where ltVal = (convFunc $ T.strip $ fst taken) | |
taken = T.breakOn ";" txt | |
getString :: T.Text -> Maybe T.Text | |
getString txt = Just txt | |
getInt :: T.Text -> Maybe Int | |
getInt txt = readMaybe (T.unpack txt) :: Maybe Int | |
getFloat :: T.Text -> Maybe Float | |
getFloat txt = readMaybe (T.unpack txt) :: Maybe Float | |
getDouble :: T.Text -> Maybe Double | |
getDouble txt = readMaybe (T.unpack txt) :: Maybe Double | |
csvCarParser :: T.Text -> Maybe Car | |
csvCarParser input = do | |
(a, last ) <- generalFunc input getInt | |
(b, last2) <- generalFunc last getString | |
(c, last3) <- generalFunc last2 getString | |
(d, last4) <- generalFunc last3 getString | |
(e, last5) <- generalFunc last4 getFloat | |
Just (Car a b c d e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment