bool-extras-0.4.0: A fold function for Bool

Safe HaskellSafe
LanguageHaskell98

Data.Bool.Extras

Contents

Description

This module provides some convenient functions for dealing with Booleans.

The most important one being bool, a function that can be used in place of the build-in if then else-syntax.

Synopsis

Main function

bool :: a -> a -> Bool -> a Source #

Case analysis for the Bool type. bool x y p evaluates to x when p is False, and evaluates to y when p is True.

This is equivalent to if p then y else x; that is, one can think of it as an if-then-else construct with its arguments reordered.

Examples

Expand

Basic usage:

>>> bool "foo" "bar" True
"bar"
>>> bool "foo" "bar" False
"foo"

Confirm that bool x y p and if p then y else x are equivalent:

>>> let p = True; x = "bar"; y = "foo"
>>> bool x y p == if p then y else x
True
>>> let p = False
>>> bool x y p == if p then y else x
True

Since: base-4.7.0.0

Other functions

mwhen :: Monoid a => a -> Bool -> a Source #

Boolean operation for monoids.

Returns its first argument when applied to True, returns mempty when applied to False.

mwhenM :: (Monad m, Monoid a) => m a -> Bool -> m a Source #

Boolean operation for monads, with a monoid default.

Return its first argument when applied to True, returns `return mempty' when applied to False.

whenA :: Arrow a => a b b -> Bool -> a b b Source #

Boolean operation for arrows.

Returns its first argument when applied to True, returns returnA when applied to False.

whenC :: Category cat => cat a a -> Bool -> cat a a Source #

Boolean operation for categories.

Returns its first argument when applied to True, returns Control.Category.id when applied to False.

whenM :: Monad m => (a -> m a) -> Bool -> a -> m a Source #

Boolean operation for monads.

Returns its first argument when applied to True, returns return when applied to False.

Control.Monad.when can be expressed in terms of whenM, like so:

when :: Monad m => Bool -> m () -> m ()
when b m = (const m `whenM` b) ()

Morphisms

type BoolAlgebra r = (r, r) Source #

Algebra for Bool data type.

The first field of the pair represents the False value, the second field represents the True value.

cata :: BoolAlgebra r -> Bool -> r Source #

Catamorphism for booleans.

ana :: (b -> Bool) -> b -> Bool Source #

Anamorphism for booleans.