module Orville.PostgreSQL.Plan.Many
( Many
, NotAKey (NotAKey)
, fromKeys
, lookup
, keys
, elems
, map
, toMap
, apply
, compose
)
where
import Prelude (Either (Left, Right), Functor (fmap), Maybe (Just, Nothing), Ord, ($), (.), (<*>))
import qualified Data.Either as Either
import qualified Data.Map as Map
import qualified Data.Maybe as Maybe
data NotAKey
= NotAKey
data Many k a
= Many [k] (k -> Either NotAKey a)
instance Functor (Many k) where
fmap :: forall a b. (a -> b) -> Many k a -> Many k b
fmap = (a -> b) -> Many k a -> Many k b
forall a b k. (a -> b) -> Many k a -> Many k b
map
fromKeys :: [k] -> (k -> Either NotAKey a) -> Many k a
fromKeys :: forall k a. [k] -> (k -> Either NotAKey a) -> Many k a
fromKeys =
[k] -> (k -> Either NotAKey a) -> Many k a
forall k a. [k] -> (k -> Either NotAKey a) -> Many k a
Many
map :: (a -> b) -> Many k a -> Many k b
map :: forall a b k. (a -> b) -> Many k a -> Many k b
map a -> b
f (Many [k]
ks k -> Either NotAKey a
keyToValue) =
[k] -> (k -> Either NotAKey b) -> Many k b
forall k a. [k] -> (k -> Either NotAKey a) -> Many k a
Many [k]
ks ((a -> b) -> Either NotAKey a -> Either NotAKey b
forall a b. (a -> b) -> Either NotAKey a -> Either NotAKey b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (Either NotAKey a -> Either NotAKey b)
-> (k -> Either NotAKey a) -> k -> Either NotAKey b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. k -> Either NotAKey a
keyToValue)
apply ::
(Many param (a -> b)) ->
(Many param a) ->
(Many param b)
apply :: forall param a b.
Many param (a -> b) -> Many param a -> Many param b
apply Many param (a -> b)
manyFs Many param a
manyAs =
[param] -> (param -> Either NotAKey b) -> Many param b
forall k a. [k] -> (k -> Either NotAKey a) -> Many k a
fromKeys (Many param (a -> b) -> [param]
forall k a. Many k a -> [k]
keys Many param (a -> b)
manyFs) param -> Either NotAKey b
applyF
where
applyF :: param -> Either NotAKey b
applyF param
param =
param -> Many param (a -> b) -> Either NotAKey (a -> b)
forall k a. k -> Many k a -> Either NotAKey a
lookup param
param Many param (a -> b)
manyFs Either NotAKey (a -> b) -> Either NotAKey a -> Either NotAKey b
forall a b.
Either NotAKey (a -> b) -> Either NotAKey a -> Either NotAKey b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> param -> Many param a -> Either NotAKey a
forall k a. k -> Many k a -> Either NotAKey a
lookup param
param Many param a
manyAs
compose :: Many b c -> Many a b -> Many a c
compose :: forall b c a. Many b c -> Many a b -> Many a c
compose Many b c
manyBC Many a b
manyAB =
[a] -> (a -> Either NotAKey c) -> Many a c
forall k a. [k] -> (k -> Either NotAKey a) -> Many k a
fromKeys (Many a b -> [a]
forall k a. Many k a -> [k]
keys Many a b
manyAB) a -> Either NotAKey c
aToC
where
aToC :: a -> Either NotAKey c
aToC a
a = do
b
b <- a -> Many a b -> Either NotAKey b
forall k a. k -> Many k a -> Either NotAKey a
lookup a
a Many a b
manyAB
b -> Many b c -> Either NotAKey c
forall k a. k -> Many k a -> Either NotAKey a
lookup b
b Many b c
manyBC
keys :: Many k a -> [k]
keys :: forall k a. Many k a -> [k]
keys (Many [k]
ks k -> Either NotAKey a
_) =
[k]
ks
elems :: Many k a -> [a]
elems :: forall k a. Many k a -> [a]
elems (Many [k]
ks k -> Either NotAKey a
keyToValue) =
[Either NotAKey a] -> [a]
forall a b. [Either a b] -> [b]
Either.rights ([Either NotAKey a] -> [a]) -> [Either NotAKey a] -> [a]
forall a b. (a -> b) -> a -> b
$ (k -> Either NotAKey a) -> [k] -> [Either NotAKey a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap k -> Either NotAKey a
keyToValue [k]
ks
toMap :: Ord k => Many k a -> Map.Map k a
toMap :: forall k a. Ord k => Many k a -> Map k a
toMap (Many [k]
ks k -> Either NotAKey a
keyToValue) =
[(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ((k -> Maybe (k, a)) -> [k] -> [(k, a)]
forall a b. (a -> Maybe b) -> [a] -> [b]
Maybe.mapMaybe k -> Maybe (k, a)
mkPair [k]
ks)
where
mkPair :: k -> Maybe (k, a)
mkPair k
k =
case k -> Either NotAKey a
keyToValue k
k of
Left NotAKey
NotAKey ->
Maybe (k, a)
forall a. Maybe a
Nothing
Right a
value ->
(k, a) -> Maybe (k, a)
forall a. a -> Maybe a
Just (k
k, a
value)
lookup :: k -> Many k a -> Either NotAKey a
lookup :: forall k a. k -> Many k a -> Either NotAKey a
lookup k
k (Many [k]
_ k -> Either NotAKey a
keyToValue) =
k -> Either NotAKey a
keyToValue k
k