on 03b366b help sign in
rel8/src/Rel8/Query/Null.hs
38 lines 864 B history raw
1module Rel8.Query.Null
2 ( catNullable
3 )
4where
6-- base
7import Prelude
9-- rel8
10import Rel8.Expr ( Expr )
11import Rel8.Expr.Null ( isNonNull, unsafeUnnullify )
12import Rel8.Query ( Query )
13import Rel8.Query.Filter ( where_ )
16-- | Filter a 'Query' that might return @null@ to a 'Query' without any
17-- @null@s.
18--
19-- Corresponds to 'Data.Maybe.catMaybes'.
20--
21-- >>> select c $ pure (nullExpr :: Expr (Maybe Bool))
22-- [Nothing]
23--
24-- >>> select c $ catNullable (nullExpr :: Expr (Maybe Bool))
25-- []
26--
27-- >>> select c $ catNullable (lit (Just True))
28-- [True]
29--
30-- Notice how in the last example a @Bool@ is returned (rather than @Maybe
31-- Bool@):
32--
33-- >>> :t catNullable (lit (Just True))
34-- catMaybe (lit (Just True)) :: Query (Expr Bool)
35catNullable :: Expr (Maybe a) -> Query (Expr a)
36catNullable a = do
37 where_ $ isNonNull a
38 pure $ unsafeUnnullify a