{-# language OverloadedStrings #-}
{-# language FlexibleInstances #-}
{-# language DeriveFunctor, DeriveFoldable, DeriveTraversable, GeneralizedNewtypeDeriving #-}
{-# language ConstraintKinds #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{-# OPTIONS_GHC -Wno-type-defaults #-}
module Core.Data.Frame.List (
Frame,
frameFromList,
Core.Data.Frame.List.head,
Core.Data.Frame.List.take,
Core.Data.Frame.List.drop, Core.Data.Frame.List.zipWith, numRows,
Core.Data.Frame.List.filter,
filterA,
groupWith,
Core.Data.Frame.List.scanl, Core.Data.Frame.List.scanr,
toVector, fromVector,
) where
import qualified Control.Monad as CM (filterM)
import Data.List (groupBy)
import qualified Data.Vector as V
newtype Frame row = Frame {
tableRows :: [row] } deriving (Show, Functor, Foldable, Traversable)
head :: Frame row -> row
head = Prelude.head . tableRows
take :: Int -> Frame row -> Frame row
take n = Frame . Prelude.take n . tableRows
drop :: Int -> Frame row -> Frame row
drop n = Frame . Prelude.drop n . tableRows
zipWith :: (a -> b -> c) -> Frame a -> Frame b -> Frame c
zipWith f x y = frameFromList $ Prelude.zipWith f (tableRows x) (tableRows y)
frameFromList :: [row] -> Frame row
frameFromList = Frame
toList :: Frame row -> [row]
toList = tableRows
numRows :: Frame row -> Int
numRows = length . tableRows
filter :: (row -> Bool) -> Frame row -> Frame row
filter p = Frame . Prelude.filter p . tableRows
filterA :: Applicative f =>
(row -> f Bool) -> Frame row -> f (Frame row)
filterA fm t = frameFromList <$> CM.filterM fm (toList t)
scanl :: (b -> a -> b) -> b -> Frame a -> Frame b
scanl f z tt = Frame $ Prelude.scanl f z (tableRows tt)
scanr :: (a -> b -> b) -> b -> Frame a -> Frame b
scanr f z tt = Frame $ Prelude.scanr f z (tableRows tt)
groupWith :: (row -> row -> Bool) -> Frame row -> [Frame row]
groupWith f t = Frame <$> groupBy f (tableRows t)
toVector :: Frame row -> V.Vector row
toVector = V.fromList . tableRows
fromVector :: V.Vector row -> Frame row
fromVector = frameFromList . V.toList