Skip to content

Commit 405cf74

Browse files
committed
Cleanup Function adt.
1 parent e28e951 commit 405cf74

File tree

6 files changed

+40
-203
lines changed

6 files changed

+40
-203
lines changed

dataframe.cabal

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ library
2525
exposed-modules: DataFrame
2626
other-modules: DataFrame.Internal.Types,
2727
DataFrame.Internal.Expression,
28-
DataFrame.Internal.Function,
2928
DataFrame.Internal.Parsing,
3029
DataFrame.Internal.Column,
3130
DataFrame.Display.Terminal.PrettyPrint,
@@ -62,7 +61,6 @@ executable dataframe
6261
other-modules: DataFrame,
6362
DataFrame.Internal.Types,
6463
DataFrame.Internal.Expression,
65-
DataFrame.Internal.Function,
6664
DataFrame.Internal.Parsing,
6765
DataFrame.Internal.Column,
6866
DataFrame.Display.Terminal.PrettyPrint,

src/DataFrame.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ where
66

77
import DataFrame.Internal.Types as D
88
import DataFrame.Internal.Expression as D
9-
import DataFrame.Internal.Function as D
109
import DataFrame.Internal.Parsing as D
1110
import DataFrame.Internal.Column as D
1211
import DataFrame.Internal.DataFrame as D hiding (columnIndices, columns)

src/DataFrame/Internal/Column.hs

