lens 1.1 → 1.1.1
raw patch · 5 files changed
+56/−8 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Control.Lens: folds :: ((c -> m) -> a -> m) -> Getting m a b c d
+ Data.IntSet.Lens: members :: Setter IntSet IntSet Int Int
+ Data.Set.Lens: members :: (Ord i, Ord j) => Setter (Set i) (Set j) i j
- Control.Lens: (%%~) :: LensLike ((,) e) a b c d -> (c -> (e, d)) -> a -> (e, b)
+ Control.Lens: (%%~) :: LensLike f a b c d -> (c -> f d) -> a -> f b
Files
- lens.cabal +1/−1
- src/Control/Lens.hs +21/−7
- src/Data/IntMap/Lens.hs +9/−0
- src/Data/IntSet/Lens.hs +13/−0
- src/Data/Set/Lens.hs +12/−0
lens.cabal view
@@ -1,6 +1,6 @@ name: lens category: Data, Lenses-version: 1.1+version: 1.1.1 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE
src/Control/Lens.hs view
@@ -90,6 +90,7 @@ , Fold , Getting , to+ , folds , folding , folded , filtered@@ -260,20 +261,26 @@ -- | ('%%~') can be used in one of two scenarios: -- -- When applied to a 'Lens', it can edit the target of the 'Lens' in a structure, extracting a--- supplemental result, and the new structure.+-- functorial result. ----- When applied to a 'Traversal', it can edit the targets of the 'Traversals', extracting a--- supplemental monoidal summary of its actions.+-- When applied to a 'Traversal', it can edit the targets of the 'Traversals', extracting an+-- applicative summary of its actions. -- -- For all that the definition of this combinator is just: -- -- > (%%~) = id ----- It may be beneficial to think about it as if it had these more restrictive types, however:+-- > (%%~) :: Functor f => Lens a b c d -> (c -> f d) -> a -> f b+-- > (%%~) :: Applicative f => Traversal a b c d -> (c -> f d) -> a -> f b --+-- It may be beneficial to think about it as if it had these even more restrictive types, however:+--+-- When applied to a 'Traversal', it can edit the targets of the 'Traversals', extracting a+-- supplemental monoidal summary of its actions, by choosing f = ((,) m)+-- -- > (%%~) :: Lens a b c d -> (c -> (e, d)) -> a -> (e, b) -- > (%%~) :: Monoid m => Traversal a b c d -> (c -> (m, d)) -> a -> (m, b)-(%%~) :: LensLike ((,) e) a b c d -> (c -> (e, d)) -> a -> (e, b)+(%%~) :: LensLike f a b c d -> (c -> f d) -> a -> f b (%%~) = id {-# INLINE (%%~) #-} @@ -635,6 +642,7 @@ to f g a = Const (getConst (g (f a))) {-# INLINE to #-} + -- | -- Most 'Getter' combinators are able to be used with both a 'Getter' or a 'Fold' in -- limited situations, to do so, they need to be monomorphic in what we are going to@@ -917,6 +925,12 @@ -- > 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 +-- | Build a 'Getter' or 'Fold' from a 'foldMap'-like function.+--+-- > folds :: ((c -> m) -> a -> m) -> (c -> Const m d) -> a -> Const m b+folds :: ((c -> m) -> a -> m) -> Getting m a b c d+folds l f = Const . l (getConst . f)+ -- | 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'.@@ -926,9 +940,9 @@ -- | Obtain a 'Fold' from any 'Foldable' ----- > folded = folding id+-- > folded = folds foldMap folded :: Foldable f => Fold (f c) b c d-folded g = Const . foldMap (getConst . g)+folded = folds foldMap {-# INLINE folded #-} -- | Obtain a 'Fold' by filtering a 'Lens', 'Getter, 'Fold' or 'Traversal'.
src/Data/IntMap/Lens.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} {-# LANGUAGE Rank2Types #-} {-# LANGUAGE LiberalTypeSynonyms #-} -----------------------------------------------------------------------------@@ -47,13 +48,21 @@ -- | Traverse the value at the minimum key in a Map traverseAtMin :: Simple Traversal (IntMap v) v traverseAtMin f m = case IntMap.minView m of+#if MIN_VERSION_containers(0,5,0)+ Just (a, _) -> (\v -> IntMap.updateMin (const (Just v)) m) <$> f a+#else Just (a, _) -> (\v -> IntMap.updateMin (const v) m) <$> f a+#endif Nothing -> pure m {-# INLINE traverseAtMin #-} -- | Traverse the value at the maximum key in a Map traverseAtMax :: Simple Traversal (IntMap v) v traverseAtMax f m = case IntMap.maxView m of+#if MIN_VERSION_containers(0,5,0)+ Just (a, _) -> (\v -> IntMap.updateMax (const (Just v)) m) <$> f a+#else Just (a, _) -> (\v -> IntMap.updateMax (const v) m) <$> f a+#endif Nothing -> pure m {-# INLINE traverseAtMax #-}
src/Data/IntSet/Lens.hs view
@@ -10,6 +10,7 @@ ---------------------------------------------------------------------------- module Data.IntSet.Lens ( contains+ , members ) where import Control.Lens@@ -27,3 +28,15 @@ go False = IntSet.delete k s go True = IntSet.insert k s {-# INLINE contains #-}++-- | This 'Setter' can be used to change the contents of an 'IntSet' by mapping+-- the elements to new values.+--+-- Sadly, you can't create a valid 'Traversal' for a 'Set', because the number of+-- elements might change but you can manipulate it by reading using 'folded' and+-- reindexing it via 'setmap'.+--+-- > ghci> adjust members (+1) (fromList [1,2,3,4]+-- > fromList [2,3,4,5]+members :: Setter IntSet IntSet Int Int+members = sets IntSet.map
src/Data/Set/Lens.hs view
@@ -10,6 +10,7 @@ ---------------------------------------------------------------------------- module Data.Set.Lens ( contains+ , members ) where import Control.Lens@@ -27,3 +28,14 @@ go False = Set.delete k s go True = Set.insert k s {-# INLINE contains #-}++-- | This 'Setter' can be used to change the type of a 'Set' by mapping+-- the elements to new values.+--+-- Sadly, you can't create a valid 'Traversal' for a 'Set', but you can+-- manipulate it by reading using 'folded' and reindexing it via 'setmap'.+--+-- > ghci> adjust members (+1) (fromList [1,2,3,4]+-- > fromList [2,3,4,5]+members :: (Ord i, Ord j) => Setter (Set i) (Set j) i j+members = sets Set.map