Skip to content

Commit dc97993

Browse files
TomTom
authored andcommitted
WIP matchMaybe
1 parent 4faa2ec commit dc97993

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/Opaleye/Internal/MaybeFields.hs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Opaleye.Internal.Operators ((.&&), (.||), (.==), restrict, not,
3535
ifExplict, IfPP, EqPP(EqPP))
3636
import qualified Opaleye.Internal.Lateral
3737
import qualified Opaleye.SqlTypes
38-
import Opaleye.SqlTypes (SqlBool, IsSqlType)
38+
import Opaleye.SqlTypes (SqlBool, IsSqlType, SqlInt4)
3939

4040
import Control.Monad (replicateM_)
4141

@@ -92,6 +92,15 @@ justFields = pure
9292
maybeFields :: PP.Default IfPP b b => b -> (a -> b) -> MaybeFields a -> b
9393
maybeFields = maybeFieldsExplicit PP.def
9494

95+
matchMaybe :: PP.Default IfPP b b => MaybeFields a -> (Maybe a -> b) -> b
96+
matchMaybe mf f = maybeFields (f Nothing) (f . Just) mf
97+
98+
example :: MaybeFields (Field SqlInt4)
99+
-> Field SqlInt4
100+
example mf = matchMaybe mf $ \case
101+
Nothing -> 0
102+
Just x -> x * 100
103+
95104
-- | The Opaleye analogue of 'Data.Maybe.fromMaybe'
96105
fromMaybeFields :: PP.Default IfPP b b => b -> MaybeFields b -> b
97106
fromMaybeFields = fromMaybeFieldsExplicit PP.def

0 commit comments

Comments
 (0)