Lines changed: 35 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{-# LANGUAGE ConstraintKinds #-}
2-
{-# LANGUAGE ExplicitNamespaces #-}
32
{-# LANGUAGE GADTs #-}
43
{-# LANGUAGE InstanceSigs #-}
54
{-# LANGUAGE OverloadedStrings #-}
@@ -30,7 +29,6 @@ import qualified Data.Vector.Unboxed as VU
3029
import qualified Data.Vector.Unboxed.Mutable as VUM
3130

3231
import Control.Monad.ST (runST)
33-
import DataFrame.Internal.Function
3432
import DataFrame.Internal.Types
3533
import DataFrame.Internal.Parsing
3634
import Data.Int
@@ -221,44 +219,38 @@ type UnboxIf a = When (Unboxable a) (VU.Unbox a)
221219

222220
-- | Generic column transformation (no index).
223221
transform
224-
:: forall b c. -- element types
222+
:: forall b c.
225223
( Columnable b
226224
, Columnable c
227-
, UnboxIf c -- only required when ‘c’ is unboxable
225+
, UnboxIf c
228226
, Typeable b
229227
, Typeable c )
230228
=> (b -> c)
231229
-> Column
232230
-> Maybe Column
233231
transform f = \case
234-
235232
BoxedColumn (col :: VB.Vector a)
236233
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
237234
-> Just (toColumn' @c (VB.map f col))
238235
| otherwise -> Nothing
239-
240236
OptionalColumn (col :: VB.Vector a)
241237
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
242238
-> Just (toColumn' @c (VB.map f col))
243239
| otherwise -> Nothing
244-
245240
UnboxedColumn (col :: VU.Vector a)
246241
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
247242
-> Just $ case sUnbox @c of
248-
STrue -> UnboxedColumn (VU.map f col) -- needs VU.Unbox c
243+
STrue -> UnboxedColumn (VU.map f col)
249244
SFalse -> toColumn' @c (VB.map f (VB.convert col))
250245
| otherwise -> Nothing
251-
252246
GroupedBoxedColumn (col :: VB.Vector (VB.Vector a))
253247
| Just Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
254248
-> Just (toColumn' @c (VB.map f col))
255249
| otherwise -> Nothing
256-
257250
GroupedUnboxedColumn (col :: VB.Vector (VU.Vector a))
258251
| Just Refl <- testEquality (typeRep @(VU.Vector a)) (typeRep @b)
259252
-> Just (toColumn' @c (VB.map f col))
260253
| otherwise -> Nothing
261-
262254
GroupedOptionalColumn (col :: VB.Vector (VB.Vector a))
263255
| Just Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
264256
-> Just (toColumn' @c (VB.map f col))
@@ -365,79 +357,34 @@ sortedIndexes asc (GroupedOptionalColumn column) = runST $ do
365357
return $ VU.generate (VG.length column) (\i -> fst (sorted VG.! i))
366358
{-# INLINE sortedIndexes #-}
367359

368-
-- Operations on a column that may change its type.
369-
370-
-- instance Transformable Column where
371-
-- transform :: forall b c . (Columnable b, ColumnifyRep (KindOf b) b, Columnable c, ColumnifyRep (KindOf c) c) => (b -> c) -> Column -> Maybe Column
372-
-- transform f (BoxedColumn (column :: VB.Vector a)) = do
373-
-- Refl <- testEquality (typeRep @a) (typeRep @b)
374-
-- return (toColumn' @c (VB.map f column))
375-
-- transform f (OptionalColumn (column :: VB.Vector a)) = do
376-
-- Refl <- testEquality (typeRep @a) (typeRep @b)
377-
-- return (toColumn' @c (VB.map f column))
378-
-- transform f (UnboxedColumn (column :: VU.Vector a)) = do
379-
-- Refl <- testEquality (typeRep @a) (typeRep @b)
380-
-- return $ if testUnboxable (typeRep @c) then transformUnboxed f column else toColumn' (VB.map f (VB.convert column))
381-
-- transform f (GroupedBoxedColumn (column :: VB.Vector (VB.Vector a))) = do
382-
-- Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
383-
-- return (toColumn' @c (VB.map f column))
384-
-- transform f (GroupedUnboxedColumn (column :: VB.Vector (VU.Vector a))) = do
385-
-- Refl <- testEquality (typeRep @(VU.Vector a)) (typeRep @b)
386-
-- return (toColumn' @c (VB.map f column))
387-
-- transform f (GroupedOptionalColumn (column :: VB.Vector (VB.Vector a))) = do
388-
-- Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
389-
-- return (toColumn' @c (VB.map f column))
390-
391360
-- | Applies a function that returns an unboxed result to an unboxed vector, storing the result in a column.
392-
transformUnboxed :: forall a b . (Columnable a, ColumnifyRep (KindOf a) a, VU.Unbox a, Columnable b, ColumnifyRep (KindOf b) b) => (a -> b) -> VU.Vector a -> Column
393-
transformUnboxed f = itransformUnboxed (const f)
394-
395-
-- TODO: Make a type class with incoherent instances.
396-
itransformUnboxed :: forall a b . (Columnable a, ColumnifyRep (KindOf a) a, VU.Unbox a, Columnable b, ColumnifyRep (KindOf b) b) => (Int -> a -> b) -> VU.Vector a -> Column
397-
itransformUnboxed f column = case testEquality (typeRep @b) (typeRep @Int) of
398-
Just Refl -> UnboxedColumn $ VU.imap f column
399-
Nothing -> case testEquality (typeRep @b) (typeRep @Int8) of
400-
Just Refl -> UnboxedColumn $ VU.imap f column
401-
Nothing -> case testEquality (typeRep @b) (typeRep @Int16) of
402-
Just Refl -> UnboxedColumn $ VU.imap f column
403-
Nothing -> case testEquality (typeRep @b) (typeRep @Int32) of
404-
Just Refl -> UnboxedColumn $ VU.imap f column
405-
Nothing -> case testEquality (typeRep @b) (typeRep @Int64) of
406-
Just Refl -> UnboxedColumn $ VU.imap f column
407-
Nothing -> case testEquality (typeRep @b) (typeRep @Word8) of
408-
Just Refl -> UnboxedColumn $ VU.imap f column
409-
Nothing-> case testEquality (typeRep @b) (typeRep @Word16) of
410-
Just Refl -> UnboxedColumn $ VU.imap f column
411-
Nothing -> case testEquality (typeRep @b) (typeRep @Word32) of
412-
Just Refl -> UnboxedColumn $ VU.imap f column
413-
Nothing -> case testEquality (typeRep @b) (typeRep @Word64) of
414-
Just Refl -> UnboxedColumn $ VU.imap f column
415-
Nothing -> case testEquality (typeRep @b) (typeRep @Char) of
416-
Just Refl -> UnboxedColumn $ VU.imap f column
417-
Nothing -> case testEquality (typeRep @b) (typeRep @Bool) of
418-
Just Refl -> UnboxedColumn $ VU.imap f column
419-
Nothing -> case testEquality (typeRep @b) (typeRep @Float) of
420-
Just Refl -> UnboxedColumn $ VU.imap f column
421-
Nothing -> case testEquality (typeRep @b) (typeRep @Double) of
422-
Just Refl -> UnboxedColumn $ VU.imap f column
423-
Nothing -> case testEquality (typeRep @b) (typeRep @Word) of
424-
Just Refl -> UnboxedColumn $ VU.imap f column
425-
Nothing -> error "Result type is unboxed" -- since we only call this after confirming
426-
427-
-- | transform with index.
428-
itransform :: forall b c. (Columnable b, ColumnifyRep (KindOf b) b, Columnable b, Columnable c, ColumnifyRep (KindOf c) c) => (Int -> b -> c) -> Column -> Maybe Column
429-
itransform f (BoxedColumn (column :: VB.Vector a)) = do
430-
Refl <- testEquality (typeRep @a) (typeRep @b)
431-
return (toColumn' (VB.imap f column))
432-
itransform f (UnboxedColumn (column :: VU.Vector a)) = do
433-
Refl <- testEquality (typeRep @a) (typeRep @b)
434-
return $ if testUnboxable (typeRep @c) then itransformUnboxed f column else toColumn' (VB.imap f (VB.convert column))
435-
itransform f (GroupedBoxedColumn (column :: VB.Vector (VB.Vector a))) = do
436-
Refl <- testEquality (typeRep @(VB.Vector a)) (typeRep @b)
437-
return (toColumn' (VB.imap f column))
438-
itransform f (GroupedUnboxedColumn (column :: VB.Vector (VU.Vector a))) = do
439-
Refl <- testEquality (typeRep @(VU.Vector a)) (typeRep @b)
440-
return (toColumn' (VB.imap f column))
361+
itransform
362+
:: forall b c. (Typeable b, Typeable c, Columnable b, Columnable c)
363+
=> (Int -> b -> c) -> Column -> Maybe Column
364+
itransform f = \case
365+
BoxedColumn (col :: VB.Vector a)
366+
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
367+
-> Just (toColumn' @c (VB.imap f col))
368+
| otherwise -> Nothing
369+
UnboxedColumn (col :: VU.Vector a)
370+
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
371+
-> Just $
372+
case sUnbox @c of
373+
STrue -> UnboxedColumn (VU.imap f col)
374+
SFalse -> toColumn' @c (VB.imap f (VB.convert col))
375+
| otherwise -> Nothing
376+
OptionalColumn (col :: VB.Vector a)
377+
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
378+
-> Just (toColumn' @c (VB.imap f col))
379+
| otherwise -> Nothing
380+
GroupedBoxedColumn (col :: VB.Vector a)
381+
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
382+
-> Just (toColumn' @c (VB.imap f col))
383+
| otherwise -> Nothing
384+
GroupedUnboxedColumn (col :: VB.Vector a)
385+
| Just Refl <- testEquality (typeRep @a) (typeRep @b)
386+
-> Just (toColumn' @c (VB.imap f col))
387+
| otherwise -> Nothing
441388

442389
-- | Filter column with index.
443390
ifilterColumn :: forall a . (Columnable a) => (Int -> a -> Bool) -> Column -> Maybe Column
@@ -454,23 +401,6 @@ ifilterColumn f c@(GroupedUnboxedColumn (column :: VB.Vector b)) = do
454401
Refl <- testEquality (typeRep @a) (typeRep @b)
455402
return $ GroupedUnboxedColumn $ VG.ifilter f column
456403

457-
-- TODO: Expand this to use more predicates.
458-
ifilterColumnF :: Function -> Column -> Maybe Column
459-
ifilterColumnF (ICond (f :: Int -> a -> Bool)) c@(BoxedColumn (column :: VB.Vector b)) = do
460-
Refl <- testEquality (typeRep @a) (typeRep @b)
461-
return $ BoxedColumn $ VG.ifilter f column
462-
ifilterColumnF (ICond (f :: Int -> a -> Bool)) c@(UnboxedColumn (column :: VU.Vector b)) = do
463-
Refl <- testEquality (typeRep @a) (typeRep @b)
464-
return $ UnboxedColumn $ VG.ifilter f column
465-
ifilterColumnF (ICond (f :: Int -> a -> Bool)) c@(OptionalColumn (column :: VB.Vector b)) = do
466-
Refl <- testEquality (typeRep @a) (typeRep @b)
467-
return $ OptionalColumn $ VG.ifilter f column
468-
ifilterColumnF (ICond (f :: Int -> a -> Bool)) c@(GroupedBoxedColumn (column :: VB.Vector b)) = do
469-
Refl <- testEquality (typeRep @a) (typeRep @b)
470-
return $ GroupedBoxedColumn $ VG.ifilter f column
471-
ifilterColumnF (ICond (f :: Int -> a -> Bool)) c@(GroupedUnboxedColumn (column :: VB.Vector b)) = do
472-
Refl <- testEquality (typeRep @a) (typeRep @b)
473-
return $ GroupedUnboxedColumn $ VG.ifilter f column
474404

475405
ifoldrColumn :: forall a b. (Columnable a, Columnable b) => (Int -> a -> b -> b) -> b -> Column -> Maybe b
476406
ifoldrColumn f acc c@(BoxedColumn (column :: VB.Vector d)) = do
@@ -540,23 +470,16 @@ zipColumns (UnboxedColumn column) (BoxedColumn other) = BoxedColumn (VB.generate
540470
zipColumns (UnboxedColumn column) (UnboxedColumn other) = UnboxedColumn (VG.zip column other)
541471
{-# INLINE zipColumns #-}
542472

543-
zipWithColumns :: forall a b c . (Columnable a, ColumnifyRep (KindOf a) a, Columnable b, ColumnifyRep (KindOf b) b, Columnable c, ColumnifyRep (KindOf c) c) => (a -> b -> c) -> Column -> Column -> Column
544-
zipWithColumns f (BoxedColumn (column :: VB.Vector d)) (BoxedColumn (other :: VB.Vector e)) = case testEquality (typeRep @a) (typeRep @d) of
545-
Just Refl -> case testEquality (typeRep @b) (typeRep @e) of
546-
Just Refl -> toColumn' (VG.zipWith f column other)
547-
Nothing -> throw $ TypeMismatchException' (typeRep @b) (show $ typeRep @e) "" "zipWithColumns"
548-
Nothing -> throw $ TypeMismatchException' (typeRep @a) (show $ typeRep @d) "" "zipWithColumns"
549-
zipWithColumns f (BoxedColumn (column :: VB.Vector d)) (UnboxedColumn (other :: VU.Vector e)) = case testEquality (typeRep @a) (typeRep @d) of
550-
Just Refl -> case testEquality (typeRep @b) (typeRep @e) of
551-
Just Refl -> toColumn' (VG.zipWith f column (VB.convert other))
552-
Nothing -> throw $ TypeMismatchException' (typeRep @b) (show $ typeRep @e) "" "zipWithColumns"
553-
Nothing -> throw $ TypeMismatchException' (typeRep @a) (show $ typeRep @d) "" "zipWithColumns"
554-
zipWithColumns f left@(UnboxedColumn (column :: VU.Vector d)) right@(BoxedColumn (other :: VB.Vector e)) = zipWithColumns f right left
473+
zipWithColumns :: forall a b c . (Columnable a, Columnable b, Columnable c) => (a -> b -> c) -> Column -> Column -> Column
555474
zipWithColumns f (UnboxedColumn (column :: VU.Vector d)) (UnboxedColumn (other :: VU.Vector e)) = case testEquality (typeRep @a) (typeRep @d) of
556475
Just Refl -> case testEquality (typeRep @b) (typeRep @e) of
557476
Just Refl -> toColumn' $ VB.zipWith f (VG.convert column) (VG.convert other)
558477
Nothing -> throw $ TypeMismatchException' (typeRep @b) (show $ typeRep @e) "" "zipWithColumns"
559478
Nothing -> throw $ TypeMismatchException' (typeRep @a) (show $ typeRep @d) "" "zipWithColumns"
479+
zipWithColumns f left right = let
480+
left' = toVector @a left
481+
right' = toVector @b right
482+
in toColumn' $ VB.zipWith f left' right'
560483
{-# INLINE zipWithColumns #-}
561484

562485
-- Functions for mutable columns (intended for IO).

src/DataFrame/Internal/Function.hs

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/DataFrame/Operations/Subset.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Control.Exception (throw)
2020
import DataFrame.Errors (DataFrameException(..))
2121
import DataFrame.Internal.Column
2222
import DataFrame.Internal.DataFrame (DataFrame(..), getColumn, empty)
23-
import DataFrame.Internal.Function
23+
import DataFrame.Internal.Expression
2424
import DataFrame.Internal.Row (mkRowFromArgs)
2525
import DataFrame.Internal.Types (RowValue, toRowValue)
2626
import DataFrame.Operations.Core
@@ -97,9 +97,10 @@ filterBy = flip filter
9797
-- must appear in the same order as they do in the list.
9898
--
9999
-- > filterWhere (["x", "y"], func (\x y -> x + y > 5)) df
100-
filterWhere :: ([T.Text], Function) -> DataFrame -> DataFrame
101-
filterWhere (args, f) df = let
102-
indexes = VG.ifoldl' (\s i row -> if funcApply @Bool row f then S.insert i s else s) S.empty $ V.generate (fst (dimensions df)) (mkRowFromArgs args df)
100+
filterWhere :: Expr Bool -> DataFrame -> DataFrame
101+
filterWhere expr df = let
102+
(TColumn col) = interpret @Bool df expr
103+
(Just indexes) = ifoldlColumn (\s i satisfied -> if satisfied then S.insert i s else s) S.empty col
103104
c' = snd $ dataframeDimensions df
104105
pick idxs col = atIndices idxs <$> col
105106
in df {columns = V.map (pick indexes) (columns df), dataframeDimensions = (S.size indexes, c')}

src/DataFrame/Operations/Transformations.hs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import DataFrame.Errors (DataFrameException(..))
1717
import DataFrame.Internal.Column (Column(..), columnTypeString, itransform, ifoldrColumn, TypedColumn (TColumn), Columnable, transform, unwrapTypedColumn)
1818
import DataFrame.Internal.DataFrame (DataFrame(..), getColumn)
1919
import DataFrame.Internal.Expression
20-
import DataFrame.Internal.Function (Function(..), funcApply)
2120
import DataFrame.Internal.Row (mkRowFromArgs)
2221
import DataFrame.Internal.Types (RowValue, toRowValue)
2322
import DataFrame.Operations.Core

0 commit comments

Comments
 (0)