{-# LANGUAGE BlockArguments #-}

module HLRDB.Primitives.Aggregate
       ( Q(..)
       -- | Aggregate, atomic multi-set query (as in setting multiple things in a single query)
       , MSET
       ) where

import Control.Lens
import Data.ByteString
import HLRDB.Internal (MSET)


-- | An applicative representing a single bulk query, reified into the Redis monad via @mget@.
newtype Q a = Q (Traversal () a ByteString (Maybe ByteString))

instance Functor Q where
  {-# INLINE fmap #-}
  fmap :: (a -> b) -> Q a -> Q b
fmap a -> b
f = \(Q Traversal () a ByteString (Maybe ByteString)
g) -> Traversal () b ByteString (Maybe ByteString) -> Q b
forall a. Traversal () a ByteString (Maybe ByteString) -> Q a
Q \ByteString -> f (Maybe ByteString)
x -> (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (f a -> f b) -> (() -> f a) -> () -> f b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ByteString -> f (Maybe ByteString)) -> () -> f a
Traversal () a ByteString (Maybe ByteString)
g ByteString -> f (Maybe ByteString)
x

instance Applicative Q where
  {-# INLINE pure #-}
  pure :: a -> Q a
pure a
x = Traversal () a ByteString (Maybe ByteString) -> Q a
forall a. Traversal () a ByteString (Maybe ByteString) -> Q a
Q \ByteString -> f (Maybe ByteString)
_ ()
_ -> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
  {-# INLINE (<*>) #-}
  <*> :: Q (a -> b) -> Q a -> Q b
(<*>) (Q Traversal () (a -> b) ByteString (Maybe ByteString)
f) (Q Traversal () a ByteString (Maybe ByteString)
x) = Traversal () b ByteString (Maybe ByteString) -> Q b
forall a. Traversal () a ByteString (Maybe ByteString) -> Q a
Q \ByteString -> f (Maybe ByteString)
g ()
a -> (ByteString -> f (Maybe ByteString)) -> () -> f (a -> b)
Traversal () (a -> b) ByteString (Maybe ByteString)
f ByteString -> f (Maybe ByteString)
g ()
a f (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (ByteString -> f (Maybe ByteString)) -> () -> f a
Traversal () a ByteString (Maybe ByteString)
x ByteString -> f (Maybe ByteString)
g ()
a