optics-core-0.4.1.1: Optics as an abstract interface: core definitions
Safe HaskellSafe-Inferred
LanguageHaskell2010

Numeric.Optics

Description

 
Synopsis

Documentation

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

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 Source #

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

Arithmetic lenses

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

adding n = iso (+n) (subtract n)
>>> [1..3] ^.. traversed % adding 1000
[1001,1002,1003]

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

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

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

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 ^. re fahrenheit
110.0

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

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

Note: This errors for n = 0

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

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

Note: This errors for n = 0

>>> au (coerced1 @Sum % re (exponentiating 2)) (foldMapOf each) (3,4) == 5
True

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

negated = iso negate negate
>>> au (coerced1 @Sum % negated) (foldMapOf each) (3,4) == 7
True
>>> au (coerced1 @Sum) (foldMapOf (each % negated)) (3,4) == -7
True

pattern Integral :: forall a. Integral a => a -> Integer Source #

Pattern synonym that can be used to construct or pattern match on an Integer as if it were of any Integral type.