lens 1.0.1 → 1.0.2
raw patch · 3 files changed
+167/−14 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Control.Lens: dropping :: Applicative f => Int -> LensLike (AppliedState f) a b c c -> LensLike f a b c c
+ Control.Lens: droppingWhile :: Monoid m => (c -> Bool) -> Getting (Endo m) a b c d -> Getting m a b c d
+ Control.Lens: folding :: Foldable f => (a -> f c) -> Fold a b c d
+ Control.Lens: headOf :: Getting (First c) a b c d -> a -> Maybe c
+ Control.Lens: lastOf :: Getting (Last c) a b c d -> a -> Maybe c
+ Control.Lens: mapAccumLOf :: LensLike (Backwards (State s)) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
+ Control.Lens: mapAccumROf :: LensLike (State s) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)
+ Control.Lens: scanl1Of :: LensLike (Backwards (State (Maybe c))) a b c c -> (c -> c -> c) -> a -> b
+ Control.Lens: scanr1Of :: LensLike (State (Maybe c)) a b c c -> (c -> c -> c) -> a -> b
+ Control.Lens: taking :: Applicative f => Int -> LensLike (AppliedState f) a b c c -> LensLike f a b c c
+ Control.Lens: takingWhile :: Monoid m => (c -> Bool) -> Getting (Endo m) a b c d -> Getting m a b c d
Files
- lens.cabal +1/−1
- src/Control/Lens.hs +165/−13
- src/Control/Lens/Internal.hs +1/−0
lens.cabal view
@@ -1,6 +1,6 @@ name: lens category: Data, Lenses-version: 1.0.1+version: 1.0.2 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE
src/Control/Lens.hs view
@@ -65,6 +65,8 @@ , traverseOf, forOf, sequenceAOf , mapMOf, forMOf, sequenceOf , transposeOf+ , mapAccumLOf, mapAccumROf+ , scanr1Of, scanl1Of -- ** Common Lenses , valueAt, valueAtInt@@ -90,22 +92,26 @@ , (^=), (+=), (-=), (*=), (//=), (||=), (&&=), (|=), (&=), (%=) -- * Getters and Folds+ , Getter+ , Fold+ , Getting - -- ** Getters- , Getter, to+ , to - -- ** Folds- , Fold+ , folding , folded , filtered , reversed+ , takingWhile+ , droppingWhile - -- ** Getting and Folding- , Getting , view, views , (^.), (^$)+ , use, uses++ -- ** Getting and Folding , foldMapOf, foldOf- , foldrOf, foldlOf+ , foldrOf, foldlOf , toListOf , anyOf, allOf , andOf, orOf@@ -117,14 +123,13 @@ , elemOf, notElemOf , lengthOf , nullOf+ , headOf, lastOf , maximumOf, minimumOf , maximumByOf, minimumByOf , findOf- , foldrOf', foldlOf'- , foldr1Of, foldl1Of- , foldrMOf, foldlMOf- -- ** Getting and Folding State- , use, uses+ , foldrOf', foldlOf'+ , foldr1Of, foldl1Of+ , foldrMOf, foldlMOf -- * Common Traversals , traverseNothing@@ -145,6 +150,8 @@ , elementOf , elementsOf , backwards+ , taking+ , dropping -- * Cloning Lenses , clone@@ -189,10 +196,18 @@ -- | A 'Lens' is actually a lens family as described in <http://comonad.com/reader/2012/mirrored-lenses/>. ----- With great power comes great responsibility and a 'Lens' is subject to the lens laws:+-- With great power comes great responsibility and a 'Lens' is subject to the three common sense lens laws: --+-- 1) You get back what you put in:+-- -- > view l (set l b a) = b+--+-- 2) Putting back what you got doesn't change anything:+-- -- > set l (view l a) a = a+--+-- 3) Setting twice is the same as setting once:+-- -- > set l c (set l b a) = set l c a -- -- These laws are strong enough that the 4 type parameters of a 'Lens' cannot vary fully independently. For more on@@ -448,7 +463,60 @@ -- > transposeOf _2 :: (b, [a]) -> [(b, a)] transposeOf :: LensLike ZipList a b [c] c -> a -> [b] transposeOf l = getZipList . l ZipList+{-# INLINE transposeOf #-} +-- | Generalizes 'Data.Traversable.mapAccumR' to an arbitrary 'Traversal'.+--+-- > mapAccumR = mapAccumROf traverse+--+-- 'mapAccumROf' accumulates state from right to left.+--+-- > 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)+mapAccumROf :: LensLike (Lazy.State s) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)+mapAccumROf l f s0 a = swap (Lazy.runState (l (\c -> state (\s -> swap (f s c))) a) s0)+{-# INLINE mapAccumROf #-}++-- | Generalized 'Data.Traversable.mapAccumL' to an arbitrary 'Traversal'.+--+-- > mapAccumL = mapAccumLOf traverse+--+-- 'mapAccumLOf' accumulates state from left to right.+--+-- > 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)+mapAccumLOf :: LensLike (Backwards (Lazy.State s)) a b c d -> (s -> c -> (s, d)) -> s -> a -> (s, b)+mapAccumLOf l = mapAccumROf (backwards l)+{-# INLINE mapAccumLOf #-}++swap :: (a,b) -> (b,a)+swap (a,b) = (b,a)+{-# INLINE swap #-}++-- | Permit the use of 'scanr1' over an arbitrary 'Traversal' or 'Lens'.+--+-- > scanr1 = scanr1Of traverse+--+-- > scanr1Of :: Lens a b c c -> (c -> c -> c) -> a -> b+-- > scanr1Of :: Traversal a b c c -> (c -> c -> c) -> a -> b+scanr1Of :: LensLike (Lazy.State (Maybe c)) a b c c -> (c -> c -> c) -> a -> b+scanr1Of l f = snd . mapAccumROf l step Nothing where+ step Nothing c = (Just c, c)+ step (Just s) c = (Just r, r) where r = f c s+{-# INLINE scanr1Of #-}++-- | Permit the use of 'scanl1' over an arbitrary 'Traversal' or 'Lens'.+--+-- > scanl1 = scanl1Of traverse+--+-- > scanr1Of :: Lens a b c c -> (c -> c -> c) -> a -> b+-- > scanr1Of :: Traversal a b c c -> (c -> c -> c) -> a -> b+scanl1Of :: LensLike (Backwards (Lazy.State (Maybe c))) a b c c -> (c -> c -> c) -> a -> b+scanl1Of l f = snd . mapAccumLOf l step Nothing where+ step Nothing c = (Just c, c)+ step (Just s) c = (Just r, r) where r = f s c+{-# INLINE scanl1Of #-}+ ------------------------------------------------------------------------------ -- Setters ------------------------------------------------------------------------------@@ -923,7 +991,16 @@ -- > type Fold a b c d = forall m. Monoid m => Getting m a b c d type Fold a b c d = forall m. Monoid m => (c -> Const m d) -> a -> Const m b +-- | Obtain a 'Fold' by lifting an operation that returns a foldable result.+--+-- This can be useful to lift operations from @Data.List@ and elsewhere into a 'Fold'.+folding :: Foldable f => (a -> f c) -> Fold a b c d+folding f g = Const . foldMap (getConst . g) . f+{-# INLINE folding #-}+ -- | Obtain a 'Fold' from any 'Foldable'+--+-- > folded = folding id folded :: Foldable f => Fold (f c) b c d folded g = Const . foldMap (getConst . g) {-# INLINE folded #-}@@ -940,6 +1017,39 @@ reversed l f = Const . getDual . getConst . l (Const . Dual . getConst . f) {-# INLINE reversed #-} +--taking :: Int -> Getting (Taking m) a b c d -> Getting m a b c d+--dropping :: Int -> Getting (Dropping m) a b c d -> Getting m a b c d++-- | Obtain a 'Fold' by taking elements from another 'Fold', 'Lens', 'Getter' or 'Traversal' while a predicate holds.+--+-- > takeWhile p = toListOf (takingWhile p folded)+--+-- > ghci> toList (takingWhile (<=3) folded) [1..]+-- > [1,2,3]+takingWhile :: Monoid m => (c -> Bool) -> Getting (Endo m) a b c d -> Getting m a b c d+takingWhile p l f = Const . foldrOf l (\a r -> if p a then getConst (f a) `mappend` r else mempty) mempty+{-# INLINE takingWhile #-}++{-+taking :: Monoid m => Int -> Getting [c] a b c d -> Getting m a b c d+taking n l f = foldMap (getConst . f) . take n . toListOf l+{-# INLINE taking #-}++dropping :: Monoid m => Int -> Getting [c] a b c d -> Getting m a b c d+dropping n l f = foldMap (getConst . f) . drop n . toListOf l+{-# INLINE dropping #-}+-}++-- | Obtain a 'Fold' by dropping elements from another 'Fold', 'Lens', 'Getter' or 'Traversal' while a predicate holds.+--+-- > dropWhile p = toListOf (droppingWhile p folded)+--+-- > ghci> toList (dropWhile (<=3) folded) [1..6]+-- > [4,5,6]+droppingWhile :: Monoid m => (c -> Bool) -> Getting (Endo m) a b c d -> Getting m a b c d+droppingWhile p l f = Const . foldrOf l (\a r -> if p a then mempty else mappend r (getConst (f a))) mempty+{-# INLINE droppingWhile #-}+ -------------------------- -- Fold/Getter combinators --------------------------@@ -1237,6 +1347,30 @@ lengthOf l = getSum . foldMapOf l (\_ -> Sum 1) {-# INLINE lengthOf #-} +-- | Perform a safe 'head' of a 'Fold' or 'Traversal' or retrieve 'Just' the result +-- from a 'Getter' or 'Lens'.+--+-- > listToMaybe . toList = headOf folded+--+-- > headOf :: Getter a b c d -> a -> Maybe c+-- > headOf :: Lens a b c d -> a -> Maybe c+-- > headOf :: Fold a b c d -> a -> Maybe c+-- > headOf :: Traversal a b c d -> a -> Maybe c+headOf :: Getting (First c) a b c d -> a -> Maybe c+headOf l = getFirst . foldMapOf l (First . Just)+{-# INLINE headOf #-}++-- | Perform a safe 'last' of a 'Fold' or 'Traversal' or retrieve 'Just' the result+-- from a 'Getter' or 'Lens'.+--+-- > lastOf :: Getter a b c d -> a -> Maybe c+-- > lastOf :: Lens a b c d -> a -> Maybe c+-- > lastOf :: Fold a b c d -> a -> Maybe c+-- > lastOf :: Traversal a b c d -> a -> Maybe c+lastOf :: Getting (Last c) a b c d -> a -> Maybe c+lastOf l = getLast . foldMapOf l (Last . Just)+{-# INLINE lastOf #-}+ -- | -- Returns 'True' if this 'Fold' or 'Traversal' has no targets in the given container. --@@ -1686,3 +1820,21 @@ backwards :: LensLike (Backwards f) a b c d -> LensLike f a b c d backwards l f = getBackwards . l (Backwards . f) {-# INLINE backwards #-}++-- | Build a 'Traversal' that traverses the first @n@ elements of another 'Traversal'.+--+-- > take n = toListOf (taking n traverse)+--+-- To 'take' from something that is merely a 'Fold', compose with @'folding' ('take' n)@ instead.+taking :: Applicative f => Int -> LensLike (AppliedState f) a b c c -> LensLike f a b c c+taking n l = elementsOf l (<n)+{-# INLINE taking #-}++-- | Build a 'Traversal' that skips over the first @n@ elements of another 'Traversal', returning the rest.+--+-- > drop n = toListOf (dropping n traverse)+--+-- To 'drop' from something that is merely a 'Fold', compose with @'folding' ('drop' n)@ instead.+dropping :: Applicative f => Int -> LensLike (AppliedState f) a b c c -> LensLike f a b c c+dropping n l = elementsOf l (>=n)+{-# INLINE dropping #-}
src/Control/Lens/Internal.hs view
@@ -113,6 +113,7 @@ getMax NoMax = Nothing getMax (Max a) = Just a +-- | Run an Applicative backwards newtype Backwards f a = Backwards { getBackwards :: f a } instance Functor f => Functor (Backwards f) where