packages feed

lens 1.5 → 1.6

raw patch · 8 files changed

+521/−110 lines, 8 files

Files

lens.cabal view
@@ -1,6 +1,6 @@ name:          lens category:      Data, Lenses-version:       1.5+version:       1.6 license:       BSD3 cabal-version: >= 1.8 license-file:  LICENSE
src/Control/Lens/Fold.hs view
@@ -15,16 +15,19 @@ -- -- A @'Fold' a c@ is a generalization of something 'Foldable'. It allows you to -- extract multiple results from a container. A 'Foldable' container can be--- characterized by the behavior of @foldMap :: (Foldable t, Monoid m) => (c -> m) -> t c -> m@.+-- characterized by the behavior of @foldMap :: ('Foldable' t, 'Monoid' m) => (c -> m) -> t c -> m@. -- Since we want to be able to work with monomorphic containers, we generalize this signature to -- @forall m. 'Monoid' m => (c -> m) -> a -> m@, and then decorate it with 'Const' to obtain ----- > type Fold a c = forall m b d. Monoid m => Getting m a b c d+-- @type 'Fold' a c = forall m b d. 'Monoid' m => 'Getting' m a b c d@ --+-- In practice the type we use is slightly more complicated to allow for better error messages and+-- for it to be transformed by certain 'Applicative' transformers.+-- -- Every 'Getter' is a valid 'Fold' that simply doesn't use the 'Monoid' it is passed. -- -- Everything you can do with a 'Foldable' container, you can with with a 'Fold' and there are--- combinators that generalize the usual 'Foldable' operations in @Control.Lens@.+-- combinators that generalize the usual 'Foldable' operations here. ---------------------------------------------------------------------------- module Control.Lens.Fold   (@@ -89,7 +92,7 @@ -- typeclass, see 'foldMapOf' and the other 'Fold' combinators. -- -- By convention, if there exists a 'foo' method that expects a @'Foldable' (f c)@, then there should be a--- 'fooOf' method that takes a @'Fold' a c@ and a value of type @a@.+-- @fooOf@ method that takes a @'Fold' a c@ and a value of type @a@. -- -- A 'Getter' is a legal 'Fold' that just ignores the supplied 'Monoid' --@@ -384,7 +387,7 @@ -- > forOf_ :: Functor f     => Iso a b c d       -> a -> (c -> f e) -> f () -- > forOf_ :: Applicative f => Traversal a b c d -> a -> (c -> f e) -> f () forOf_ :: Functor f => Getting (Traversed f) a b c d -> a -> (c -> f e) -> f ()-forOf_ l a f = traverseOf_ l f a+forOf_ = flip . traverseOf_ {-# INLINE forOf_ #-}  -- |@@ -424,7 +427,7 @@ -- > forMOf_ :: Monad m => Iso a b c d       -> a -> (c -> m e) -> m () -- > forMOf_ :: Monad m => Traversal a b c d -> a -> (c -> m e) -> m () forMOf_ :: Monad m => Getting (Sequenced m) a b c d -> a -> (c -> m e) -> m ()-forMOf_ l a f = mapMOf_ l f a+forMOf_ = flip . mapMOf_ {-# INLINE forMOf_ #-}  -- |@@ -498,7 +501,7 @@ -- > concatMapOf :: Iso a b c d       -> (c -> [e]) -> a -> [e] -- > concatMapOf :: Traversal a b c d -> (c -> [e]) -> a -> [e] concatMapOf :: Getting [e] a b c d -> (c -> [e]) -> a -> [e]-concatMapOf l ces a = runAccessor (l (Accessor . ces) a)+concatMapOf l ces = runAccessor . l (Accessor . ces) {-# INLINE concatMapOf #-}  -- |
src/Control/Lens/Indexed.hs view
@@ -20,23 +20,41 @@   , Indexable   , Index(..)   , (.@)-  , composeWithIndex+  , icompose   , reindex -  -- * Indexed Folds-  , IndexedFold-  , foldMapWithIndexOf-  , foldrWithIndexOf+  -- * Indexed Setter+  , IndexedSetter+  , imapOf+  , (%@)    -- * Indexed Traversals   , IndexedTraversal-  , traverseWithIndexOf-  , mapMWithIndexOf+  , itraverseOf+  , iforOf+  , imapMOf+  , iforMOf+  , imapAccumROf+  , imapAccumLOf -  -- * Indexed Setter-  , IndexedSetter-  , mapWithIndexOf-  , (%@)+  -- * Indexed Folds+  , IndexedFold+  , IndexedFolding+  , ifoldMapOf+  , ifoldrOf+  , ifoldlOf+  , ianyOf+  , iallOf+  , itraverseOf_+  , iforOf_+  , imapMOf_+  , iforMOf_+  , iconcatMapOf+  -- , imaximumByOf , iminimumByOf , ifindOf+  , ifoldrOf'+  , ifoldlOf'+  , ifoldrMOf+  , ifoldlMOf    -- * Simple   , SimpleIndexedTraversal@@ -44,9 +62,14 @@   ) where  import Control.Applicative-import Control.Lens.Type+import Control.Applicative.Backwards import Control.Lens.Getter+import Control.Lens.Internal import Control.Lens.Setter+import Control.Lens.Type+import Control.Monad+import Control.Monad.State.Class as State+import Control.Monad.Trans.State.Lazy as Lazy import Data.Monoid  -- | Permit overloading of function application for things that also admit a notion of a key or index.@@ -82,16 +105,16 @@ infixr 9 .@ -- | Composition of indexed functions (.@) :: Indexed (i, j) k => Index i b c -> Index j a b -> k a c-f .@ g = composeWithIndex (,) f g+f .@ g = icompose (,) f g {-# INLINE (.@) #-} {-# SPECIALIZE (.@) :: Index i b c -> Index j a b -> Index (i,j) a c #-} {-# SPECIALIZE (.@) :: Index i b c -> Index j a b -> a -> c #-}  -- | Composition of indexed functions with a user supplied function for combining indexs-composeWithIndex :: Indexed k r => (i -> j -> k) -> Index i b c -> Index j a b -> r a c-composeWithIndex ijk (Index ibc) (Index jab) = index $ \ka -> ibc $ \i -> jab $ \j -> ka (ijk i j)-{-# INLINE composeWithIndex #-}-{-# SPECIALIZE composeWithIndex :: (i -> j -> k) -> Index i b c -> Index j a b -> a -> c #-}+icompose :: Indexed k r => (i -> j -> k) -> Index i b c -> Index j a b -> r a c+icompose ijk (Index ibc) (Index jab) = index $ \ka -> ibc $ \i -> jab $ \j -> ka (ijk i j)+{-# INLINE icompose #-}+{-# SPECIALIZE icompose :: (i -> j -> k) -> Index i b c -> Index j a b -> a -> c #-}  ------------------------------------------------------------------------------ -- Indexed Folds@@ -104,21 +127,161 @@  -- | ----- > foldMapWithIndexOf :: Monoid m => IndexedFold i a c          -> (i -> c -> m) -> a -> m--- > foldMapWithIndexOf :: Monoid m => IndexedTraversal i a b c d -> (i -> c -> m) -> a -> m-foldMapWithIndexOf :: IndexedFolding i m a b c d -> (i -> c -> m) -> a -> m-foldMapWithIndexOf l f = runAccessor . withIndex l (\i -> Accessor . f i)-{-# INLINE foldMapWithIndexOf #-}+-- > ifoldMapOf :: Monoid m => IndexedFold i a c          -> (i -> c -> m) -> a -> m+-- > ifoldMapOf :: Monoid m => IndexedTraversal i a b c d -> (i -> c -> m) -> a -> m+ifoldMapOf :: IndexedFolding i m a b c d -> (i -> c -> m) -> a -> m+ifoldMapOf l f = runAccessor . withIndex l (\i -> Accessor . f i)+{-# INLINE ifoldMapOf #-}  -- | -- Right-associative fold of parts of a structure that are viewed through a 'Lens', 'Getter', 'Fold' or 'Traversal'. ----- > foldrWithIndexOf :: IndexedFold i a c          -> (i -> c -> e -> e) -> e -> a -> e--- > foldrWithIndexOf :: IndexedTraversal i a b c d -> (i -> c -> e -> e) -> e -> a -> e-foldrWithIndexOf :: IndexedFolding i (Endo e) a b c d -> (i -> c -> e -> e) -> e -> a -> e-foldrWithIndexOf l f z t = appEndo (foldMapWithIndexOf l (\i -> Endo . f i) t) z-{-# INLINE foldrWithIndexOf #-}+-- > ifoldrOf :: IndexedFold i a c          -> (i -> c -> e -> e) -> e -> a -> e+-- > ifoldrOf :: IndexedTraversal i a b c d -> (i -> c -> e -> e) -> e -> a -> e+ifoldrOf :: IndexedFolding i (Endo e) a b c d -> (i -> c -> e -> e) -> e -> a -> e+ifoldrOf l f z t = appEndo (ifoldMapOf l (\i -> Endo . f i) t) z+{-# INLINE ifoldrOf #-} +-- |+-- Left-associative fold of the parts of a structure that are viewed through a 'Lens', 'Getter', 'Fold' or 'Traversal'.+--+-- > foldl = foldlOf folded+--+-- > ifoldlOf :: IndexedFold i a c          -> (i -> e -> c -> e) -> e -> a -> e+-- > ifoldlOf :: IndexedTraversal i a b c d -> (i -> e -> c -> e) -> e -> a -> e+ifoldlOf :: IndexedFolding i (Dual (Endo e)) a b c d -> (i -> e -> c -> e) -> e -> a -> e+ifoldlOf l f z t = appEndo (getDual (ifoldMapOf l (\i -> Dual . Endo . flip (f i)) t)) z+{-# INLINE ifoldlOf #-}+++-- |+-- > ianyOf :: IndexedFold i a c          -> (i -> c -> Bool) -> a -> Bool+-- > ianyOf :: IndexedTraversal i a b c d -> (i -> c -> Bool) -> a -> Bool+ianyOf :: IndexedFolding i Any a b c d -> (i -> c -> Bool) -> a -> Bool+ianyOf l f = getAny . ifoldMapOf l (\i -> Any . f i)+{-# INLINE ianyOf #-}++-- |+-- > iallOf :: IndexedFold i a c          -> (i -> c -> Bool) -> a -> Bool+-- > iallOf :: IndexedTraversal i a b c d -> (i -> c -> Bool) -> a -> Bool+iallOf :: IndexedFolding i All a b c d -> (i -> c -> Bool) -> a -> Bool+iallOf l f = getAll . ifoldMapOf l (\i -> All . f i)+{-# INLINE iallOf #-}++-- |+-- > itraverseOf_ :: Applicative f => IndexedFold i a c          -> (i -> c -> f e) -> a -> f ()+-- > itraverseOf_ :: Applicative f => IndexedTraversal i a b c d -> (i -> c -> f e) -> a -> f ()+itraverseOf_ :: Functor f => IndexedFolding i (Traversed f) a b c d -> (i -> c -> f e) -> a -> f ()+itraverseOf_ l f = getTraversed . ifoldMapOf l (\i -> Traversed . void . f i)+{-# INLINE itraverseOf_ #-}++-- |+-- > iforOf_ :: Applicative f => IndexedFold i a c          -> a -> (i -> c -> f e) -> f ()+-- > iforOf_ :: Applicative f => IndexedTraversal i a b c d -> a -> (i -> c -> f e) -> f ()+iforOf_ :: Functor f => IndexedFolding i (Traversed f) a b c d -> a -> (i -> c -> f e) -> f ()+iforOf_ = flip . itraverseOf_+{-# INLINE iforOf_ #-}++-- |+-- > imapMOf_ :: Monad m => IndexedFold i a c          -> (i -> c -> m e) -> a -> m ()+-- > imapMOf_ :: Monad m => IndexedTraversal i a b c d -> (i -> c -> m e) -> a -> m ()+imapMOf_ :: Monad m => IndexedFolding i (Sequenced m) a b c d -> (i -> c -> m e) -> a -> m ()+imapMOf_ l f = getSequenced . ifoldMapOf l (\i -> Sequenced . liftM skip . f i)+{-# INLINE imapMOf_ #-}++skip :: a -> ()+skip _ = ()+{-# INLINE skip #-}++-- |+-- > iforMOf_ :: Monad m => IndexedFold i a c          -> a -> (i -> c -> m e) -> m ()+-- > iforMOf_ :: Monad m => IndexedTraversal i a b c d -> a -> (i -> c -> m e) -> m ()+iforMOf_ :: Monad m => IndexedFolding i (Sequenced m) a b c d -> a -> (i -> c -> m e) -> m ()+iforMOf_ = flip . imapMOf_+{-# INLINE iforMOf_ #-}++-- |+-- > iconcatMapOf :: IndexedFold i a c          -> (i -> c -> [e]) -> a -> [e]+-- > iconcatMapOf :: IndexedTraversal i a b c d -> (i -> c -> [e]) -> a -> [e]+iconcatMapOf :: IndexedFolding i [e] a b c d -> (i -> c -> [e]) -> a -> [e]+iconcatMapOf l ices = runAccessor . withIndex l (\i -> Accessor . ices i)+{-# INLINE iconcatMapOf #-}++{-+-- |+-- Obtain the maximum element (if any) targeted by an 'IndexedFold' or 'IndexedTraversal'+-- according to a user supplied ordering with access to the indices, returning the index and result of the winning entry+--+-- > imaximumByOf :: IndexedFold a c          -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+-- > imaximumByOf :: IndexedTraversal a b c d -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+imaximumByOf :: IndexedFolding i (Endo (Maybe c)) a b c d -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+imaximumByOf l cmp = ifoldrOf l step Nothing where+  step i a Nothing  = Just (i, a)+  step i a (Just (j, b)) = Just $! if cmp i j a b == GT then (i, a) else (j, b)+{-# INLINE imaximumByOf #-}++-- |+-- Obtain the minimum element (if any) targeted by an 'IndexedFold' or 'IndexedTraversal'+-- according to a user supplied ordering with access to the indices, returning the index and result of the winning entry+--+-- > iminimumByOf :: IndexedFold a c          -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+-- > iminimumByOf :: IndexedTraversal a b c d -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+iminimumByOf :: IndexedFolding i (Endo (Maybe c)) a b c d -> (i -> i -> c -> c -> Ordering) -> a -> Maybe (i, c)+iminimumByOf l cmp = ifoldrOf l step Nothing where+  step i a Nothing  = Just (i, a)+  step i a (Just (j, b)) = Just $! if cmp i j a b == GT then (j, b) else (i, a)+{-# INLINE iminimumByOf #-}++-- | The 'findOf' function takes an IndexedFold or IndexedTraversal, a predicate,+-- a structure and returns the leftmost element of the structure+-- matching the predicate, or 'Nothing' if there is no such element.+--+-- > ifindOf :: IndexedFold a c          -> (i -> c -> Bool) -> a -> Maybe (i, c)+-- > ifindOf :: IndexedTraversal a b c d -> (i -> c -> Bool) -> a -> Maybe (i, c)+ifindOf :: IndexedFolding i (First c) a b c d -> (i -> c -> Bool) -> a -> Maybe (i, c)+ifindOf l p = getFirst . ifoldMapOf l step where+  step i c+    | p i c     = First (Just (i, c))+    | otherwise = First Nothing+{-# INLINE ifindOf #-}+-}++-- | Strictly fold right over the elements of a structure with an index.+--+-- > ifoldrOf' :: IndexedFold i a c          -> (i -> c -> e -> e) -> e -> a -> e+-- > ifoldrOf' :: IndexedTraversal i a b c d -> (i -> c -> e -> e) -> e -> a -> e+ifoldrOf' :: IndexedFolding i (Dual (Endo (e -> e))) a b c d -> (i -> c -> e -> e) -> e -> a -> e+ifoldrOf' l f z0 xs = ifoldlOf l f' id xs z0+  where f' i k x z = k $! f i x z+{-# INLINE ifoldrOf' #-}++-- | Fold over the elements of a structure with an index, associating to the left, but strictly.+--+-- > ifoldlOf' :: IndexedFold i a c            -> (i -> e -> c -> e) -> e -> a -> e+-- > ifoldlOf' :: IndexedTraversal i a b c d   -> (i -> e -> c -> e) -> e -> a -> e+ifoldlOf' :: IndexedFolding i (Endo (e -> e)) a b c d -> (i -> e -> c -> e) -> e -> a -> e+ifoldlOf' l f z0 xs = ifoldrOf l f' id xs z0+  where f' i x k z = k $! f i z x+{-# INLINE ifoldlOf' #-}++-- | Monadic fold right over the elements of a structure with an index.+--+-- > ifoldrMOf :: Monad m => IndexedFold i a c          -> (i -> c -> e -> m e) -> e -> a -> e+-- > ifoldrMOf :: Monad m => IndexedTraversal i a b c d -> (i -> c -> e -> m e) -> e -> a -> e+ifoldrMOf :: Monad m => IndexedFolding i (Dual (Endo (e -> m e))) a b c d -> (i -> c -> e -> m e) -> e -> a -> m e+ifoldrMOf l f z0 xs = ifoldlOf l f' return xs z0+  where f' i k x z = f i x z >>= k+{-# INLINE ifoldrMOf #-}++-- | Monadic fold over the elements of a structure with an index, associating to the left.+--+-- > ifoldlOf' :: Monad m => IndexedFold i a c            -> (i -> e -> c -> m e) -> e -> a -> e+-- > ifoldlOf' :: Monad m => IndexedTraversal i a b c d   -> (i -> e -> c -> m e) -> e -> a -> e+ifoldlMOf :: Monad m => IndexedFolding i (Endo (e -> m e)) a b c d -> (i -> e -> c -> m e) -> e -> a -> m e+ifoldlMOf l f z0 xs = ifoldrOf l f' return xs z0+  where f' i x k z = f i z x >>= k+{-# INLINE ifoldlMOf #-}+ ------------------------------------------------------------------------------ -- Indexed Traversals ------------------------------------------------------------------------------@@ -128,27 +291,61 @@ -- The Traversal laws are still required to hold. type IndexedTraversal i a b c d = forall f k. (Indexed i k, Applicative f) => k (c -> f d) (a -> f b) --- | @type 'SimpleIdexedTraversal i = 'Simple' ('IndexedTraversal' i)@+-- | @type 'SimpleIdexedTraversal' i = 'Simple' ('IndexedTraversal' i)@ type SimpleIndexedTraversal i a b = IndexedTraversal i a a b b +-- | Traversal with an index.+--+-- > itraverseOf = withIndex+--+-- > itraverseOf :: IndexedTraversal i a b c d -> (i -> c -> f d) -> a -> f b+itraverseOf :: Overloaded (Index i) f a b c d -> (i -> c -> f d) -> a -> f b+itraverseOf = withIndex+{-# INLINE itraverseOf #-}+ -- |--- > traverseWithIndexOf :: IndexedTraversal i a b c d -> (i -> c -> f d) -> a -> f b-traverseWithIndexOf :: Overloaded (Index i) f a b c d -> (i -> c -> f d) -> a -> f b-traverseWithIndexOf = withIndex-{-# INLINE traverseWithIndexOf #-}+-- > iforOf = flip . itraverseOf+iforOf :: Overloaded (Index i) f a b c d -> a -> (i -> c -> f d) -> f b+iforOf = flip . withIndex+{-# INLINE iforOf #-}  -- | Map each element of a structure targeted by a lens to a monadic action, -- evaluate these actions from left to right, and collect the results, with access -- its position. ----- > mapMWithIndexOf :: Monad m => IndexedTraversal a b c d -> (i -> c -> m d) -> a -> m b-mapMWithIndexOf :: Overloaded (Index i) (WrappedMonad m) a b c d -> (i -> c -> m d) -> a -> m b-mapMWithIndexOf l f = unwrapMonad . withIndex l (\i -> WrapMonad . f i)-{-# INLINE mapMWithIndexOf #-}+-- > imapMOf :: Monad m => IndexedTraversal a b c d -> (i -> c -> m d) -> a -> m b+imapMOf :: Overloaded (Index i) (WrappedMonad m) a b c d -> (i -> c -> m d) -> a -> m b+imapMOf l f = unwrapMonad . withIndex l (\i -> WrapMonad . f i)+{-# INLINE imapMOf #-} --- | Every indexed Setter is a valid Setter+-- |+-- > iforMOf = flip . imapMOf+iforMOf :: Overloaded (Index i) (WrappedMonad m) a b c d -> a -> (i -> c -> m d) -> m b+iforMOf = flip . imapMOf+{-# INLINE iforMOf #-}++-- | Generalizes 'Data.Traversable.mapAccumR' to an arbitrary 'IndexedTraversal'. ----- The Setter laws are still required to hold.+-- 'imapAccumROf' accumulates state from right to left.+--+imapAccumROf :: Overloaded (Index i) (Lazy.State s) a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+imapAccumROf l f s0 a = swap (Lazy.runState (withIndex l (\i c -> State.state (\s -> swap (f i s c))) a) s0)+{-# INLINE imapAccumROf #-}++-- | Generalized 'Data.Traversable.mapAccumL' to an arbitrary 'IndexedTraversal'.+--+-- 'imapAccumLOf' accumulates state from left to right.+imapAccumLOf :: Overloaded (Index i) (Backwards (Lazy.State s)) a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+imapAccumLOf l f s0 a = swap (Lazy.runState (forwards (withIndex l (\i c -> Backwards (State.state (\s -> swap (f i s c)))) a)) s0)+{-# INLINE imapAccumLOf #-}++swap :: (a,b) -> (b,a)+swap (a,b) = (b,a)+{-# INLINE swap #-}++-- | Every 'IndexedSetter' is a valid 'Setter'+--+-- The 'Setter' laws are still required to hold. type IndexedSetter i a b c d = forall f k. (Indexed i k, Settable f) => k (c -> f d) (a -> f b)  -- | @type 'SimpleIdexedTraversal i = 'Simple' ('IndexedTraversal' i)@@@ -156,13 +353,15 @@  -- | Map with index ----- > mapWithIndexOf :: IndexedSetter i a b c d -> (i -> c -> d) -> a -> b-mapWithIndexOf :: Overloaded (Index i) Mutator a b c d -> (i -> c -> d) -> a -> b-mapWithIndexOf l f = runMutator . withIndex l (\i -> Mutator . f i)+-- > imapOf :: IndexedTraversal i a b c d -> (i -> c -> d) -> a -> b+-- > imapOf :: IndexedSetter i a b c d -> (i -> c -> d) -> a -> b+imapOf :: Overloaded (Index i) Mutator a b c d -> (i -> c -> d) -> a -> b+imapOf l f = runMutator . withIndex l (\i -> Mutator . f i)+{-# INLINE imapOf #-}  infixr 4 %@ --- | > (%@) = mapWithIndexOf+-- | > (%@) = imapOf (%@) :: Overloaded (Index i) Mutator a b c d -> (i -> c -> d) -> a -> b l %@ f = runMutator . withIndex l (\i -> Mutator . f i)-+{-# INLINE (%@) #-}
src/Control/Lens/Iso.hs view
@@ -43,7 +43,7 @@   -- | Build this morphism out of an isomorphism   --   -- The intention is that by using 'isomorphic', you can supply both halves of an-  -- isomorphism, but k can be instantiated to (->), so you can freely use+  -- isomorphism, but k can be instantiated to @(->)@, so you can freely use   -- the resulting isomorphism as a function.   isomorphic :: (a -> b) -> (b -> a) -> k a b @@ -82,19 +82,19 @@ -- -- > from (from l) = l ----- If you imported 'Control.Category.(.)', then:+-- If you imported 'Control.Category..' from @Control.Category@, then: -- -- > from l . from r = from (r . l)------ > from :: (a :~> b) -> (b :~> a) from :: Isomorphic k => Isomorphism a b -> k b a from (Isomorphism a b) = isomorphic b a {-# INLINE from #-} {-# SPECIALIZE from :: Isomorphism a b -> b -> a #-} {-# SPECIALIZE from :: Isomorphism a b -> Isomorphism b a #-} --- |--- > via :: Isomorphism a b -> (a :~> b)+-- | Convert from an 'Isomorphism' back to any 'Isomorphic' value.+--+-- This is useful when you need to store an isomoprhism as a data type inside a container+-- and later reconstitute it as an overloaded function. via :: Isomorphic k => Isomorphism a b -> k a b via (Isomorphism a b) = isomorphic a b {-# INLINE via #-}@@ -105,23 +105,24 @@ -- Isomorphisms families as Lenses ----------------------------------------------------------------------------- --- | Isomorphim families can be composed with other lenses using either' (.)' and 'id'+-- | Isomorphim families can be composed with other lenses using either ('.') and 'id' -- from the Prelude or from Control.Category. However, if you compose them--- with each other using '(.)' from the Prelude, they will be dumbed down to a+-- with each other using ('.') from the Prelude, they will be dumbed down to a -- mere 'Lens'. -- -- > import Control.Category -- > import Prelude hiding ((.),id) ----- > type Iso a b c d = forall k f. (Isomorphic k, Functor f) => Overloaded k f a b c d+-- @type Iso a b c d = forall k f. ('Isomorphic' k, 'Functor' f) => 'Overloaded' k f a b c d@ type Iso a b c d = forall k f. (Isomorphic k, Functor f) => k (c -> f d) (a -> f b) --- | > type SimpleIso a b = Simple Iso a b+-- |+-- @type SimpleIso = 'Control.Lens.Type.Simple' 'Iso'@ type SimpleIso a b = Iso a a b b  -- | Build an isomorphism family from two pairs of inverse functions ----- > isos :: (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> Iso a b c d+-- @isos :: (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> 'Iso' a b c d@ isos :: (Isomorphic k, Functor f) => (a -> c) -> (c -> a) -> (b -> d) -> (d -> b) -> k (c -> f d) (a -> f b) isos ac ca bd db = isomorphic   (\cfd a -> db <$> cfd (ac a))@@ -132,7 +133,7 @@  -- | Build a simple isomorphism from a pair of inverse functions ----- > iso :: (a -> b) -> (b -> a) -> Simple Iso a b+-- @iso :: (a -> b) -> (b -> a) -> 'Control.Lens.Type.Simple' 'Iso' a b@ iso :: (Isomorphic k, Functor f) => (a -> b) -> (b -> a) -> k (b -> f b) (a -> f a) iso ab ba = isos ab ba ab ba {-# INLINE iso #-}@@ -145,16 +146,18 @@  -- | This isomorphism can be used to wrap or unwrap a value in 'Identity'. ----- > x^.identity = Identity x--- > Identity x^.from identity = x+-- @x^.identity = 'Identity' x@+--+-- @'Identity' x^.from identity = x@ identity :: Iso a b (Identity a) (Identity b) identity = isos Identity runIdentity Identity runIdentity {-# INLINE identity #-}  -- | This isomorphism can be used to wrap or unwrap a value in 'Const' ----- > x^._const = Const x--- > Const x^.from _const = x+-- @x^._const = 'Const' x@+--+-- @'Const' x^.from _const = x@ _const :: Iso a b (Const a c) (Const b d) _const = isos Const getConst Const getConst {-# INLINE _const #-}
src/Control/Lens/Setter.hs view
@@ -17,10 +17,10 @@ -- -- > type Setter a b c d = (c -> Identity d) -> a -> Identity b -----  Every 'Traversal' is a valid 'Setter', since 'Identity' is 'Applicative'.+--  Every 'Control.Lens.Traversal.Traversal' is a valid 'Setter', since 'Identity' is 'Applicative'. -- -- Everything you can do with a 'Functor', you can do with a 'Setter'. There--- are combinators that generalize 'fmap' and '(<$)'.+-- are combinators that generalize 'fmap' and ('<$'). ---------------------------------------------------------------------------- module Control.Lens.Setter   (@@ -39,10 +39,10 @@   , mapOf   , set   , (.~), (%~)-  , (+~), (-~), (*~), (//~), (^~), (^^~), (**~), (||~), (&&~), (<>~)+  , (+~), (-~), (*~), (//~), (^~), (^^~), (**~), (||~), (&&~), (<>~), (<.~)   -- * State Combinators   , (.=), (%=)-  , (+=), (-=), (*=), (//=), (^=), (^^=), (**=), (||=), (&&=), (<>=)+  , (+=), (-=), (*=), (//=), (^=), (^^=), (**=), (||=), (&&=), (<>=), (<.=)   , (<~)   -- * MonadWriter   , whisper@@ -59,9 +59,8 @@ import Data.Functor.Identity import Data.Monoid -infixr 4 .~, +~, *~, -~, //~, ^~, ^^~, **~, &&~, ||~, %~, <>~-infix  4 .=, +=, *=, -=, //=, ^=, ^^=, **=, &&=, ||=, %=, <>=-+infixr 4 .~, +~, *~, -~, //~, ^~, ^^~, **~, &&~, ||~, %~, <>~, <.~+infix  4 .=, +=, *=, -=, //=, ^=, ^^=, **=, &&=, ||=, %=, <>=, <.= infixr 2 <~  @@ -70,7 +69,7 @@ ------------------------------------------------------------------------------  -- |--- The only 'Lens'-like law that can apply to a 'Setter' @l@ is that+-- The only 'Control.Lens.Type.Lens'-like law that can apply to a 'Setter' @l@ is that -- -- > set l c (set l b a) = set l c a --@@ -86,7 +85,7 @@ -- > l pure = pure -- > l f . run . l g = l (f . run . g) ----- You can compose a 'Setter' with a 'Lens' or a 'Traversal' using @(.)@ from the Prelude+-- You can compose a 'Setter' with a 'Control.Lens.Type.Lens' or a 'Control.Lens.Traversal.Traversal' using @(.)@ from the Prelude -- and the result is always only a 'Setter' and nothing more. type Setter a b c d = forall f. Settable f => (c -> f d) -> a -> f b @@ -170,7 +169,7 @@ -- Using Setters ----------------------------------------------------------------------------- --- | Modify the target of a 'Lens' or all the targets of a 'Setter' or 'Traversal'+-- | Modify the target of a 'Control.Lens.Type.Lens' or all the targets of a 'Setter' or 'Control.Lens.Traversal.Traversal' -- with a function. -- -- > fmap        = adjust mapped@@ -187,7 +186,7 @@ adjust l f = runMutator . l (Mutator . f) {-# INLINE adjust #-} --- | Modify the target of a 'Lens' or all the targets of a 'Setter' or 'Traversal'+-- | Modify the target of a 'Control.Lens.Type.Lens' or all the targets of a 'Setter' or 'Control.Lens.Traversal.Traversal' -- with a function. This is an alias for adjust that is provided for consistency. -- -- > mapOf = adjust@@ -206,8 +205,8 @@ mapOf = adjust {-# INLINE mapOf #-} --- | Replace the target of a 'Lens' or all of the targets of a 'Setter'--- or 'Traversal' with a constant value.+-- | Replace the target of a 'Control.Lens.Type.Lens' or all of the targets of a 'Setter'+-- or 'Control.Lens.Traversal.Traversal' with a constant value. -- -- > (<$) = set mapped --@@ -219,8 +218,8 @@ set l d = runMutator . l (\_ -> Mutator d) {-# INLINE set #-} --- | Modifies the target of a 'Lens' or all of the targets of a 'Setter' or--- 'Traversal' with a user supplied function.+-- | Modifies the target of a 'Control.Lens.Type.Lens' or all of the targets of a 'Setter' or+-- 'Control.Lens.Traversal.Traversal' with a user supplied function. -- -- This is an infix version of 'adjust' --@@ -238,10 +237,10 @@ (%~) = adjust {-# INLINE (%~) #-} --- | Replace the target of a 'Lens' or all of the targets of a 'Setter'--- or 'Traversal' with a constant value.+-- | Replace the target of a 'Control.Lens.Type.Lens' or all of the targets of a 'Setter'+-- or 'Control.Lens.Traversal.Traversal' with a constant value. ----- This is an infix version of 'set', provided for consistency with '(.=)'+-- This is an infix version of 'set', provided for consistency with ('.=') -- -- -- > f <$ a = mapped .~ f $ a@@ -257,15 +256,24 @@ (.~) = set {-# INLINE (.~) #-} --- | Increment the target(s) of a numerically valued 'Lens', Setter' or 'Traversal'+-- | Set with pass-through --+-- This is mostly present for consistency, but may be useful for for chaining assignments+--+-- If you do not need a copy of the intermediate result, then using @l .~ d@ directly is a good idea.+(<.~) :: Setting a b c d -> d -> a -> (d, b)+l <.~ d = \a -> (d, l .~ d $ a)+{-# INLINE (<.~) #-}++-- | Increment the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal'+-- -- > ghci> _1 +~ 1 $ (1,2) -- > (2,2) (+~) :: Num c => Setting a b c c -> c -> a -> b l +~ n = adjust l (+ n) {-# INLINE (+~) #-} --- | Multiply the target(s) of a numerically valued 'Lens', 'Iso', 'Setter' or 'Traversal'+-- | Multiply the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' -- -- >>> _2 *~ 4 $ (1,2) -- (1,8)@@ -273,7 +281,7 @@ l *~ n = adjust l (* n) {-# INLINE (*~) #-} --- | Decrement the target(s) of a numerically valued 'Lens', 'Iso', 'Setter' or 'Traversal'+-- | Decrement the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' -- -- >>> _1 -~ 2 $ (1,2) -- (-1,2)@@ -281,11 +289,11 @@ l -~ n = adjust l (subtract n) {-# INLINE (-~) #-} --- | Divide the target(s) of a numerically valued 'Lens', 'Iso', 'Setter' or 'Traversal'+-- | Divide the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' (//~) :: Fractional c => Setting a b c c -> c -> a -> b l //~ n = adjust l (/ n) --- | Raise the target(s) of a numerically valued 'Lens', 'Setter' or 'Traversal' to a non-negative integral power+-- | Raise the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to a non-negative integral power -- -- >>> _2 ^~ 2 $ (1,3) -- (1,9)@@ -293,7 +301,7 @@ l ^~ n = adjust l (^ n) {-# INLINE (^~) #-} --- | Raise the target(s) of a fractionally valued 'Lens', 'Setter' or 'Traversal' to an integral power+-- | Raise the target(s) of a fractionally valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to an integral power -- -- >>> _2 ^^~ (-1) $ (1,2) -- (1,0.5)@@ -301,7 +309,7 @@ l ^^~ n = adjust l (^^ n) {-# INLINE (^^~) #-} --- | Raise the target(s) of a floating-point valued 'Lens', 'Setter' or 'Traversal' to an arbitrary power.+-- | Raise the target(s) of a floating-point valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to an arbitrary power. -- -- >>> _2 **~ pi $ (1,3) -- (1,31.54428070019754)@@ -309,12 +317,12 @@ l **~ n = adjust l (** n) {-# INLINE (**~) #-} --- | Logically '||' the target(s) of a 'Bool'-valued 'Lens' or 'Setter'+-- | Logically '||' the target(s) of a 'Bool'-valued 'Control.Lens.Type.Lens' or 'Setter' (||~):: Setting a b Bool Bool -> Bool -> a -> b l ||~ n = adjust l (|| n) {-# INLINE (||~) #-} --- | Logically '&&' the target(s) of a 'Bool'-valued 'Lens' or 'Setter'+-- | Logically '&&' the target(s) of a 'Bool'-valued 'Control.Lens.Type.Lens' or 'Setter' (&&~) :: Setting a b Bool Bool -> Bool -> a -> b l &&~ n = adjust l (&& n) {-# INLINE (&&~) #-}@@ -328,7 +336,7 @@ -- Using Setters with State ------------------------------------------------------------------------------ --- | Replace the target of a 'Lens' or all of the targets of a 'Setter' or 'Traversal' in our monadic+-- | Replace the target of a 'Control.Lens.Type.Lens' or all of the targets of a 'Setter' or 'Control.Lens.Traversal.Traversal' in our monadic -- state with a new value, irrespective of the old. -- -- > (.=) :: MonadState a m => Iso a a c d             -> d -> m ()@@ -341,7 +349,7 @@ l .= b = State.modify (l .~ b) {-# INLINE (.=) #-} --- | Map over the target of a 'Lens' or all of the targets of a 'Setter' or 'Traversal in our monadic state.+-- | Map over the target of a 'Control.Lens.Type.Lens' or all of the targets of a 'Setter' or 'Traversal in our monadic state. -- -- > (%=) :: MonadState a m => Iso a a c d             -> (c -> d) -> m () -- > (%=) :: MonadState a m => Lens a a c d            -> (c -> d) -> m ()@@ -351,7 +359,7 @@ l %= f = State.modify (l %~ f) {-# INLINE (%=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by adding a value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by adding a value -- -- Example: --@@ -362,52 +370,52 @@ l += b = State.modify (l +~ b) {-# INLINE (+=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by subtracting a value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by subtracting a value (-=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m () l -= b = State.modify (l -~ b) {-# INLINE (-=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by multiplying by value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by multiplying by value (*=) :: (MonadState a m, Num b) => SimpleSetting a b -> b -> m () l *= b = State.modify (l *~ b) {-# INLINE (*=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by dividing by a value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by dividing by a value (//=) ::  (MonadState a m, Fractional b) => SimpleSetting a b -> b -> m () l //= b = State.modify (l //~ b) {-# INLINE (//=) #-} --- | Raise the target(s) of a numerically valued 'Lens', 'Setter' or 'Traversal' to a non-negative integral power+-- | Raise the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to a non-negative integral power (^=) ::  (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m () l ^= c = State.modify (l ^~ c) {-# INLINE (^=) #-} --- | Raise the target(s) of a numerically valued 'Lens', 'Setter' or 'Traversal' to an integral power+-- | Raise the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to an integral power (^^=) ::  (MonadState a m, Fractional b, Integral c) => SimpleSetting a b -> c -> m () l ^^= c = State.modify (l ^^~ c) {-# INLINE (^^=) #-} --- | Raise the target(s) of a numerically valued 'Lens', 'Setter' or 'Traversal' to an arbitrary power+-- | Raise the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to an arbitrary power (**=) ::  (MonadState a m, Floating b) => SimpleSetting a b -> b -> m () l **= b = State.modify (l **~ b) {-# INLINE (**=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by taking their logical '&&' with a value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by taking their logical '&&' with a value (&&=):: MonadState a m => SimpleSetting a Bool -> Bool -> m () l &&= b = State.modify (l &&~ b) {-# INLINE (&&=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso, 'Setter' or 'Traversal' by taking their logical '||' with a value+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Iso, 'Setter' or 'Control.Lens.Traversal.Traversal' by taking their logical '||' with a value (||=) :: MonadState a m => SimpleSetting a Bool -> Bool -> m () l ||= b = State.modify (l ||~ b) {-# INLINE (||=) #-} --- | Modify the target(s) of a 'Simple' 'Lens', 'Iso', 'Setter' or 'Traversal' by 'mappend'ing a value.+-- | Modify the target(s) of a 'Simple' 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal' by 'mappend'ing a value. (<>=) :: (MonadState a m, Monoid b) => SimpleSetting a b -> b -> m () l <>= b = State.modify (l <>~ b) {-# INLINE (<>=) #-} --- | Run a monadic action, and set all of the targets of a 'Lens', 'Setter' or 'Traversal' to its result.+-- | Run a monadic action, and set all of the targets of a 'Control.Lens.Type.Lens', 'Setter' or 'Control.Lens.Traversal.Traversal' to its result. -- -- > (<~) :: MonadState a m => Iso a a c d       -> m d -> m () -- > (<~) :: MonadState a m => Lens a a c d      -> m d -> m ()@@ -429,6 +437,19 @@ (<~) :: MonadState a m => Setting a a c d -> m d -> m () l <~ md = md >>= (l .=) {-# INLINE (<~) #-}++-- | Set with pass-through+--+-- This is useful for chaining assignment+--+-- > do x <- _2 <.= (an expensive expression)+--+-- If you do not need a copy of the intermediate result, then using @l .= d@ will avoid unused binding warnings+(<.=) :: MonadState a m => Setting a a c d -> d -> m d+l <.= d = do+  l .= d+  return d+{-# INLINE (<.=) #-}  ------------------------------------------------------------------------------ -- MonadWriter
src/Control/Lens/Type.hs view
@@ -3,6 +3,11 @@ {-# LANGUAGE Rank2Types #-} {-# LANGUAGE LiberalTypeSynonyms #-} {-# LANGUAGE FlexibleContexts #-}++#ifndef MIN_VERSION_mtl+#define MIN_VERSION_mtl(x,y,z) 1+#endif+ ----------------------------------------------------------------------------- -- | -- Module      :  Control.Lens.Type@@ -60,6 +65,12 @@   , merged   , bothLenses +  -- * Setting Functionally with Passthrough+  , (<%~), (<+~), (<-~), (<*~), (<//~), (<^~), (<^^~), (<**~), (<||~), (<&&~), (<<>~)++  -- * Setting State with Passthrough+  , (<%=), (<+=), (<-=), (<*=), (<//=), (<^=), (<^^=), (<**=), (<||=), (<&&=), (<<>=)+   -- * Simplified and In-Progress   , LensLike   , Overloaded@@ -77,9 +88,12 @@ import Control.Monad.Trans.State.Strict as Strict import Control.Monad.Trans.Reader import Data.Functor.Identity+import Data.Monoid  infixr 4 %%~ infix  4 %%=+infixr 4 <+~, <*~, <-~, <//~, <^~, <^^~, <**~, <&&~, <||~, <%~, <<>~+infix  4 <+=, <*=, <-=, <//=, <^=, <^^=, <**=, <&&=, <||=, <%=, <<>=  -------------------------- -- Lenses@@ -346,3 +360,174 @@ -- | > type SimpleOverloaded k f a b = Simple (Overloaded k f) a b type SimpleOverloaded k f a b = Overloaded k f a a b b +-----------------------------------------------------------------------------+-- Setting and Remembering+-----------------------------------------------------------------------------++-- | Modify the target of a 'Lens' and return the result+--+-- When you do not need the result of the addition, ('+~') is more flexible.+(<%~) :: LensLike ((,)d) a b c d -> (c -> d) -> a -> (d, b)+l <%~ f = l $ \c -> let d = f c in (d, d)+{-# INLINE (<%~) #-}++-- | Increment the target of a numerically valued 'Lens' and return the result+--+-- When you do not need the result of the addition, ('+~') is more flexible.+(<+~) :: Num c => LensLike ((,)c) a b c c -> c -> a -> (c, b)+l <+~ c = l <%~ (+ c)+{-# INLINE (<+~) #-}++-- | Decrement the target of a numerically valued 'Lens' and return the result+--+-- When you do not need the result of the subtraction, ('-~') is more flexible.+(<-~) :: Num c => LensLike ((,)c) a b c c -> c -> a -> (c, b)+l <-~ c = l <%~ subtract c+{-# INLINE (<-~) #-}++-- | Decrement the target of a numerically valued 'Lens' and return the result+--+-- When you do not need the result of the subtraction, ('*~') is more flexible.+(<*~) :: Num c => LensLike ((,)c) a b c c -> c -> a -> (c, b)+l <*~ c = l <%~ (* c)+{-# INLINE (<*~) #-}++-- | Divide the target of a fractionally valued 'Lens' and return the result.+--+-- When you do not need the result of the division, ('//~') is more flexible.+(<//~) :: Fractional c => LensLike ((,)c) a b c c -> c -> a -> (c, b)+l <//~ c = l <%~ (/ c)+{-# INLINE (<//~) #-}++-- | Raise the target of a numerically valued 'Lens' to a non-negative 'Integral' power and return the result+--+-- When you do not need the result of the division, ('^~') is more flexible.+(<^~) :: (Num c, Integral d) => LensLike ((,)c) a b c c -> d -> a -> (c, b)+l <^~ d = l <%~ (^ d)+{-# INLINE (<^~) #-}++-- | Raise the target of a fractionally valued 'Lens' to an 'Integral' power and return the result+--+-- When you do not need the result of the division, ('^^~') is more flexible.+(<^^~) :: (Fractional c, Integral d) => LensLike ((,)c) a b c c -> d -> a -> (c, b)+l <^^~ d = l <%~ (^^ d)+{-# INLINE (<^^~) #-}++-- | Raise the target of a floating-point valued 'Lens' to an arbitrary power and return the result+--+-- When you do not need the result of the division, ('**~') is more flexible.+(<**~) :: Floating c => LensLike ((,)c) a b c c -> c -> a -> (c, b)+l <**~ c = l <%~ (** c)+{-# INLINE (<**~) #-}++-- | Logically '||' a Boolean valued 'Lens' and return the result+--+-- When you do not need the result of the operation, ('||~') is more flexible.+(<||~) :: LensLike ((,)Bool) a b Bool Bool -> Bool -> a -> (Bool, b)+l <||~ c = l <%~ (|| c)+{-# INLINE (<||~) #-}++-- | Logically '&&' a Boolean valued 'Lens' and return the result+--+-- When you do not need the result of the operation, ('&&~') is more flexible.+(<&&~) :: LensLike ((,)Bool) a b Bool Bool -> Bool -> a -> (Bool, b)+l <&&~ c = l <%~ (&& c)+{-# INLINE (<&&~) #-}++-- | 'mappend' a monoidal value onto the end of the target of a 'Lens' and return the result+--+-- When you do not need the result of the operation, ('<>~') is more flexible.+(<<>~) :: Monoid m => LensLike ((,)m) a b m m -> m -> a -> (m, b)+l <<>~ m = l <%~ (`mappend` m)+{-# INLINE (<<>~) #-}++-----------------------------------------------------------------------------+-- Setting and Remembering State+-----------------------------------------------------------------------------++-- | Modify the target of a ' into your monad's state by a user supplied function and return the result.+--+-- When you do not need the result of the operation, ('%=') is more flexible.+(<%=) :: MonadState a m => LensLike ((,)d) a a c d -> (c -> d) -> m d+l <%= f = l %%= (\c -> let d = f c in (d,d))+{-# INLINE (<%=) #-}++-- | Add to the target of a numerically valued 'Lens' into your monad's state and return the result.+--+-- When you do not need the result of the multiplication, ('+=') is more flexible.+(<+=) :: (MonadState a m, Num b) => SimpleLensLike ((,)b) a b -> b -> m b+l <+= b = l <%= (+ b)+{-# INLINE (<+=) #-}++-- | Subtract from the target of a numerically valued 'Lens' into your monad's state and return the result.+--+-- When you do not need the result of the multiplication, ('-=') is more flexible.+(<-=) :: (MonadState a m, Num b) => SimpleLensLike ((,)b) a b -> b -> m b+l <-= b = l <%= subtract b+{-# INLINE (<-=) #-}++-- | Multiply the target of a numerically valued 'Lens' into your monad's state and return the result.+--+-- When you do not need the result of the multiplication, ('*=') is more flexible.+(<*=) :: (MonadState a m, Num b) => SimpleLensLike ((,)b) a b -> b -> m b+l <*= b = l <%= (* b)+{-# INLINE (<*=) #-}++-- | Divide the target of a fractionally valued 'Lens' into your monad's state and return the result.+--+-- When you do not need the result of the division, ('//=') is more flexible.+(<//=) :: (MonadState a m, Fractional b) => SimpleLensLike ((,)b) a b -> b -> m b+l <//= b = l <%= (/ b)+{-# INLINE (<//=) #-}++-- | Raise the target of a numerically valued 'Lens' into your monad's state to a non-negative 'Integral' power and return the result+--+-- When you do not need the result of the operation, ('**=') is more flexible.+(<^=) :: (MonadState a m, Num b, Integral c) => SimpleLensLike ((,)b) a b -> c -> m b+l <^= c = l <%= (^ c)+{-# INLINE (<^=) #-}++-- | Raise the target of a fractionally valued 'Lens' into your monad's state to an 'Integral' power and return the result+--+-- When you do not need the result of the operation, ('^^=') is more flexible.+(<^^=) :: (MonadState a m, Fractional b, Integral c) => SimpleLensLike ((,)b) a b -> c -> m b+l <^^= c = l <%= (^^ c)+{-# INLINE (<^^=) #-}++-- | Raise the target of a floating-point valued 'Lens' into your monad's state to an arbitrary power and return the result+--+-- When you do not need the result of the operation, ('**=') is more flexible.+(<**=) :: (MonadState a m, Floating b) => SimpleLensLike ((,)b) a b -> b -> m b+l <**= b = l <%= (** b)+{-# INLINE (<**=) #-}++-- | Logically '||' a Boolean valued 'Lens' into your monad's state and return the result+--+-- When you do not need the result of the operation, ('||=') is more flexible.+(<||=) :: MonadState a m => SimpleLensLike ((,)Bool) a Bool -> Bool -> m Bool+l <||= b = l <%= (|| b)+{-# INLINE (<||=) #-}++-- | Logically '&&' a Boolean valued 'Lens' into your monad's state and return the result+--+-- When you do not need the result of the operation, ('&&=') is more flexible.+(<&&=) :: MonadState a m => SimpleLensLike ((,)Bool) a Bool -> Bool -> m Bool+l <&&= b = l <%= (&& b)+{-# INLINE (<&&=) #-}++-- | 'mappend' a monoidal value onto the end of the target of a 'Lens' into your monad's state and return the result+--+-- When you do not need the result of the operation, ('<>=') is more flexible.+(<<>=) :: (MonadState a m, Monoid r) => SimpleLensLike ((,)r) a r -> r -> m r+l <<>= r = l <%= (`mappend` r)+{-# INLINE (<<>=) #-}++-- These belong in Setter.+{-+(<.~) :: LensLike ((,)d) a b c d -> d -> a -> (d, b)+l <.~ d = l $ \_ -> (d,d)+{-# INLINE (<.~) #-}++(<.=) :: MonadState a m => LensLike ((,)d) a a c d -> d -> m d+(<.=) l = state (l.~)+-}
src/Data/List/Lens.hs view
@@ -117,7 +117,7 @@ traverseTail :: SimpleIndexedTraversal Int [a] a traverseTail = index $ \f aas -> case aas of   []     -> pure []-  (a:as) -> (a:) <$> traverseWithIndexOf traverseList (f . (+1)) as+  (a:as) -> (a:) <$> withIndex traverseList (f . (+1)) as {-# INLINE traverseTail #-}  -- | Traverse the last element in a list.@@ -147,5 +147,5 @@ traverseInit :: SimpleIndexedTraversal Int [a] a traverseInit = index $ \f aas -> case aas of   [] -> pure []-  as -> (++ [Prelude.last as]) <$> traverseWithIndexOf traverseList f (Prelude.init as)+  as -> (++ [Prelude.last as]) <$> withIndex traverseList f (Prelude.init as) {-# INLINE traverseInit #-}
src/Data/Tree/Lens.hs view
@@ -28,5 +28,5 @@ -- | A 'Traversal' of the direct descendants of the root of a 'Tree' -- indexed by its position in the list of children children :: SimpleIndexedTraversal Int (Tree a) (Tree a)-children = index $ \ f (Node a as) -> Node a <$> traverseWithIndexOf traverseList f as+children = index $ \ f (Node a as) -> Node a <$> withIndex traverseList f as {-# INLINE children #-}