on master help sign in
Add orderTabulation
5a45f67Shane O'Brien
src/Rel8/Tabulate.hs+13
@@ -24,6 +24,7 @@ module Rel8.Tabulate
24 , similarity
24 , similarity
25 , difference
25 , difference
26 , aggregateTabulation
26 , aggregateTabulation
27+ , orderTabulation
27 , optionalTabulation
28 , optionalTabulation
28 , manyTabulation
29 , manyTabulation
29 , someTabulation
30 , someTabulation
@@ -35,6 +36,7 @@ import Control.Applicative ( liftA2 )
35import Control.Monad ( join, liftM2 )
36import Control.Monad ( join, liftM2 )
36import Data.Bifunctor ( bimap, first )
37import Data.Bifunctor ( bimap, first )
37import Data.Foldable ( traverse_ )
38import Data.Foldable ( traverse_ )
39+import Data.Functor.Contravariant ( (>$<) )
38import Data.Maybe ( fromMaybe )
40import Data.Maybe ( fromMaybe )
39import Prelude hiding ( filter, lookup, undefined, zip, zipWith )
41import Prelude hiding ( filter, lookup, undefined, zip, zipWith )
40
42
@@ -42,11 +44,13 @@ import Prelude hiding ( filter, lookup, undefined, zip, zipWith )
42import Rel8.Aggregate ( Aggregate )
44import Rel8.Aggregate ( Aggregate )
43import Rel8.Expr ( Expr )
45import Rel8.Expr ( Expr )
44import Rel8.Expr.Bool ( not_, true )
46import Rel8.Expr.Bool ( not_, true )
47+import Rel8.Order ( Order )
45import Rel8.Query ( Query )
48import Rel8.Query ( Query )
46import Rel8.Query.Aggregate ( aggregate )
49import Rel8.Query.Aggregate ( aggregate )
47import Rel8.Query.Exists ( withBy, withoutBy )
50import Rel8.Query.Exists ( withBy, withoutBy )
48import Rel8.Query.Filter ( filter, where_ )
51import Rel8.Query.Filter ( filter, where_ )
49import Rel8.Query.Maybe ( optional )
52import Rel8.Query.Maybe ( optional )
53+import Rel8.Query.Order ( orderBy )
50import Rel8.Query.These ( alignBy )
54import Rel8.Query.These ( alignBy )
51import Rel8.Table ( Table )
55import Rel8.Table ( Table )
52import Rel8.Table.Aggregate ( groupBy, listAgg, nonEmptyAgg )
56import Rel8.Table.Aggregate ( groupBy, listAgg, nonEmptyAgg )
@@ -330,6 +334,15 @@ aggregateTabulation kas = do
330 fromQuery $ aggregate $ bitraverse1 groupBy id <$> as
334 fromQuery $ aggregate $ bitraverse1 groupBy id <$> as
331
335
332
336
337+-- | 'orderTabulation' orders the /values/ of a 'Tabulation' (not the keys).
338+-- In general this is meaningless, but if used together with 'manyTabulation'
339+-- or 'someTabulation', the resulting lists will be ordered according to
340+-- ordering given to 'orderTabulation'.
341+orderTabulation :: Order a -> Tabulation k a -> Tabulation k a
342+orderTabulation ordering (Tabulation as) =
343+ Tabulation $ orderBy (snd >$< ordering) . as
344+
345+
333optionalTabulation :: EqTable k
346optionalTabulation :: EqTable k
334 => Tabulation k a -> Tabulation k (MaybeTable a)
347 => Tabulation k a -> Tabulation k (MaybeTable a)
335optionalTabulation as = Tabulation $ \k -> do
348optionalTabulation as = Tabulation $ \k -> do