1{-# language DerivingVia #-}
2{-# language GeneralizedNewtypeDeriving #-}
3{-# language NamedFieldPuns #-}
4{-# language StandaloneKindSignatures #-}
7 ( Aggregate(..), foldInputs, mapInputs
8 , Aggregator(..), unsafeMakeAggregate
13import Data.Functor.Const ( Const( Const ), getConst )
14import Data.Kind ( Type )
18import qualified Opaleye.Internal.Aggregate as Opaleye
19import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
20import qualified Opaleye.Internal.PackMap as Opaleye
23import Data.Functor.Apply ( Apply, WrappedApplicative(..) )
26-- | An @Aggregate a@ describes how to aggregate @Table@s of type @a@. You can
27-- unpack an @Aggregate@ back to @a@ by running it with 'aggregate'. As
28-- @Aggregate@ is almost an 'Applicative' functor - but there is no 'pure'
29-- operation. This means 'Aggregate' is an instance of 'Apply', and you can
30-- combine @Aggregate@s using the @<.>@ combinator.
31type Aggregate :: Type -> Type
32newtype Aggregate a = Aggregate (Opaleye.Aggregator () a)
33 deriving newtype Functor
34 deriving Apply via (WrappedApplicative (Opaleye.Aggregator ()))
38 => (Maybe Aggregator -> Opaleye.PrimExpr -> b) -> Aggregate a -> b
39foldInputs f (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
40 getConst $ flip agg () $ \(aggregator, a) ->
41 Const $ f (detuplize <$> aggregator) a
43 detuplize (operation, ordering, distinction) =
44 Aggregator {operation, ordering, distinction}
48 => (Opaleye.PrimExpr -> Opaleye.PrimExpr) -> Aggregate a -> Aggregate a
49mapInputs transform (Aggregate (Opaleye.Aggregator (Opaleye.PackMap agg))) =
50 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ agg . \f input ->
51 f (fmap transform input)
54type Aggregator :: Type
55data Aggregator = Aggregator
56 { operation :: Opaleye.AggrOp
57 , ordering :: [Opaleye.OrderExpr]
58 , distinction :: Opaleye.AggrDistinct
62unsafeMakeAggregate :: ()
63 => (input -> Opaleye.PrimExpr)
64 -> (Opaleye.PrimExpr -> output)
68unsafeMakeAggregate input output aggregator expr =
69 Aggregate $ Opaleye.Aggregator $ Opaleye.PackMap $ \f _ ->
70 output <$> f (tuplize <$> aggregator, input expr)
72 tuplize Aggregator {operation, ordering, distinction} =
73 (operation, ordering, distinction)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list