microlens 0.4.0.1 → 0.4.1.0
raw patch · 4 files changed
+65/−8 lines, 4 files
Files
- CHANGELOG.md +8/−0
- microlens.cabal +1/−1
- src/Lens/Micro.hs +21/−5
- src/Lens/Micro/Internal.hs +35/−2
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.4.1.0++* Added `strict` and `lazy`.+ # 0.4.0.1 * Fixed a bug that wasn't letting the package compile with GHC 8.0 (see issue #63).@@ -7,6 +11,10 @@ * Added `folding`. * Renamed `Getter` and `Fold` to `SimpleGetter` and `SimpleFold` and put them into `Lens.Micro`. Genuine `Getter` and `Fold` are available in microlens-contra. * Replaced `Applicative (Const r)` constraints with `Monoid r` because it's the same thing but easier to understand.++# 0.3.5.1++* Backported the fix for the bug that wasn't letting the package compile with GHC 8.0 (see issue #63). # 0.3.5.0
microlens.cabal view
@@ -1,5 +1,5 @@ name: microlens-version: 0.4.0.1+version: 0.4.1.0 synopsis: A tiny part of the lens library with no dependencies description: This is an extract from <http://hackage.haskell.org/package/lens lens> (with no dependencies). It's not a toy lenses library, unsuitable for “real world”, but merely a small one. It is compatible with lens, and should have same performance. It also has better documentation.
src/Lens/Micro.hs view
@@ -45,9 +45,13 @@ Lens, Lens', lens, at,- non, _1, _2, _3, _4, _5, + -- * Iso: a lens that only changes the representation+ -- $isos-note+ strict, lazy,+ non,+ -- * Traversal: a lens iterating over several elements Traversal, Traversal', failing,@@ -567,6 +571,19 @@ lens sa sbt afb s = sbt s <$> afb (sa s) {-# INLINE lens #-} +-- Isos --------------------------------------------------------------------++{- $isos-note++Isos (or isomorphisms) are lenses that convert a value instead of targeting a part of it; in other words, inside of every list lives a reversed list, inside of every strict @Text@ lives a lazy @Text@, and inside of every @(a, b)@ lives a @(b, a)@. Since an isomorphism doesn't lose any information, it's possible to /reverse/ it and use it in the opposite direction by using @from@ from the lens library:++@+from :: Iso' s a -> Iso' a s+@++However, it's not possible for microlens to export isomorphisms, because their type depends on @<http://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html#t:Profunctor Profunctor>@, which resides in the <http://hackage.haskell.org/package/profunctors profunctors> library, which is a somewhat huge dependency. So, all isomorphisms included here are lenses instead (and thus you can't use them in the opposite direction).+-}+ {- | 'non' lets you “relabel” a 'Maybe' by equating 'Nothing' to an arbitrary value (which you can choose): @@ -634,7 +651,6 @@ * @at \"Soon\"@ sees @Nothing@ and deletes the corresponding value from the map * the resulting empty map is passed to @non Map.empty@, which sees that it's empty and thus produces @Nothing@ * @at \"Dez Mona\"@ sees @Nothing@ and removes the key from the map- -} non :: Eq a => a -> Lens' (Maybe a) a non x afb s = f <$> afb (fromMaybe x s)@@ -778,7 +794,7 @@ >>> [] ^? _head Nothing -This package only lets you use '_head' on lists, but you can use @Lens.Micro.GHC@ from <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> and get instances for @ByteString@ and @Seq@, or use @Lens.Micro.Platform@ from <http://hackage.haskell.org/package/microlens-platform microlens-platform> and additionally get instances for @Text@ and @Vector@.+This package only lets you use '_head' on lists, but if you use <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> you get instances for @ByteString@ and @Seq@, and if you use <http://hackage.haskell.org/package/microlens-platform microlens-platform> you additionally get instances for @Text@ and @Vector@. -} _head :: Cons s s a a => Traversal' s a _head = _Cons._1@@ -808,7 +824,7 @@ >>> "I HATE CAPS." & _tail.each %~ toLower "I hate caps." -This package only lets you use '_tail' on lists, but you can use @Lens.Micro.GHC@ from <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> and get instances for @ByteString@ and @Seq@, or use @Lens.Micro.Platform@ from <http://hackage.haskell.org/package/microlens-platform microlens-platform> and additionally get instances for @Text@ and @Vector@.+This package only lets you use '_tail' on lists, but if you use <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> you get instances for @ByteString@ and @Seq@, and if you use <http://hackage.haskell.org/package/microlens-platform microlens-platform> you additionally get instances for @Text@ and @Vector@. -} _tail :: Cons s s a a => Traversal' s s _tail = _Cons._2@@ -852,7 +868,7 @@ Left 5 @ -However, it's not possible for microlens to export prisms, because their type depends on @<http://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html#t:Choice Choice>@, which resides in the <http://hackage.haskell.org/package/profunctors profunctors> library, which is a somewhat huge dependency. So, all prisms included here are traversals instead.+However, it's not possible for microlens to export prisms, because their type depends on @<http://hackage.haskell.org/package/profunctors/docs/Data-Profunctor.html#t:Choice Choice>@ from <http://hackage.haskell.org/package/profunctors profunctors>. So, all prisms included here are traversals instead (and you can't reverse them). -} {- |
src/Lens/Micro/Internal.hs view
@@ -47,6 +47,7 @@ Field5(..), Cons(..), Snoc(..),+ Strict(..), ) where @@ -178,7 +179,7 @@ 'each' :: ('RealFloat' a, 'RealFloat' b) => 'Traversal' ('Complex' a) ('Complex' b) a b @ -Additionally, you can use 'each' with types from <http://hackage.haskell.org/package/array array>, <http://hackage.haskell.org/package/bytestring bytestring>, and <http://hackage.haskell.org/package/containers containers> by using @Lens.Micro.GHC@ from <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>, or with types from <http://hackage.haskell.org/package/vector vector>, <http://hackage.haskell.org/package/text text>, and <http://hackage.haskell.org/package/unordered-containers unordered-containers> by using @Lens.Micro.Platform@ from <http://hackage.haskell.org/package/microlens-platform microlens-platform>.+You can also use 'each' with types from <http://hackage.haskell.org/package/array array>, <http://hackage.haskell.org/package/bytestring bytestring>, and <http://hackage.haskell.org/package/containers containers> by using <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>, or additionally with types from <http://hackage.haskell.org/package/vector vector>, <http://hackage.haskell.org/package/text text>, and <http://hackage.haskell.org/package/unordered-containers unordered-containers> by using <http://hackage.haskell.org/package/microlens-platform microlens-platform>. -} each :: Traversal s t a b default each :: (Traversable g, s ~ g a, t ~ g b) => Traversal s t a b@@ -252,7 +253,7 @@ 'ix' :: ('Eq' e) => e -> 'Traversal'' (e -> a) a @ -Additionally, you can use 'ix' with types from <http://hackage.haskell.org/package/array array>, <http://hackage.haskell.org/package/bytestring bytestring>, and <http://hackage.haskell.org/package/containers containers> by using @Lens.Micro.GHC@ from <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>, or with types from <http://hackage.haskell.org/package/vector vector>, <http://hackage.haskell.org/package/text text>, and <http://hackage.haskell.org/package/unordered-containers unordered-containers> by using @Lens.Micro.Platform@ from <http://hackage.haskell.org/package/microlens-platform microlens-platform>.+You can also use 'ix' with types from <http://hackage.haskell.org/package/array array>, <http://hackage.haskell.org/package/bytestring bytestring>, and <http://hackage.haskell.org/package/containers containers> by using <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>, or additionally with types from <http://hackage.haskell.org/package/vector vector>, <http://hackage.haskell.org/package/text text>, and <http://hackage.haskell.org/package/unordered-containers unordered-containers> by using <http://hackage.haskell.org/package/microlens-platform microlens-platform>. -} ix :: Index m -> Traversal' m (IxValue m) default ix :: (At m) => Index m -> Traversal' m (IxValue m)@@ -534,3 +535,35 @@ _Snoc _ [] = pure [] _Snoc f xs = (\(as,a) -> as ++ [a]) <$> f (init xs, last xs) {-# INLINE _Snoc #-}++class Strict lazy strict | lazy -> strict, strict -> lazy where+ {- |+'strict' lets you convert between strict and lazy versions of a datatype:++>>> let someText = "hello" :: Lazy.Text+>>> someText ^. strict+"hello" :: Strict.Text++It can also be useful if you have a function that works on a strict type but your type is lazy:++@+stripDiacritics :: Strict.Text -> Strict.Text+stripDiacritics = ...+@++>>> let someText = "Paul Erdős" :: Lazy.Text+>>> someText & strict %~ stripDiacritics+"Paul Erdos" :: Lazy.Text++'strict' works on @ByteString@ and @StateT@\/@WriterT@\/@RWST@ if you use <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>, and additionally on @Text@ if you use <http://hackage.haskell.org/package/microlens-platform microlens-platform>.+ -}+ strict :: Lens' lazy strict++ {- |+'lazy' is like 'strict' but works in opposite direction:++>>> let someText = "hello" :: Strict.Text+>>> someText ^. lazy+"hello" :: Lazy.Text+ -}+ lazy :: Lens' strict lazy