This seems long, but lately I have needed the following:
mjoin :: (Monoid b, Monad m) => m b -> m b -> m b
mjoin a b = do
a' <- a
b' <- b
return $ mappend a' b'
The example uses the following:
> mjoin (Just [1,2,3]) (Just [4, 5, 6])
Just [1,2,3,4,5,6]
> mjoin (Just [1,2,3]) Nothing
Nothing
> mjoin Nothing (Just [4, 5, 6])
Nothing
In other words, if both parameters Nothing, return Nothing. Otherwise, return the Justadded values.
Is there a standard function for this or a simpler formulation, possibly with >>=?
source
share