hsmisc-1.2: A collection of miscellaneous modules

Safe HaskellSafe
LanguageHaskell98

HsMisc.Control.Monad.MonadOr

Description

The MonadPlus instance for Maybe works like a logical or:

(Just x) `mplus` _ == Just x
Nothing  `mplus` y == y

But the MonadPlus instance for List works like concatination:

[1, 2, 3] `mplus` [10, 20] == [1, 2, 3, 10, 20]
[] `mplus` [1, 2, 3]       == [1, 2, 3]

I ran into a situation where I wanted the former behavior for lists, success and failure:

[1, 2, 3] `morelse` [10, 20] == [1, 2, 3]
[] `morelse` [10, 20]        == [10, 20]

Perhaps to express a default value:

let x = ""
let y = x `morelse` "default value"
y == "default value"

To fulfill that need, this module implements a MonadOr typeclass with morelse

Synopsis

Documentation

class MonadPlus m => MonadOr m where Source

Example of use for lists:

import HsMisc.Control.Monad.MonadOr
instance MonadOr []

Minimal complete definition

Nothing

Methods

morelse :: Eq (m a) => m a -> m a -> m a Source