lens-3.8.7.3: Lenses, Folds and Traversals

Portability | portable
Stability | provisional
Maintainer | Edward Kmett <ekmett@gmail.com>
Safe Haskell | Safe-Infered

Control.Lens.Combinators

Description

These are general purpose combinators that provide utility for non-lens code

Synopsis

- (&) :: a -> (a -> b) -> b
- (<&>) :: Functor f => f a -> (a -> b) -> f b
- (??) :: Functor f => f (a -> b) -> a -> f b

# Documentation

(&) :: a -> (a -> b) -> b

Passes the result of the left side to the function on the right side (forward pipe operator).

This is the flipped version of (`$`), which is more common in languages like F# as (`|>`) where it is needed for inference. Here it is supplied for notational convenience and given a precedence that allows it to be nested inside uses of (`$`).

    >>> a & f
    f a

    >>> "hello" & length & succ
    6

This combinator is commonly used when applying multiple `Lens` operations in sequence.

    >>> ("hello","world") & _1.element 0 .~ 'j' & _1.element 4 .~ 'y'
    ("jelly","world")

This reads somewhat similar to:

    >>> flip execState ("hello","world") $ do _1.element 0 .= 'j'; _1.element 4 .= 'y'
    ("jelly","world")

(<&>) :: Functor f => f a -> (a -> b) -> f b

Infix flipped `fmap`.

     (<&>) = flip fmap

(??) :: Functor f => f (a -> b) -> a -> f b

This is convenient to `flip` argument order of composite functions.

    >>> over _2 ?? ("hello","world") $ length
    ("hello",5)

    >>> over ?? length ?? ("hello","world") $ _2
    ("hello",5)