module Lens.Family (
to, view, (^.)
, folding, views, (^..), (^?)
, toListOf, allOf, anyOf, firstOf, lastOf, sumOf, productOf
, lengthOf, nullOf
, backwards
, over, (%~), set, (.~)
, (&)
, (+~), (*~), (-~), (//~), (&&~), (||~), (<>~)
, LensLike, LensLike'
, FoldLike, FoldLike'
, ASetter, ASetter'
, Phantom
, Constant, Identity
, Applicative, Foldable, Monoid
, Backwards, All, Any, First, Last, Sum, Product
) where
import Control.Applicative (Applicative)
import Control.Applicative.Backwards (Backwards(..))
import Data.Foldable (Foldable, traverse_)
import Data.Functor.Identity (Identity(..))
import Data.Functor.Constant (Constant(..))
import Data.Monoid ( Monoid, mappend
, All(..), Any(..)
, First(..), Last(..)
, Sum(..), Product(..)
)
import Lens.Family.Phantom (Phantom, coerce)
import Lens.Family.Unchecked ( LensLike, LensLike' )
type FoldLike r a a' b b' = LensLike (Constant r) a a' b b'
type FoldLike' r a b = LensLike' (Constant r) a b
type ASetter a a' b b' = LensLike Identity a a' b b'
type ASetter' a b = LensLike' Identity a b
to :: Phantom f => (a -> b) -> LensLike f a a' b b'
to p f = coerce . f . p
view :: FoldLike b a a' b b' -> a -> b
view l = (^.l)
folding :: (Foldable g, Phantom f, Applicative f) => (a -> g b) -> LensLike f a a' b b'
folding p f = coerce . traverse_ f . p
views :: FoldLike r a a' b b' -> (b -> r) -> a -> r
views l f = getConstant . l (Constant . f)
toListOf :: FoldLike [b] a a' b b' -> a -> [b]
toListOf l = views l (:[])
allOf :: FoldLike All a a' b b' -> (b -> Bool) -> a -> Bool
allOf l p = getAll . views l (All . p)
anyOf :: FoldLike Any a a' b b' -> (b -> Bool) -> a -> Bool
anyOf l p = getAny . views l (Any . p)
firstOf :: FoldLike (First b) a a' b b' -> a -> Maybe b
firstOf l = getFirst . views l (First . Just)
lastOf :: FoldLike (Last b) a a' b b' -> a -> Maybe b
lastOf l = getLast . views l (Last . Just)
sumOf :: Num b => FoldLike (Sum b) a a' b b' -> a -> b
sumOf l = getSum . views l Sum
productOf :: Num b => FoldLike (Product b) a a' b b' -> a -> b
productOf l = getProduct . views l Product
lengthOf :: Num r => FoldLike (Sum r) a a' b b' -> a -> r
lengthOf l = getSum . views l (const (Sum 1))
nullOf :: FoldLike All a a' b b' -> a -> Bool
nullOf l = allOf l (const False)
infixr 8 ^.
(^.) :: a -> FoldLike b a a' b b' -> b
x^.l = getConstant $ l Constant x
infixr 8 ^..
(^..) :: a -> FoldLike [b] a a' b b' -> [b]
x^..l = toListOf l x
infixr 8 ^?
(^?) :: a -> FoldLike (First b) a a' b b' -> Maybe b
x^?l = firstOf l x
backwards :: LensLike (Backwards f) a a' b b' -> LensLike f a a' b b'
backwards l f = forwards . l (Backwards . f)
over :: ASetter a a' b b' -> (b -> b') -> a -> a'
over l = (l %~)
infixr 4 %~
(%~) :: ASetter a a' b b' -> (b -> b') -> a -> a'
l %~ f = runIdentity . l (Identity . f)
infixr 4 .~
(.~) :: ASetter a a' b b' -> b' -> a -> a'
l .~ b = l %~ const b
set :: ASetter a a' b b' -> b' -> a -> a'
set = (.~)
infixl 1 &
(&) :: a -> (a -> b) -> b
(&) = flip ($)
infixr 4 +~, -~, *~
(+~), (-~), (*~) :: Num b => ASetter' a b -> b -> a -> a
f +~ b = f %~ (+ b)
f -~ b = f %~ subtract b
f *~ b = f %~ (* b)
infixr 4 //~
(//~) :: Fractional b => ASetter' a b -> b -> a -> a
f //~ b = f %~ (/ b)
infixr 4 &&~, ||~
(&&~), (||~) :: ASetter' a Bool -> Bool -> a -> a
f &&~ b = f %~ (&& b)
f ||~ b = f %~ (|| b)
infixr 4 <>~
(<>~) :: (Monoid o) => ASetter' a o -> o -> a -> a
f <>~ o = f %~ (`mappend` o)