Skip to content

Instantly share code, notes, and snippets.

@acowley
Created May 22, 2015 02:33
Show Gist options
  • Save acowley/3de44c2dec15466f7247 to your computer and use it in GitHub Desktop.
Save acowley/3de44c2dec15466f7247 to your computer and use it in GitHub Desktop.
Declaring columns in Frames
{-# LANGUAGE OverloadedStrings, QuasiQuotes, RankNTypes #-}
{-# LANGUAGE DataKinds, FlexibleContexts, TemplateHaskell, TypeOperators #-}
import Frames
import Frames.ColumnUniverse
import Frames.CSV
import Lens.Family
import Pipes
-- Manually declared columns using a TH helper
declareColumn "x2" ''Double
declareColumn "y" ''Int
type Data2 = Record '[X1, X2, Y]
-- Manually declared column with no help
type X1 = "x1" :-> Double
x1 :: (forall f. Functor f => (Double -> f Double) -> Data2 -> f Data2)
x1 = rlens [pr|X1|]
data2Parser = defaultParser { headerOverride = Just [] }
data2Stream :: Producer Data2 IO ()
data2Stream = readTableOpt data2Parser "data/ex1data2.txt"
loadData2 :: IO (Frame Data2)
loadData2 = inCoreAoS data2Stream
mean :: Frame s -> (s -> Double) -> Double
mean d col = sum (col <$> d) / (fromIntegral $ frameLength d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment