Created
January 4, 2016 10:07
-
-
Save sorpaas/3f61784c3cd3c18d434c to your computer and use it in GitHub Desktop.
Haskell Data Analysis
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 DeriveGeneric #-} | |
> import Data.Csv (decodeByName, FromNamedRecord, ToNamedRecord, DefaultOrdered, Header) | |
> import Data.Text | |
> import Data.Text.Encoding (encodeUtf8) | |
> import Data.Text.IO (putStrLn) | |
> import Data.Vector | |
> import Data.Set | |
> import Prelude hiding (readFile) | |
> import Data.ByteString.Lazy (readFile) | |
> import GHC.Generics | |
> data SeqEcoRecord = SeqEcoRecord { id :: !Int, | |
> datetime_sys :: !Text, | |
> eventname :: !Text, | |
> datetime :: !Text, | |
> username :: !Text, | |
> key :: !Text, | |
> value :: !Text, | |
> raw :: !Text } | |
> deriving (Generic, Show) | |
> instance FromNamedRecord SeqEcoRecord | |
> instance ToNamedRecord SeqEcoRecord | |
> instance DefaultOrdered SeqEcoRecord | |
> mapIO :: (a -> IO ()) -> [a] -> IO () | |
> mapIO f xs = if Prelude.null xs then return () else do | |
> f (Prelude.head xs) | |
> mapIO f (Prelude.tail xs) | |
`allKeys` fetches all the keys that occured in the original sequence record. | |
> allKeys :: Vector SeqEcoRecord -> Set Text | |
> allKeys a = Data.Set.fromList $ Data.Vector.toList $ Data.Vector.map key a | |
Here's the result of all keys: | |
QA_section_started | |
QA_section_started_at_entry_point | |
localStorage_error | |
next_button_clicked | |
question_「各盡所能,各取所需」是個很棒的念頭 | |
question_人民根本上是通過階級而非國籍來劃分的 | |
question_以資本生財的人對社會的貢獻比不上靠勞動賺錢的人 | |
question_企業不會自覺保護環境,因此需要規章來規範它們 | |
question_保護主義對貿易來說有時候是必須的 | |
question_公司為它的股東賺取利潤就是它僅有的社會職責 | |
question_土地完全可以作為商品在市場上自由買買 | |
question_土地完全可以作為商品在市場上自由買賣 | |
question_在重大工程的決策中,個人利益應為社會整體利益讓路 | |
question_如果食品價格過高,政府應當干預 | |
question_學歷 | |
question_對富人徵收的稅太高了 | |
question_市場越自由,人民越自由 | |
question_年齡 | |
question_性別 | |
question_控制通貨膨脹比控制失業更重要 | |
question_改善低收入人士生活的首要手段是政府給予財政補貼和扶持 | |
question_政府應對商品的出入境設限,以首先滿足境內需求 | |
question_政府應該懲罰誤導公眾的商業行為 | |
question_政府應該為最低工資立法 | |
question_教育應當儘可能公營 | |
question_社會中一些基本事物如飲用水不應該成為貨架上的商品 | |
question_試圖控制房地產價格的行為會破壞經濟發展 | |
question_那些付得起錢的人理應有權獲得更好的醫療 | |
referrer | |
render | |
share_button_click | |
total_score_before_normalization | |
url | |
userAgent | |
So based on those keys, we define UserRecord: | |
> data UserRecord = UserRecord | |
> vectorMain :: Vector SeqEcoRecord -> IO () | |
> vectorMain v = mapIO Data.Text.IO.putStrLn $ Data.Set.toList $ allKeys v | |
> main :: IO () | |
> main = do | |
> s <- readFile "ecocompass.csv" | |
> let d = decodeByName s :: Either String (Header, Vector SeqEcoRecord) | |
> case d of | |
> Left x -> print x | |
> Right (h, r) -> vectorMain r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment