Skip to content

Commit

Permalink
[#293] Add memptyIfTrue/False functions (#295)
Browse files Browse the repository at this point in the history
Resolves #293
  • Loading branch information
vrom911 committed May 14, 2020
1 parent 9001bc3 commit 80ffb14
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ The changelog is available [on GitHub][2].
`[f]map (mapToFst f)` and `[f]map (mapToSnd f)`
* Reexport `NonEmpty` functions from `Relude.List.NonEmpty` instead of
`Relude.List.Reexport`.
* [#293](https://github.com/kowainik/relude/issues/293):
Add `memptyIfFalse` and `memptyIfTrue` functions.

## 0.6.0.0 — Oct 30, 2019

Expand Down
32 changes: 32 additions & 0 deletions src/Relude/Monoid.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ module Relude.Monoid

-- * Combinators
, maybeToMonoid
, memptyIfFalse
, memptyIfTrue
) where

#if MIN_VERSION_base(4,12,0)
Expand All @@ -38,6 +40,7 @@ import Data.Monoid (All (..), Alt (..), Any (..), Dual (..), Endo (..), First (.
import Data.Semigroup (Option (..), Semigroup (sconcat, stimes, (<>)), WrappedMonoid, cycle1,
mtimesDefault, stimesIdempotent, stimesIdempotentMonoid, stimesMonoid)

import Relude.Bool.Reexport (Bool (..))
import Relude.Monad.Reexport (Maybe, fromMaybe)

#if !MIN_VERSION_base(4,12,0)
Expand Down Expand Up @@ -68,6 +71,35 @@ maybeToMonoid :: Monoid m => Maybe m -> m
maybeToMonoid = fromMaybe mempty
{-# INLINE maybeToMonoid #-}

{- | Returns the given value in case of the given predicate is satisfied
(is 'True'). Otherwise, it returns 'mempty'.
>>> memptyIfFalse True (Just "Hello")
Just "Hello"
>>> memptyIfFalse False "Doesn't matter"
""
@since 0.7.0.0
-}
memptyIfFalse :: Monoid m => Bool -> m -> m
memptyIfFalse p val = if p then val else mempty
{-# INLINE memptyIfFalse #-}


{- | Returns the given value in case of the given predicate is unsatisfied
(is 'False'). Otherwise, it returns 'mempty'.
>>> memptyIfTrue True (Just "Hello")
Nothing
>>> memptyIfTrue False "Does matter"
"Does matter"
@since 0.7.0.0
-}
memptyIfTrue :: Monoid m => Bool -> m -> m
memptyIfTrue p val = if p then mempty else val
{-# INLINE memptyIfTrue #-}

#if !MIN_VERSION_base(4,12,0)
-- | This data type witnesses the lifting of a 'Monoid' into an
-- 'Applicative' pointwise.
Expand Down

0 comments on commit 80ffb14

Please sign in to comment.