lens-5.2.3: Lenses, Folds and Traversals

Copyright | (C) 2012-16 Edward Kmett
License | BSD-style (see the file LICENSE)
Maintainer | Edward Kmett <ekmett@gmail.com>
Stability | provisional
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell2010

Numeric.Lens

Contents

- Predefined bases
- Arithmetic lenses

Description

Synopsis

- base :: (HasCallStack, Integral a) => Int -> Prism' String a
- integral :: (Integral a, Integral b) => Prism Integer Integer a b
- binary :: Integral a => Prism' String a
- octal :: Integral a => Prism' String a
- decimal :: Integral a => Prism' String a
- hex :: Integral a => Prism' String a
- adding :: Num a => a -> Iso' a a
- subtracting :: Num a => a -> Iso' a a
- multiplying :: (Fractional a, Eq a) => a -> Iso' a a
- dividing :: (Fractional a, Eq a) => a -> Iso' a a
- exponentiating :: (Floating a, Eq a) => a -> Iso' a a
- negated :: Num a => Iso' a a
- pattern Integral :: Integral a => a -> Integer

# Documentation

base :: (HasCallStack, Integral a) => Int -> Prism' String a #

A prism that shows and reads integers in base-2 through base-36

Note: This is an improper prism, since leading 0s are stripped when reading.

    >>> "100" ^? base 16
    Just 256

    >>> 1767707668033969 ^. re (base 36)
    "helloworld"

integral :: (Integral a, Integral b) => Prism Integer Integer a b #

This `Prism` can be used to model the fact that every `Integral` type is a subset of `Integer`.

Embedding through the `Prism` only succeeds if the `Integer` would pass through unmodified when re-extracted.

# Predefined bases

binary :: Integral a => Prism' String a #

    binary = base 2

octal :: Integral a => Prism' String a #

    octal = base 8

decimal :: Integral a => Prism' String a #

    decimal = base 10

hex :: Integral a => Prism' String a #

    hex = base 16

# Arithmetic lenses

adding :: Num a => a -> Iso' a a #

    adding n = iso (+n) (subtract n)

    >>> [1..3]^..traverse.adding 1000
    [1001,1002,1003]

subtracting :: Num a => a -> Iso' a a #

    subtracting n = iso (subtract n) ((+n)
    subtracting n = from (adding n)

multiplying :: (Fractional a, Eq a) => a -> Iso' a a #

    multiplying n = iso (*n) (/n)

Note: This errors for n = 0

    >>> 5 & multiplying 1000 +~ 3
    5.003

    >>> let fahrenheit = multiplying (9/5).adding 32 in 230^.from fahrenheit
    110.0

dividing :: (Fractional a, Eq a) => a -> Iso' a a #

     dividing n = iso (/n) (*n)
     dividing n = from (multiplying n)

Note: This errors for n = 0

exponentiating :: (Floating a, Eq a) => a -> Iso' a a #

    exponentiating n = iso (**n) (**recip n)

Note: This errors for n = 0

    >>> au (_Wrapping Sum . from (exponentiating 2)) (foldMapOf each) (3,4) == 5
    True

negated :: Num a => Iso' a a #

    negated = iso negate negate

    >>> au (_Wrapping Sum . negated) (foldMapOf each) (3,4) == 7
    True

    >>> au (_Wrapping Sum) (foldMapOf (each.negated)) (3,4) == -7
    True

pattern Integral :: Integral a => a -> Integer #