lens 1.9 → 1.9.1
raw patch · 13 files changed
+200/−118 lines, 13 files
Files
- README.markdown +9/−4
- lens.cabal +1/−1
- src/Control/Lens/Fold.hs +2/−0
- src/Control/Lens/Getter.hs +18/−18
- src/Control/Lens/IndexedFold.hs +33/−0
- src/Control/Lens/IndexedLens.hs +25/−9
- src/Control/Lens/IndexedSetter.hs +17/−10
- src/Control/Lens/IndexedTraversal.hs +32/−18
- src/Control/Lens/Iso.hs +3/−3
- src/Control/Lens/Setter.hs +12/−15
- src/Control/Lens/Traversal.hs +14/−14
- src/Data/Sequence/Lens.hs +30/−24
- src/Data/Text/Lens.hs +4/−2
README.markdown view
@@ -1,13 +1,13 @@-lens-====+Lens: Lenses, Folds, and Traversals+================================== [](http://travis-ci.org/ekmett/lens) This package provides families of [lenses](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Type.hs), [isomorphisms](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Iso.hs), [folds](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Fold.hs), [traversals](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Traversal.hs), [getters](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Getter.hs) and [setters](https://github.com/ekmett/lens/blob/master/src/Control/Lens/Setter.hs). -An overview of the derivation of setters, folds, traversals, getters and lenses can be found on the lens wiki under [Tutorial](https://github.com/ekmett/lens/wiki/Tutorial).+An overview of the [derivation](https://github.com/ekmett/lens/wiki/Derivation) of these types can be found on the [Lens Wiki](https://github.com/ekmett/lens/wiki) along with a brief [Tutorial](https://github.com/ekmett/lens/wiki/Tutorial). -[](https://creately.com/diagram/h5nyo9ne1/LBbRz63yg4yQsTXGLtub1bQU4%3D)+Documentation is available through [github](https://ekmett.github.com/lens) or [hackage](http://hackage.haskell.org/package/lens). Examples --------@@ -136,6 +136,11 @@ ``` There is also a fully operational, but simple game of [Pong](https://github.com/ekmett/lens/blob/master/examples/Pong.hs) in the [examples/](https://github.com/ekmett/lens/blob/master/examples/) folder.++Field Guide+-----------++[](https://creately.com/diagram/h5nyo9ne1/LBbRz63yg4yQsTXGLtub1bQU4%3D) Contact Information -------------------
lens.cabal view
@@ -1,6 +1,6 @@ name: lens category: Data, Lenses-version: 1.9+version: 1.9.1 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE
src/Control/Lens/Fold.hs view
@@ -67,6 +67,8 @@ , foldrOf', foldlOf' , foldr1Of, foldl1Of , foldrMOf, foldlMOf+ -- * Implementation Details+ , noEffect ) where import Control.Applicative as Applicative
src/Control/Lens/Getter.hs view
@@ -106,9 +106,9 @@ -- A 'Gettable' 'Functor' ignores its argument, which it carries solely as a phantom -- type parameter. ----- To ensure this, it is required to satisfy:+-- To ensure this, an instance of 'Gettable' is required to satisfy: ----- > id = fmap f = coerce+-- @'id' = 'fmap' f = 'coerce'@ class Functor f => Gettable f where -- | Replace the phantom type argument. coerce :: f a -> f b@@ -163,8 +163,8 @@ -- "hello" -- -- @--- view :: 'Getter' a c -> a -> c--- view :: 'Monoid' m => 'Control.Lens.Fold.Fold' a m -> a -> m+-- view :: 'Getter' a c -> a -> c+-- view :: 'Monoid' m => 'Control.Lens.Fold.Fold' a m -> a -> m -- view :: 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> a -> c -- view :: 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> a -> c -- view :: 'Monoid' m => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a m -> a -> m@@ -183,8 +183,8 @@ -- 5 -- -- @--- views :: 'Getter' a c -> (c -> d) -> a -> d--- views :: 'Monoid' m => 'Control.Lens.Fold.Fold' a c -> (c -> m) -> a -> m+-- views :: 'Getter' a c -> (c -> d) -> a -> d+-- views :: 'Monoid' m => 'Control.Lens.Fold.Fold' a c -> (c -> m) -> a -> m -- views :: 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> (c -> d) -> a -> d -- views :: 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> (c -> d) -> a -> d -- views :: 'Monoid' m => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a c -> (c -> m) -> a -> m@@ -203,8 +203,8 @@ -- "hello" -- -- @--- (^$) :: 'Getter' a c -> a -> c--- (^$) :: 'Monoid' m => 'Control.Lens.Fold.Fold' a m -> a -> m+-- (^$) :: 'Getter' a c -> a -> c+-- (^$) :: 'Monoid' m => 'Control.Lens.Fold.Fold' a m -> a -> m -- (^$) :: 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> a -> c -- (^$) :: 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> a -> c -- (^$) :: 'Monoid' m => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a m -> a -> m@@ -226,8 +226,8 @@ -- 2.23606797749979 -- -- @--- (^.) :: a -> 'Getter' a c -> c--- (^.) :: 'Monoid' m => a -> 'Control.Lens.Fold.Fold' a m -> m+-- (^.) :: a -> 'Getter' a c -> c+-- (^.) :: 'Monoid' m => a -> 'Control.Lens.Fold.Fold' a m -> m -- (^.) :: a -> 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> c -- (^.) :: a -> 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> c -- (^.) :: 'Monoid' m => a -> 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a m -> m@@ -245,8 +245,8 @@ -- summary of a 'Control.Lens.Fold.Fold' or 'Control.Lens.Traversal.Traversal' that points to a monoidal value. -- -- @--- query :: 'MonadReader' a m => 'Getter' a c -> m c--- query :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Fold.Fold' a c -> m c+-- query :: 'MonadReader' a m => 'Getter' a c -> m c+-- query :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Fold.Fold' a c -> m c -- query :: 'MonadReader' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> m c -- query :: 'MonadReader' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> m c -- query :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a c -> m c@@ -260,8 +260,8 @@ -- summary of a 'Control.Lens.Fold.Fold' or 'Control.Lens.Traversal.Traversal' that points to a monoidal value. -- -- @--- queries :: 'MonadReader' a m => 'Getter' a c -> (c -> e) -> m e--- queries :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Fold.Fold' a c -> (c -> e) -> m e+-- queries :: 'MonadReader' a m => 'Getter' a c -> (c -> e) -> m e+-- queries :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Fold.Fold' a c -> (c -> e) -> m e -- queries :: 'MonadReader' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> (c -> e) -> m e -- queries :: 'MonadReader' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> (c -> e) -> m e -- queries :: ('MonadReader' a m, 'Monoid' c) => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a c -> (c -> e) -> m e@@ -279,8 +279,8 @@ -- summary of a 'Control.Lens.Fold.Fold' or 'Control.Lens.Traversal.Traversal' that points to a monoidal value. -- -- @--- use :: 'MonadState' a m => 'Getter' a c -> m c--- use :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Fold.Fold' a r -> m r+-- use :: 'MonadState' a m => 'Getter' a c -> m c+-- use :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Fold.Fold' a r -> m r -- use :: 'MonadState' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> m c -- use :: 'MonadState' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> m c -- use :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a r -> m r@@ -294,8 +294,8 @@ -- summary of a 'Control.Lens.Fold.Fold' or 'Control.Lens.Traversal.Traversal' that points to a monoidal value. -- -- @--- uses :: 'MonadState' a m => 'Getter' a c -> (c -> e) -> m e--- uses :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Fold.Fold' a c -> (c -> r) -> m r+-- uses :: 'MonadState' a m => 'Getter' a c -> (c -> e) -> m e+-- uses :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Fold.Fold' a c -> (c -> r) -> m r -- uses :: 'MonadState' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Type.Lens' a c -> (c -> e) -> m e -- uses :: 'MonadState' a m => 'Control.Lens.Type.Simple' 'Control.Lens.Iso.Iso' a c -> (c -> e) -> m e -- uses :: ('MonadState' a m, 'Monoid' r) => 'Control.Lens.Type.Simple' 'Control.Lens.Traversal.Traversal' a c -> (c -> r) -> m r
src/Control/Lens/IndexedFold.hs view
@@ -17,6 +17,8 @@ ( -- * Indexed Folds IndexedFold++ -- * Consuming Indexed Folds , ifoldMapOf , ifoldrOf , ifoldlOf@@ -33,6 +35,11 @@ , ifoldrMOf , ifoldlMOf , itoListOf++ -- * Building Indexed Folds+ , ifiltered+ , itakingWhile+ , idroppingWhile ) where import Control.Applicative@@ -341,3 +348,29 @@ itoListOf :: IndexedGetting i [(i,c)] a c -> a -> [(i,c)] itoListOf l = ifoldMapOf l (\i c -> [(i,c)]) {-# INLINE itoListOf #-}++noEffect :: (Applicative f, Gettable f) => f a+noEffect = coerce $ pure ()+{-# INLINE noEffect #-}++-- | Obtain an 'IndexedFold' by filtering a 'Control.Lens.IndexedLens.IndexedLens', 'IndexedGetter', 'IndexedFold' or 'Control.Lens.IndexedTraversal.IndexedTraversal'.+ifiltered :: (Gettable f, Applicative f, Indexed i k) => (i -> c -> Bool) -> Index i (c -> f c) (a -> f a) -> k (c -> f c) (a -> f a)+ifiltered p l = index $ \ f -> withIndex l $ \ i c -> if p i c then f i c else noEffect+{-# INLINE ifiltered #-}++-- | Obtain an 'IndexedFold' by taking elements from another 'IndexedFold', 'Control.Lens.IndexedLens.IndexedLens', 'IndexedGetter' or 'Control.Lens.IndexedTraversal.IndexedTraversal' while a predicate holds.+itakingWhile :: (Gettable f, Applicative f, Indexed i k)+ => (i -> c -> Bool)+ -> IndexedGetting i (Endo (f a)) a c+ -> k (c -> f c) (a -> f a)+itakingWhile p l = index $ \ f -> ifoldrOf l (\i a r -> if p i a then f i a *> r else noEffect) noEffect+{-# INLINE itakingWhile #-}+++-- | Obtain an 'IndexedFold' by dropping elements from another 'IndexedFold', 'Control.Lens.IndexedLens.IndexedLens', 'IndexedGetter' or 'Control.Lens.IndexedTraversal.IndexedTraversal' while a predicate holds.+idroppingWhile :: (Gettable f, Applicative f, Indexed i k)+ => (i -> c -> Bool)+ -> IndexedGetting i (Endo (f a)) a c+ -> k (c -> f c) (a -> f a)+idroppingWhile p l = index $ \f -> ifoldrOf l (\i a r -> if p i a then r else f i a *> r) noEffect+{-# INLINE idroppingWhile #-}
src/Control/Lens/IndexedLens.hs view
@@ -55,8 +55,10 @@ -- -- If you do not need the intermediate result, you can use ('Control.Lens.Type.%@~') or even ('Control.Lens.Type.%~'). ----- > (<%@~) :: IndexedLens i a b c d -> (i -> c -> d) -> a -> (d, b)--- > (<%@~) :: Monoid d => IndexedTraversal i a b c d -> (i -> c -> d) -> a -> (d, b)+-- @+-- (<%@~) :: 'IndexedLens' i a b c d -> (i -> c -> d) -> a -> (d, b)+-- (<%@~) :: 'Monoid' d => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a b c d -> (i -> c -> d) -> a -> (d, b)+-- @ (<%@~) :: Overloaded (Index i) ((,)d) a b c d -> (i -> c -> d) -> a -> (d, b) l <%@~ f = withIndex l $ \i c -> let d = f i c in (d, d) {-# INLINE (<%@~) #-}@@ -67,9 +69,19 @@ -- -- @('%%@~') = 'withIndex'@ ----- > (%%@~) :: IndexedLens i a b c d -> (i -> c -> (e, d)) -> a -> (e, b)--- > (%%@~) :: Monoid e => IndexedTraversal i a b c d -> (i -> c -> (e, d)) -> a -> (e, b)-(%%@~) :: Overloaded (Index i) ((,)e) a b c d -> (i -> c -> (e, d)) -> a -> (e, b)+-- @+-- (%%@~) :: 'Functor' f => 'IndexedLens' i a b c d -> (i -> c -> f d) -> a -> f b+-- (%%@~) :: 'Functor' f => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a b c d -> (i -> c -> f d) -> a -> f b+-- @+--+-- In particular, it is often useful to think of this function as having one of these even more +-- restrictive type signatures+--+-- @+-- (%%@~) :: 'IndexedLens' i a b c d -> (i -> c -> (e, d)) -> a -> (e, b)+-- (%%@~) :: 'Monoid' e => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a b c d -> (i -> c -> (e, d)) -> a -> (e, b)+-- @+(%%@~) :: Overloaded (Index i) f a b c d -> (i -> c -> f d) -> a -> f b (%%@~) = withIndex {-# INLINE (%%@~) #-} @@ -79,8 +91,10 @@ -- -- @l '%%@=' f = 'state' (l '%%@~' f)@ ----- > (%%@=) :: MonadState a m IndexedLens i a a c d -> (i -> c -> (e, d)) -> a -> m e--- > (%%@=) :: (MonadState a m, Monoid e) => IndexedTraversal i a a c d -> (i -> c -> (e, d)) -> a -> m e+-- @+-- (%%@=) :: 'MonadState' a m 'IndexedLens' i a a c d -> (i -> c -> (e, d)) -> a -> m e+-- (%%@=) :: ('MonadState' a m, 'Monoid' e) => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a a c d -> (i -> c -> (e, d)) -> a -> m e+-- @ (%%@=) :: MonadState a m => Overloaded (Index i) ((,)e) a a c d -> (i -> c -> (e, d)) -> m e #if MIN_VERSION_mtl(2,1,0) l %%@= f = State.state (l %%@~ f)@@ -96,8 +110,10 @@ -- adjust all of the targets of an 'Control.Lens.IndexedTraversal.IndexedTraversal' within the current state, and -- return a monoidal summary of the intermediate results. ----- > (<%@=) :: MonadState a m IndexedLens i a a c d -> (i -> c -> d) -> a -> m d--- > (<%@=) :: (MonadState a m, Monoid e) => IndexedTraversal i a a c d -> (i -> c -> d) -> a -> m d+-- @+-- (<%@=) :: 'MonadState' a m 'IndexedLens' i a a c d -> (i -> c -> d) -> a -> m d+-- (<%@=) :: ('MonadState' a m, 'Monoid' e) => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a a c d -> (i -> c -> d) -> a -> m d+-- @ (<%@=) :: MonadState a m => Overloaded (Index i) ((,)d) a a c d -> (i -> c -> d) -> m d l <%@= f = l %%@= \ i c -> let d = f i c in (d, d) {-# INLINE (<%@=) #-}
src/Control/Lens/IndexedSetter.hs view
@@ -37,7 +37,8 @@ -- 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 'SimpleIndexedSetter i = 'Simple' ('IndexedSetter' i)@+-- |+-- @type 'SimpleIndexedSetter' i = 'Simple' ('IndexedSetter' i)@ type SimpleIndexedSetter i a b = IndexedSetter i a a b b -- | Map with index.@@ -46,9 +47,11 @@ -- -- @'mapOf' l = 'imapOf' l . 'const'@ ----- > imapOf :: IndexedSetter i a b c d -> (i -> c -> d) -> a -> b--- > imapOf :: IndexedLens i a b c d -> (i -> c -> d) -> a -> b--- > 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 :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> c -> d) -> a -> b+-- imapOf :: 'Control.Lens.IndexedTraversal.IndexedTraversal' 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 #-}@@ -62,9 +65,11 @@ -- -- @l '%~' f = l '%@~' 'const' f@ ----- > (%@~) :: IndexedSetter i a b c d -> (i -> c -> d) -> a -> b--- > (%@~) :: IndexedLens i a b c d -> (i -> c -> d) -> a -> b--- > (%@~) :: IndexedTraversal i a b c d -> (i -> c -> d) -> a -> b+-- @+-- (%\@~) :: 'IndexedSetter' i a b c d -> (i -> c -> d) -> a -> b+-- (%\@~) :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> c -> d) -> a -> b+-- (%\@~) :: 'Control.Lens.IndexedTraversal.IndexedTraversal' i a b c d -> (i -> c -> d) -> a -> b+-- @ (%@~) :: Overloaded (Index i) Mutator a b c d -> (i -> c -> d) -> a -> b l %@~ f = runMutator . withIndex l (\i -> Mutator . f i) {-# INLINE (%@~) #-}@@ -76,9 +81,11 @@ -- -- @l '%=' f = l '%@=' 'const' f@ ----- > (%@=) :: MonadState a m => IndexedSetter i a a c d -> (i -> c -> d) -> m ()--- > (%@=) :: MonadState a m => IndexedLens i a a c d -> (i -> c -> d) -> m ()--- > (%@=) :: MonadState a m => IndexedTraversal i a b c d -> (i -> c -> d) -> m ()+-- @+-- (%\@=) :: 'MonadState' a m => 'IndexedSetter' i a a c d -> (i -> c -> d) -> m ()+-- (%\@=) :: 'MonadState' a m => 'Control.Lens.IndexedLens.IndexedLens' i a a c d -> (i -> c -> d) -> m ()+-- (%\@=) :: 'MonadState' a m => 'Control.Lens.IndexedTraversal.IndexedTraversal' i a b c d -> (i -> c -> d) -> m ()+-- @ (%@=) :: MonadState a m => Overloaded (Index i) Mutator a a c d -> (i -> c -> d) -> m () l %@= f = State.modify (l %@~ f) {-# INLINE (%@=) #-}
src/Control/Lens/IndexedTraversal.hs view
@@ -53,12 +53,15 @@ -- NB: When you don't need access to the index then you can just apply your 'IndexedTraversal' -- directly as a function! ----- @'itraverseOf' = 'withIndex'@------ @'Control.Lens.Traversal.traverseOf' = 'itraverseOf' . 'const' = 'id'@+-- @+-- 'itraverseOf' = 'withIndex'+-- 'Control.Lens.Traversal.traverseOf' = 'itraverseOf' . 'const' = 'id'+-- @ ----- > itraverseOf :: IndexedLens i a b c d -> (i -> c -> f d) -> a -> f b--- > itraverseOf :: IndexedTraversal i a b c d -> (i -> c -> f d) -> a -> f b+-- @+-- itraverseOf :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> c -> f d) -> a -> f b+-- 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 #-}@@ -70,8 +73,10 @@ -- -- @'iforOf' = 'flip' . 'itraverseOf'@ ----- > iforOf :: IndexedLens i a b c d -> a -> (i -> c -> f d) -> f b--- > iforOf :: IndexedTraversal i a b c d -> a -> (i -> c -> f d) -> f b+-- @+-- iforOf :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> a -> (i -> c -> f d) -> f b+-- iforOf :: 'IndexedTraversal' i a b c d -> a -> (i -> c -> f d) -> f b+-- @ iforOf :: Overloaded (Index i) f a b c d -> a -> (i -> c -> f d) -> f b iforOf = flip . withIndex {-# INLINE iforOf #-}@@ -84,8 +89,10 @@ -- -- @'Control.Lens.Traversal.mapMOf' = 'imapMOf' . 'const'@ ----- > imapMOf :: Monad m => IndexedLens i a b c d -> (i -> c -> m d) -> a -> m b--- > imapMOf :: Monad m => IndexedTraversal i a b c d -> (i -> c -> m d) -> a -> m b+-- @+-- imapMOf :: 'Monad' m => 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> c -> m d) -> a -> m b+-- imapMOf :: 'Monad' m => 'IndexedTraversal' i 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 #-}@@ -94,12 +101,15 @@ -- evaluate these actions from left to right, and collect the results, with access -- its position (and the arguments flipped). ----- @'Control.Lens.Traversal.forMOf' l a = 'iforMOf' l a . 'const'@------ @'iforMOf' = 'flip' . 'imapMOf'@+-- @+-- 'Control.Lens.Traversal.forMOf' l a = 'iforMOf' l a . 'const'+-- 'iforMOf' = 'flip' . 'imapMOf'+-- @ ----- > iforMOf :: Monad m => IndexedLens i a b c d -> a -> (i -> c -> m d) -> m b--- > iforMOf :: Monad m => IndexedTraversal i a b c d -> a -> (i -> c -> m d) -> m b+-- @+-- iforMOf :: 'Monad' m => 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> a -> (i -> c -> m d) -> m b+-- iforMOf :: 'Monad' m => 'IndexedTraversal' i a b c d -> a -> (i -> c -> m d) -> m b+-- @ iforMOf :: Overloaded (Index i) (WrappedMonad m) a b c d -> a -> (i -> c -> m d) -> m b iforMOf = flip . imapMOf {-# INLINE iforMOf #-}@@ -110,8 +120,10 @@ -- -- @'Control.Lens.Traversal.mapAccumROf' l = 'imapAccumROf' l . 'const'@ ----- > imapAccumROf :: IndexedLens i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)--- > imapAccumROf :: IndexedTraversal i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- @+-- imapAccumROf :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- imapAccumROf :: 'IndexedTraversal' i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- @ 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 -> Lazy.state (\s -> swap (f i s c))) a) s0) {-# INLINE imapAccumROf #-}@@ -122,8 +134,10 @@ -- -- @'Control.Lens.Traversal.mapAccumLOf' l = 'imapAccumLOf' l . 'const'@ ----- > imapAccumLOf :: IndexedLens i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)--- > imapAccumLOf :: IndexedTraversal i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- @+-- imapAccumLOf :: 'Control.Lens.IndexedLens.IndexedLens' i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- imapAccumLOf :: 'IndexedTraversal' i a b c d -> (i -> s -> c -> (s, d)) -> s -> a -> (s, b)+-- @ 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 (Lazy.state (\s -> swap (f i s c)))) a)) s0) {-# INLINE imapAccumLOf #-}
src/Control/Lens/Iso.hs view
@@ -163,7 +163,7 @@ -- -- @ -- x^.identity = 'Identity' x--- 'Identity' x^.from identity = x+-- 'Identity' x '^.' 'from' 'identity' = x -- @ identity :: Iso a b (Identity a) (Identity b) identity = isos Identity runIdentity Identity runIdentity@@ -172,8 +172,8 @@ -- | 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
src/Control/Lens/Setter.hs view
@@ -312,10 +312,10 @@ -- If you do not need a copy of the intermediate result, then using @l .~ d@ directly is a good idea. -- -- @--- (<.~) :: 'Setter' a b c d -> d -> a -> (d, b)--- (<.~) :: 'Control.Lens.Iso.Iso' a b c d -> d -> a -> (d, b)--- (<.~) :: 'Control.Lens.Type.Lens' a b c d -> d -> a -> (d, b)--- (<.~) :: 'Control.Lens.Traversal.Traversal' a b c d -> d -> a -> (d, b)+-- (\<.~) :: 'Setter' a b c d -> d -> a -> (d, b)+-- (\<.~) :: 'Control.Lens.Iso.Iso' a b c d -> d -> a -> (d, b)+-- (\<.~) :: 'Control.Lens.Type.Lens' a b c d -> d -> a -> (d, b)+-- (\<.~) :: 'Control.Lens.Traversal.Traversal' a b c d -> d -> a -> (d, b) -- @ (<.~) :: Setting a b c d -> d -> a -> (d, b) l <.~ d = \a -> (d, l .~ d $ a)@@ -370,9 +370,6 @@ {-# INLINE (-~) #-} -- | Divide the target(s) of a numerically valued 'Control.Lens.Type.Lens', 'Control.Lens.Iso.Iso', 'Setter' or 'Control.Lens.Traversal.Traversal'--- >>> import Control.Lens--- >>> id //~ 2 $ 1--- 0.5 -- -- @ -- (//~) :: 'Fractional' c => 'Setter' a b c c -> c -> a -> b@@ -646,10 +643,10 @@ -- | 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 => 'Control.Lens.Iso.Iso' a a c d -> m d -> m ()--- (<~) :: 'MonadState' a m => 'Control.Lens.Type.Lens' a a c d -> m d -> m ()--- (<~) :: 'MonadState' a m => 'Control.Lens.Traversal.Traversal' a a c d -> m d -> m ()--- (<~) :: 'MonadState' a m => 'Setter' a a c d -> m d -> m ()+-- (\<~) :: 'MonadState' a m => 'Control.Lens.Iso.Iso' a a c d -> m d -> m ()+-- (\<~) :: 'MonadState' a m => 'Control.Lens.Type.Lens' a a c d -> m d -> m ()+-- (\<~) :: 'MonadState' a m => 'Control.Lens.Traversal.Traversal' a a c d -> m d -> m ()+-- (\<~) :: 'MonadState' a m => 'Setter' a a c d -> m d -> m () -- @ -- -- As a reasonable mnemonic, this lets you store the result of a monadic action in a lens rather than@@ -677,10 +674,10 @@ -- If you do not need a copy of the intermediate result, then using @l .= d@ will avoid unused binding warnings -- -- @--- (<.=) :: 'MonadState' a m => 'Setter' a a c d -> d -> m d--- (<.=) :: 'MonadState' a m => 'Control.Lens.Iso.Iso' a a c d -> d -> m d--- (<.=) :: 'MonadState' a m => 'Control.Lens.Type.Lens' a a c d -> d -> m d--- (<.=) :: 'MonadState' a m => 'Control.Lens.Traversal.Traversal' a a c d -> d -> m d+-- (\<.=) :: 'MonadState' a m => 'Setter' a a c d -> d -> m d+-- (\<.=) :: 'MonadState' a m => 'Control.Lens.Iso.Iso' a a c d -> d -> m d+-- (\<.=) :: 'MonadState' a m => 'Control.Lens.Type.Lens' a a c d -> d -> m d+-- (\<.=) :: 'MonadState' a m => 'Control.Lens.Traversal.Traversal' a a c d -> d -> m d -- @ (<.=) :: MonadState a m => Setting a a c d -> d -> m d l <.= d = do
src/Control/Lens/Traversal.hs view
@@ -62,7 +62,7 @@ -- Traversals ------------------------------------------------------------------------------ --- | A 'Traversal' can be used directly as a 'Setter' or a 'Fold' (but not as a 'Lens') and provides+-- | A 'Traversal' can be used directly as a 'Control.Lens.Setter.Setter' or a 'Fold' (but not as a 'Lens') and provides -- the ability to both read and update multiple fields, subject to some relatively weak 'Traversal' laws. -- -- These have also been known as multilenses, but they have the signature and spirit of@@ -72,7 +72,7 @@ -- and the more evocative name suggests their application. -- -- Most of the time the 'Traversal' you will want to use is just 'traverse', but you can also pass any--- 'Lens' or 'Iso' as a Traversal, and composition of a 'Traversal' (or 'Lens' or 'Iso') with a 'Traversal' (or 'Lens' or 'Iso')+-- 'Lens' or 'Control.Lens.Iso.Iso' as a Traversal, and composition of a 'Traversal' (or 'Lens' or 'Control.Lens.Iso.Iso') with a 'Traversal' (or 'Lens' or 'Control.Lens.Iso.Iso') -- using (.) forms a valid 'Traversal'. -- -- The laws for a Traversal @t@ follow from the laws for Traversable as stated in \"The Essence of the Iterator Pattern\".@@ -83,9 +83,9 @@ -- -- 2) Sequential composition: ----- @'fmap' (t f) . t g = 'getCompose' . t ('Compose' . 'fmap' f . g)@+-- @'fmap' (t f) . t g = 'Data.Functor.Compose.getCompose' . t ('Data.Functor.Compose.Compose' . 'fmap' f . g)@ ----- One consequence of this requirement is that a traversal needs to leave the same number of elements as a candidate for +-- One consequence of this requirement is that a traversal needs to leave the same number of elements as a candidate for -- subsequent traversal as it started with. -- -- 3) No duplication of elements (as defined in \"The Essence of the Iterator Pattern\" section 5.5), which states@@ -108,7 +108,7 @@ -- @'traverse' = 'traverseOf' 'traverse'@ -- -- @--- 'traverseOf' :: 'Iso' a b c d -> (c -> f d) -> a -> f b+-- 'traverseOf' :: 'Control.Lens.Iso.Iso' a b c d -> (c -> f d) -> a -> f b -- 'traverseOf' :: 'Lens' a b c d -> (c -> f d) -> a -> f b -- 'traverseOf' :: 'Traversal' a b c d -> (c -> f d) -> a -> f b -- @@@ -126,7 +126,7 @@ -- @ -- -- @--- forOf :: 'Iso' a b c d -> a -> (c -> f d) -> f b+-- forOf :: 'Control.Lens.Iso.Iso' a b c d -> a -> (c -> f d) -> f b -- forOf :: 'Lens' a b c d -> a -> (c -> f d) -> f b -- forOf :: 'Traversal' a b c d -> a -> (c -> f d) -> f b -- @@@ -145,7 +145,7 @@ -- @ -- -- @--- 'sequenceAOf' :: 'Iso' a b (f c) c -> a -> f b+-- 'sequenceAOf' :: 'Control.Lens.Iso.Iso' a b (f c) c -> a -> f b -- 'sequenceAOf' :: 'Lens' a b (f c) c -> a -> f b -- 'sequenceAOf' :: 'Applicative' f => 'Traversal' a b (f c) c -> a -> f b -- @@@ -159,7 +159,7 @@ -- @'mapM' = 'mapMOf' 'traverse'@ -- -- @--- 'mapMOf :: 'Iso' a b c d -> (c -> m d) -> a -> m b+-- 'mapMOf :: 'Control.Lens.Iso.Iso' a b c d -> (c -> m d) -> a -> m b -- 'mapMOf :: 'Lens' a b c d -> (c -> m d) -> a -> m b -- 'mapMOf :: 'Monad' m => 'Traversal' a b c d -> (c -> m d) -> a -> m b -- @@@ -174,7 +174,7 @@ -- @ -- -- @--- forMOf :: 'Iso' a b c d -> a -> (c -> m d) -> m b+-- forMOf :: 'Control.Lens.Iso.Iso' a b c d -> a -> (c -> m d) -> m b -- forMOf :: 'Lens' a b c d -> a -> (c -> m d) -> m b -- forMOf :: 'Monad' m => 'Traversal' a b c d -> a -> (c -> m d) -> m b -- @@@ -190,7 +190,7 @@ -- @ -- -- @--- sequenceOf :: 'Iso' a b (m c) c -> a -> m b+-- sequenceOf :: 'Control.Lens.Iso.Iso' a b (m c) c -> a -> m b -- sequenceOf :: 'Lens' a b (m c) c -> a -> m b -- sequenceOf :: 'Monad' m => 'Traversal' a b (m c) c -> a -> m b -- @@@ -222,7 +222,7 @@ -- 'mapAccumROf' accumulates state from right to left. -- -- @--- mapAccumROf :: 'Iso' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)+-- mapAccumROf :: 'Control.Lens.Iso.Iso' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- mapAccumROf :: 'Lens' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- mapAccumROf :: 'Traversal' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- @@@ -237,7 +237,7 @@ -- 'mapAccumLOf' accumulates state from left to right. -- -- @--- mapAccumLOf :: 'Iso' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)+-- mapAccumLOf :: 'Control.Lens.Iso.Iso' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- mapAccumLOf :: 'Lens' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- mapAccumLOf :: 'Traversal' a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b) -- @@@ -254,7 +254,7 @@ -- @'scanr1' = 'scanr1Of' 'traverse'@ -- -- @--- scanr1Of :: 'Iso' a b c c -> (c -> c -> c) -> a -> b+-- scanr1Of :: 'Control.Lens.Iso.Iso' a b c c -> (c -> c -> c) -> a -> b -- scanr1Of :: 'Lens' a b c c -> (c -> c -> c) -> a -> b -- scanr1Of :: 'Traversal' a b c c -> (c -> c -> c) -> a -> b -- @@@ -283,7 +283,7 @@ -- Common Lenses ------------------------------------------------------------------------------ --- | A 'Lens' to view/edit the nth element 'elementOf' a 'Traversal', 'Lens' or 'Iso'.+-- | A 'Lens' to view/edit the nth element 'elementOf' a 'Traversal', 'Lens' or 'Control.Lens.Iso.Iso'. -- -- Attempts to access beyond the range of the 'Traversal' will cause an error. --
src/Data/Sequence/Lens.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleContexts #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Sequence.Lens@@ -17,16 +18,17 @@ ) where import Control.Applicative-import Control.Lens+import Control.Lens as Lens import Data.Monoid import Data.Sequence as Seq+import Data.Traversable -- | A 'Lens' that can access the @n@th element of a 'Seq'. -- -- Note: This is only a legal lens if there is such an element! ---at :: Int -> Simple Lens (Seq a) a-at i f m = (\a -> update i a m) <$> f (Seq.index m i)+at :: Int -> SimpleIndexedLens Int (Seq a) a+at i = Lens.index $ \ f m -> (\a -> update i a m) <$> f i (Seq.index m i) -- * Sequence isomorphisms @@ -52,51 +54,55 @@ unviewr EmptyR = mempty unviewr (as :> a) = as |> a +traverseSeq :: IndexedTraversal Int (Seq a) (Seq b) a b+traverseSeq = Lens.index $ \ f -> sequenceA . Seq.mapWithIndex f+{-# INLINE traverseSeq #-}+ -- * Traversals -- | Traverse the head of a 'Seq'-traverseHead :: Simple Traversal (Seq a) a-traverseHead f m = case viewl m of- a :< as -> (<| as) <$> f a+traverseHead :: SimpleIndexedTraversal Int (Seq a) a+traverseHead = Lens.index $ \f m -> case viewl m of+ a :< as -> (<| as) <$> f (0::Int) a EmptyL -> pure m {-# INLINE traverseHead #-} -- | Traverse the tail of a 'Seq'-traverseTail :: Simple Traversal (Seq a) a-traverseTail f m = case viewl m of- a :< as -> (a <|) <$> traverse f as+traverseTail :: SimpleIndexedTraversal Int (Seq a) a+traverseTail = Lens.index $ \f m -> case viewl m of+ a :< as -> (a <|) <$> withIndex traverseSeq (f . (+1)) as EmptyL -> pure m {-# INLINE traverseTail #-} -- | Traverse the last element of a 'Seq'-traverseLast :: Simple Traversal (Seq a) a-traverseLast f m = case viewr m of- as :> a -> (as |>) <$> f a+traverseLast :: SimpleIndexedTraversal Int (Seq a) a+traverseLast = Lens.index $ \f m -> case viewr m of+ as :> a -> (as |>) <$> f (Seq.length as) a EmptyR -> pure m {-# INLINE traverseLast #-} -- | Traverse all but the last element of a 'Seq'-traverseInit :: Simple Traversal (Seq a) a-traverseInit f m = case viewr m of- as :> a -> (|> a) <$> traverse f as+traverseInit :: SimpleIndexedTraversal Int (Seq a) a+traverseInit = Lens.index $ \ f m -> case viewr m of+ as :> a -> (|> a) <$> withIndex traverseSeq f as EmptyR -> pure m {-# INLINE traverseInit #-} -- | Traverse the first @n@ elements of a 'Seq'-traverseTo :: Int -> Simple Traversal (Seq a) a-traverseTo n f m = case Seq.splitAt n m of- (l,r) -> (>< r) <$> traverse f l+traverseTo :: Int -> SimpleIndexedTraversal Int (Seq a) a+traverseTo n = Lens.index $ \f m -> case Seq.splitAt n m of+ (l,r) -> (>< r) <$> withIndex traverseSeq f l {-# INLINE traverseTo #-} -- | Traverse all but the first @n@ elements of a 'Seq'-traverseFrom :: Int -> Simple Traversal (Seq a) a-traverseFrom n f m = case Seq.splitAt n m of- (l,r) -> (l ><) <$> traverse f r+traverseFrom :: Int -> SimpleIndexedTraversal Int (Seq a) a+traverseFrom n = Lens.index $ \ f m -> case Seq.splitAt n m of+ (l,r) -> (l ><) <$> withIndex traverseSeq (f . (+n)) r {-# INLINE traverseFrom #-} -- | Travere all the elements numbered from @i@ to @j@ of a 'Seq'-traverseSlice :: Int -> Int -> Simple Traversal (Seq a) a-traverseSlice i j f s = case Seq.splitAt i s of+traverseSlice :: Int -> Int -> SimpleIndexedTraversal Int (Seq a) a+traverseSlice i j = Lens.index $ \ f s -> case Seq.splitAt i s of (l,mr) -> case Seq.splitAt (j-i) mr of- (m, r) -> (\n -> l >< n >< r) <$> traverse f m+ (m, r) -> (\n -> l >< n >< r) <$> withIndex traverseSeq (f . (+i)) m {-# INLINE traverseSlice #-}
src/Data/Text/Lens.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE FlexibleContexts #-} ----------------------------------------------------------------------------- -- | -- Module : Data.Text.Lens@@ -15,6 +16,7 @@ import Control.Lens import Data.Text+import Data.List.Lens -- | Pack (or unpack) 'Text'. --@@ -28,6 +30,6 @@ -- | Traverse the individual characters in a either strict or lazy 'Text'. -- -- > anyOf text (=='c') :: Text -> Bool-text :: Simple Traversal Text Char-text = from packed . traverse+text :: SimpleIndexedTraversal Int Text Char+text = from packed .> traverseList {-# INLINE text #-}