Data.Function.Compat

base-compat-0.14.1: A compatibility layer for base

Safe Haskell | Safe-Inferred
Language | Haskell2010

Data.Function.Compat

Synopsis

- module Data.Function
- (&) :: a -> (a -> b) -> b
- applyWhen :: Bool -> (a -> a) -> a -> a

# Documentation

module Data.Function

(&) :: a -> (a -> b) -> b infixl 1 #

`&` is a reverse application operator. This provides notational convenience. Its precedence is one higher than that of the forward application operator `$`, which allows `&` to be nested in `$`.

This is a version of ``flip` `id``, where `id` is specialized from `a -> a` to `(a -> b) -> (a -> b)` which by the associativity of `(->)` is `(a -> b) -> a -> b`. flipping this yields `a -> (a -> b) -> b` which is the type signature of `&`

#### Examples

Expand

    >>> 5 & (+1) & show
    "6"

    >>> sqrt $ [1 / n^2 | n <- [1..1000]] & sum & (*6)
    3.1406380562059946

@since base-4.8.0.0

applyWhen :: Bool -> (a -> a) -> a -> a #

`applyWhen` applies a function to a value if a condition is true, otherwise, it returns the value unchanged.

It is equivalent to ``flip` (`bool` `id`)`.

#### Examples

Expand

    >>> map (\x -> applyWhen (odd x) (*2) x) [1..10]
    [2,2,6,4,10,6,14,8,18,10]

    >>> map (\x -> applyWhen (length x > 6) ((++ "...") . take 3) x) ["Hi!", "This is amazing", "Hope you're doing well today!", ":D"]
    ["Hi!","Thi...","Hop...",":D"]

#### Algebraic properties

Expand

-     applyWhen True = id

-     applyWhen False f = id

@since base-4.18.0.0