1{-# language FlexibleContexts #-}
3module Rel8.Query.Either
7 , bitraverseEitherTable
12import Data.Functor ( (<&>) )
16import Rel8.Expr ( Expr )
17import Rel8.Expr.Eq ( (==.) )
18import Rel8.Query ( Query )
19import Rel8.Query.Filter ( where_ )
20import Rel8.Query.Maybe ( optional )
21import Rel8.Table ( Table )
22import Rel8.Table.Bool ( bool )
23import Rel8.Table.Either
24 ( EitherTable( EitherTable )
25 , isLeftTable, isRightTable
27import Rel8.Table.Maybe ( MaybeTable( MaybeTable ), isJustTable )
30-- | Filter 'EitherTable's, keeping only 'leftTable's.
31keepLeftTable :: EitherTable a b -> Query a
32keepLeftTable e@(EitherTable _ a _) = do
33 where_ $ isLeftTable e
37-- | Filter 'EitherTable's, keeping only 'rightTable's.
38keepRightTable :: EitherTable a b -> Query b
39keepRightTable e@(EitherTable _ _ b) = do
40 where_ $ isRightTable e
44-- | Extend an 'EitherTable' with a 'Query' if it's a 'rightTable'. If the
45-- @EitherTable@ is a 'leftTable', the original value is preserved.
47-- This is like the '>>=' implementation for @ExceptT@.
48bindEitherTable :: (Table Expr a, Functor m)
49 => (i -> m (EitherTable a b)) -> EitherTable a i -> m (EitherTable a b)
50bindEitherTable query e@(EitherTable input a i) = do
51 query i <&> \(EitherTable output a' b) ->
52 EitherTable (input <> output) (bool a a' (isRightTable e)) b
55-- | Extend an 'EitherTable' with queries for both 'leftTable' and
57bitraverseEitherTable :: ()
61 -> Query (EitherTable c d)
62bitraverseEitherTable f g e@(EitherTable tag _ _) = do
63 mc@(MaybeTable _ c) <- optional (f =<< keepLeftTable e)
64 md@(MaybeTable _ d) <- optional (g =<< keepRightTable e)
65 where_ $ isJustTable mc ==. isLeftTable e
66 where_ $ isJustTable md ==. isRightTable e
67 pure $ EitherTable tag c d
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