microlens 0.4.10 → 0.4.11
raw patch · 5 files changed
+131/−42 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Lens.Micro: rewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b
+ Lens.Micro: transformOf :: ASetter a b a b -> (b -> b) -> a -> b
+ Lens.Micro.Internal: instance (a Data.Type.Equality.~ a', b Data.Type.Equality.~ b') => Lens.Micro.Internal.Each (Data.Either.Either a a') (Data.Either.Either b b') a b
Files
- CHANGELOG.md +8/−1
- microlens.cabal +11/−3
- src/Lens/Micro.hs +85/−37
- src/Lens/Micro/Extras.hs +7/−0
- src/Lens/Micro/Internal.hs +20/−1
CHANGELOG.md view
@@ -1,3 +1,10 @@+# 0.4.11++* Added fixity declarations for `+~` and `-~` (thanks to Francesco Ariis).+* Added `rewriteOf` and `transformOf` (thanks to @quasicomputational).+* Added an instance `Each (Either a a) (Either b b) a b`, following `lens`'s suit.+* Marked `Lens.Micro.Internal` as `Trustworthy` starting from GHC 7.8.+ # 0.4.10 * Added `+~` and `-~`.@@ -95,7 +102,7 @@ # 0.3.4.1 -* Changed the description of the package from “A tiny part of the lens library which you can depend upon” to “A tiny part of the lens library with no dependencies” because the previous one was ambiguous (I admit I kinda liked that ambiguity, tho).+* Changed the description of the package from “A tiny part of the lens library which you can depend upon” to “A tiny part of the lens library with no dependencies” because the previous one was ambiguous (I admit I kinda liked that ambiguity, though). # 0.3.4.0
microlens.cabal view
@@ -1,6 +1,6 @@ name: microlens-version: 0.4.10-synopsis: A tiny lens library with no dependencies. If you're writing an app, you probably want microlens-platform, not this.+version: 0.4.11+synopsis: A tiny lens library with no dependencies description: NOTE: If you're writing an app, you probably want <http://hackage.haskell.org/package/microlens-platform microlens-platform> – it has the most features. <http://hackage.haskell.org/package/microlens microlens> is intended more for library writers who want a tiny lens library (after all, lenses are pretty useful for everything, not just for updating records!). .@@ -18,7 +18,7 @@ . However, don't use this library if: .- * You need @Iso@s, @Prism@s, indexed traversals, or actually anything else which isn't defined here (tho some indexed functions are available elsewhere – containers and vector provide them for their types, and <http://hackage.haskell.org/package/ilist ilist> provides indexed functions for lists).+ * You need @Iso@s, @Prism@s, indexed traversals, or actually anything else which isn't defined here (though some indexed functions are available elsewhere – containers and vector provide them for their types, and <http://hackage.haskell.org/package/ilist ilist> provides indexed functions for lists). . * You want a library with a clean, understandable implementation (in which case you're looking for <http://hackage.haskell.org/package/lens-simple lens-simple>). .@@ -41,6 +41,14 @@ extra-source-files: CHANGELOG.md cabal-version: >=1.10+tested-with: GHC==7.4.2+ GHC==7.6.3+ GHC==7.8.4+ GHC==7.10.3+ GHC==8.0.2+ GHC==8.2.2+ GHC==8.4.4+ GHC==8.6.4 source-repository head type: git
src/Lens/Micro.hs view
@@ -44,6 +44,8 @@ (?~), (<%~), (<<%~), (<<.~), mapped,+ rewriteOf,+ transformOf, -- * Getter: extracts a value from a structure -- $getters-note@@ -112,7 +114,12 @@ import Data.Maybe import Data.Tuple import qualified Data.Foldable as F++#if __GLASGOW_HASKELL__ >= 708+import Data.Coerce+#else import Unsafe.Coerce+#endif #if MIN_VERSION_base(4,8,0) import Data.Function ((&))@@ -304,57 +311,69 @@ {-# INLINE over #-} --- | Increment the target(s) of a numerically valued 'Lens' or 'Traversal'.------ >>> (a,b) & _1 +~ c--- (a + c,b)------ >>> (a,b) & both +~ c--- (a + c,b + c)------ >>> (1,2) & _2 +~ 1--- (1,3)------ >>> [(a,b),(c,d)] & traverse.both +~ e--- [(a + e,b + e),(c + e,d + e)]------ @--- ('+~') :: 'Num' a => 'Lens'' s a -> a -> s -> s--- ('+~') :: 'Num' a => 'Traversal'' s a -> a -> s -> s--- @+{- |+Increment the target(s) of a numerically valued 'Lens' or 'Traversal'.++>>> (a,b) & _1 +~ c+(a + c,b)++>>> (a,b) & both +~ c+(a + c,b + c)++>>> (1,2) & _2 +~ 1+(1,3)++>>> [(a,b),(c,d)] & traverse.both +~ e+[(a + e,b + e),(c + e,d + e)]++@+('+~') :: 'Num' a => 'Lens'' s a -> a -> s -> s+('+~') :: 'Num' a => 'Traversal'' s a -> a -> s -> s+@++@since 0.4.10+-} (+~) :: Num a => ASetter s t a a -> a -> s -> t l +~ n = over l (+ n) {-# INLINE (+~) #-} --- | Decrement the target(s) of a numerically valued 'Lens', or 'Traversal'.------ >>> (a,b) & _1 -~ c--- (a - c,b)------ >>> (a,b) & both -~ c--- (a - c,b - c)------ >>> _1 -~ 2 $ (1,2)--- (-1,2)------ >>> mapped.mapped -~ 1 $ [[4,5],[6,7]]--- [[3,4],[5,6]]------ @--- ('-~') :: 'Num' a => 'Lens'' s a -> a -> s -> s--- ('-~') :: 'Num' a => 'Traversal'' s a -> a -> s -> s--- @+infixr 4 +~++{- |+Decrement the target(s) of a numerically valued 'Lens', or 'Traversal'.++>>> (a,b) & _1 -~ c+(a - c,b)++>>> (a,b) & both -~ c+(a - c,b - c)++>>> _1 -~ 2 $ (1,2)+(-1,2)++>>> mapped.mapped -~ 1 $ [[4,5],[6,7]]+[[3,4],[5,6]]++@+('-~') :: 'Num' a => 'Lens'' s a -> a -> s -> s+('-~') :: 'Num' a => 'Traversal'' s a -> a -> s -> s+@++@since 0.4.10+-} (-~) :: Num a => ASetter s t a a -> a -> s -> t l -~ n = over l (subtract n) {-# INLINE (-~) #-} -+infixr 4 -~ {- | ('<>~') appends a value monoidally to the target. >>> ("hello", "goodbye") & both <>~ " world!" ("hello world!", "goodbye world!")++@since 0.4.9 -} (<>~) :: (Monoid a) => ASetter s t a a -> a -> s -> t (<>~) l a = over l (`mappend` a)@@ -498,6 +517,30 @@ infixr 4 <<.~ +{- |+→ See <https://github.com/monadfix/microlens/pull/119#issuecomment-496004851 an example> on GitHub.++Rewrite by applying a rule everywhere you can. Ensures that the rule cannot be applied anywhere in the result.++Usually 'transformOf' is more appropriate, but 'rewriteOf' can give better compositionality. Given two single transformations @f@ and @g@, you can construct @\\a -> f a '<|>' g a@ which performs both rewrites until a fixed point.++@since 0.4.11+-}+rewriteOf :: ASetter a b a b -> (b -> Maybe a) -> a -> b+rewriteOf l f = go where+ go = transformOf l (\x -> maybe x go (f x))+{-# INLINE rewriteOf #-}++{- |+Transform every element by recursively applying a given 'ASetter' in a bottom-up manner.++@since 0.4.11+-}+transformOf :: ASetter a b a b -> (b -> b) -> a -> b+transformOf l f = go where+ go = f . over l go+{-# INLINE transformOf #-}+ -- Getting ----------------------------------------------------------------- {- $getters-note@@ -1015,8 +1058,13 @@ where Bazaar b = l sell s sell w = Bazaar ($ w)+#if __GLASGOW_HASKELL__ >= 708+ ins f = (coerce :: [Identity a] -> [a])+ (getConst (f (\ra -> Const [Identity ra])))+#else ins f = (unsafeCoerce :: [Identity a] -> [a]) (getConst (f (\ra -> Const [Identity ra])))+#endif unsafeOuts f = evalState (f (\_ -> state (unconsWithDefault fakeVal))) where fakeVal = error "unsafeOuts: not enough elements were supplied" unconsWithDefault d [] = (d,[])
src/Lens/Micro/Extras.hs view
@@ -1,4 +1,11 @@+{-# LANGUAGE CPP #-}++-- Depends on Lens.Micro.Internal marked as 'Trustworthy' or 'Unsafe'+#if __GLASGOW_HASKELL__ >= 708+{-# LANGUAGE Safe #-}+#else {-# LANGUAGE Trustworthy #-}+#endif {- |
src/Lens/Micro/Internal.hs view
@@ -10,8 +10,12 @@ {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE ConstraintKinds #-} {-# LANGUAGE ImplicitParams #-}-{-# LANGUAGE Unsafe #-} +#if __GLASGOW_HASKELL__ >= 708+{-# LANGUAGE Trustworthy #-}+#else+{-# LANGUAGE Unsafe #-}+#endif {- | Module : Lens.Micro.Internal@@ -156,6 +160,10 @@ -- Data.Profunctor.Unsafe ------------------------------------------------------------------------------ +-- Note: 'lens' defines a type-restricted version of (#.) to work around a+-- bug, but our version is restricted enough that we don't need it. See+-- <https://github.com/ekmett/lens/commit/cde2fc39c0dba413d1a6f814b47bd14431a5e339>+ #if __GLASGOW_HASKELL__ >= 708 ( #. ) :: Coercible c b => (b -> c) -> (a -> b) -> (a -> c) ( #. ) _ = coerce (\x -> x :: b) :: forall a b. Coercible b a => a -> b@@ -198,6 +206,7 @@ 'each' :: 'Traversal' [a] [b] a b 'each' :: 'Traversal' ('Maybe' a) ('Maybe' b) a b+'each' :: 'Traversal' ('Either' a a) ('Either' b b) a b -- since 0.4.11 'each' :: 'Traversal' (a,a) (b,b) a b 'each' :: 'Traversal' (a,a,a) (b,b,b) a b@@ -239,11 +248,21 @@ each = traverse {-# INLINE each #-} +{- |+@since 0.4.11+-}+instance (a~a', b~b') => Each (Either a a') (Either b b') a b where+ each f (Left a) = Left <$> f a+ each f (Right a ) = Right <$> f a+ {-# INLINE each #-}+ #if __GLASGOW_HASKELL__ >= 800 instance Each (NonEmpty a) (NonEmpty b) a b where each = traversed {-# INLINE each #-} #endif++-- NOTE: when adding new instances of 'Each', update the docs for 'each'. type family Index (s :: *) :: *