Skip to content

Commit 8398c3b

Browse files
committed
A better show instance + more helpers
1 parent 17c9c86 commit 8398c3b

File tree

4 files changed

+49
-18
lines changed

4 files changed

+49
-18
lines changed

src/Data/DataFrame.purs

+18-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ module Data.DataFrame where
22

33
import Prelude
44

5+
import Control.Apply (lift2)
56
import Data.Compactable (class Compactable, separate)
67
import Data.Filterable (class Filterable, partitionMap)
78
import Data.Foldable (intercalate, maximum, surround)
89
import Data.Function (on)
910
import Data.Generic.Rep (class Generic)
1011
import Data.List (catMaybes, head, mapMaybe, partition, sortBy, take, transpose)
11-
import Data.List (filter) as List
12+
import Data.List (filter, length) as List
1213
import Data.List.Types (List)
1314
import Data.Map (Map)
1415
import Data.Map (toUnfoldable) as Map
@@ -18,7 +19,6 @@ import Data.Newtype (class Newtype, over, unwrap, wrap)
1819
import Data.String (length)
1920
import Data.String.NonEmpty (NonEmptyString)
2021
import Data.Tuple (fst, snd)
21-
import Data.Tuple.Nested (type (/\))
2222

2323
newtype DataFrame a = DataFrame (List a)
2424

@@ -47,21 +47,28 @@ instance filterableDF :: Filterable DataFrame where
4747
instance showUntypedDataFrame :: Show a => Show (DataFrame (Map NonEmptyString a)) where
4848
show =
4949
unwrap >>>
50-
take 10 >>>
51-
map (Map.toUnfoldable >>> sortBy (compare `on` fst)) >>>
52-
transpose >>>
53-
map drawColumn >>>
54-
transpose >>>
55-
map (surround " | ") >>> ((flip intercalate) <*> (head >>> maybe 0 length >>> power "-" >>> (_ <> "\n")))
50+
(lift2 (<>) (take 10 >>> showSummary) showRemainingCount)
5651
where
57-
drawColumn :: List (NonEmptyString /\ a) -> List String
52+
showSummary =
53+
map (Map.toUnfoldable >>> sortBy (compare `on` fst))
54+
>>> transpose
55+
>>> map drawColumn
56+
>>> transpose
57+
>>> map (surround " | ")
58+
>>> (flip intercalate <*> head >>> maybe 0 length >>> power "-" >>> (_ <> "\n"))
5859
drawColumn xs =
5960
map
6061
(snd >>> padTo (max (maxLengthOfValues xs) (maybe 0 (fst >>> show >>> length) (head xs))))
6162
xs
62-
maxLengthOfValues :: List (NonEmptyString /\ a) -> Int
63-
maxLengthOfValues = map (snd >>> show >>> length) >>> maximum >>> fromMaybe 0
63+
maxLengthOfValues =
64+
map (snd >>> show >>> length)
65+
>>> maximum
66+
>>> fromMaybe 0
6467
padTo :: Int -> a -> String
6568
padTo i a =
6669
let pad = power " " $ (i - length (show a)) / 2
6770
in pad <> show a <> pad
71+
showRemainingCount =
72+
List.length >>>
73+
(_ - 10) >>>
74+
((\l -> if _ then "\n... and " <> show l <> " more" else mempty) <*> (_ > 0))

src/Data/Query.purs

+25-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import Control.Monad.Reader.Trans (ReaderT)
77
import Data.DataFrame (DataFrame(..))
88
import Data.Filterable (filter) as Filterable
99
import Data.Foldable (foldr)
10-
import Data.List (List, (:))
10+
import Data.Function (on)
11+
import Data.List (List, length, sortBy, takeEnd, (:))
1112
import Data.Map (alter, empty, toUnfoldable) as Map
1213
import Data.Maybe (Maybe(..), maybe)
13-
import Data.Newtype (over)
14+
import Data.Newtype (over, unwrap)
1415
import Data.Tuple (snd)
1516
import Data.Tuple.Nested (type (/\))
1617

@@ -46,3 +47,25 @@ ungroup
4647
. Monad m
4748
=> Query m (b /\ (List a)) a
4849
ungroup = asks (over DataFrame (map snd >>> join))
50+
51+
_do
52+
:: m a b
53+
. Monad m
54+
=> (List a -> b)
55+
-> ReaderT (DataFrame a) m b
56+
_do f = asks (f <<< unwrap)
57+
58+
count
59+
:: m a
60+
. Monad m
61+
=> ReaderT (DataFrame a) m Int
62+
count = _do length
63+
64+
top_n_by
65+
:: m a b
66+
. Monad m
67+
=> Ord b
68+
=> Int
69+
-> (a -> b)
70+
-> Query m a a
71+
top_n_by n f = asks (over DataFrame (sortBy (compare `on` f) >>> takeEnd n))

src/Data/Query/Record.purs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Data.Query.Record where
22

33
import Prelude
44

5+
import Control.Apply (lift2)
56
import Control.Monad.Reader.Class (asks)
67
import Data.DataFrame (DataFrame(..))
78
import Data.Function (on)
@@ -101,7 +102,7 @@ mutate
101102
=> SProxy sym
102103
-> ({|input} -> a)
103104
-> Query m {|input} {|output}
104-
mutate proxy f = asks (map (\r -> R.insert proxy (f r) r))
105+
mutate proxy f = asks (map (lift2 (R.insert proxy) f identity))
105106

106107
transmute
107108
:: m input sym a out
@@ -112,4 +113,4 @@ transmute
112113
=> SProxy sym
113114
-> ({|input} -> a)
114115
-> Query m {|input} {|out}
115-
transmute proxy f = asks (map \r -> R.insert proxy (f r) {})
116+
transmute proxy f = asks (map (f >>> flip (R.insert proxy) {}))

src/Data/Query/Untyped.purs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ module Data.Query.Untyped where
22

33
import Prelude
44

5+
import Control.Apply (lift2)
56
import Control.Monad.Reader.Trans (asks)
67
import Data.Bifunctor (lmap)
78
import Data.DataFrame (DataFrame(..))
89
import Data.Foldable (elem)
910
import Data.Function (on)
1011
import Data.List (sortBy)
1112
import Data.List.NonEmpty (NonEmptyList)
12-
import Data.Map (Map)
1313
import Data.Map (filterKeys, insert, lookup, pop) as Map
1414
import Data.Maybe (Maybe, fromMaybe, maybe)
1515
import Data.Newtype (over)
1616
import Data.Ordering (invert)
1717
import Data.Query (Query)
1818
import Data.String (Pattern(..))
19-
import Data.String.NonEmpty (NonEmptyString, stripPrefix)
19+
import Data.String.NonEmpty (stripPrefix)
2020
import Data.Tuple (uncurry)
2121
import Types (Column, Row)
2222

@@ -61,7 +61,7 @@ mutate
6161
=> Column
6262
-> (Row a -> a)
6363
-> Query m (Row a) (Row a)
64-
mutate column f = asks (map (\r -> Map.insert column (f r) r))
64+
mutate column f = asks (map (lift2 (Map.insert column) f identity))
6565

6666
transmute
6767
:: m a

0 commit comments

Comments
 (0)