>>> fromMaybe "" (Just "Hello, World!") "Hello, World!"
>>> fromMaybe "" Nothing ""Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:
>>> import GHC.Internal.Text.Read ( readMaybe ) >>> fromMaybe 0 (readMaybe "5") 5 >>> fromMaybe 0 (readMaybe "") 0
>>> fromMaybe "" (Just "Hello, World!") "Hello, World!"
>>> fromMaybe "" Nothing ""Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:
>>> import Text.Read ( readMaybe ) >>> fromMaybe 0 (readMaybe "5") 5 >>> fromMaybe 0 (readMaybe "") 0
>>> fromMaybe "" (Just "Hello, World!") "Hello, World!"
>>> fromMaybe "" Nothing ""Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:
>>> import Text.Read ( readMaybe ) >>> fromMaybe 0 (readMaybe "5") 5 >>> fromMaybe 0 (readMaybe "") 0
>>> fromMaybe 2 (sNothing :: SMaybe Integer) 2 :: SInteger >>> fromMaybe 2 (sJust 5 :: SMaybe Integer) 5 :: SInteger >>> prove $ \x -> fromMaybe x (sNothing :: SMaybe Integer) .== x Q.E.D. >>> prove $ \x -> fromMaybe (x+1) (sJust x :: SMaybe Integer) .== x Q.E.D.
>>> :{
let owners :: [(Person,Maybe House)]
owners =
[ (Person "Jordan" 18, Nothing)
, (Person "Ruth" 25, Just (House Red 125000))
, (Person "Sonia" 12, Just (House Green 145000))
]
:}
The column encodings defined earlier can be reused with the help of
fromMaybe:
>>> :{
let colOwners :: Colonnade Headed (Person,Maybe House) String
colOwners = mconcat
[ lmap fst colPerson
, lmap snd (fromMaybe "" colHouse)
]
:}
>>> putStr (ascii colOwners owners) +--------+-----+-------+---------+ | Name | Age | Color | Price | +--------+-----+-------+---------+ | Jordan | 18 | | | | Ruth | 25 | Red | $125000 | | Sonia | 12 | Green | $145000 | +--------+-----+-------+---------+
>>> fromMaybe "" (Just "Hello, World!") "Hello, World!"
>>> fromMaybe "" Nothing ""Read an integer from a string using readMaybe. If we fail to parse an integer, we want to return 0 by default:
>>> import Text.Read ( readMaybe ) >>> fromMaybe 0 (readMaybe "5") 5 >>> fromMaybe 0 (readMaybe "") 0
instance Monad m => Semigroup (FromMaybe m a) where mappend (FromMaybe f) (FromMaybe g) = FromMaybeM (f . Just . g)