packages feed

microlens 0.1.3.0 → 0.1.5.0

raw patch · 5 files changed

+245/−61 lines, 5 filesdep ~basePVP: major bump suggested

API removals or changes: PVP suggests a major version bump

Dependency ranges changed: base

API changes (from Hackage documentation)

- Lens.Micro: instance Applicative (Const r) => Monoid (Folding (Const r) a)
+ Lens.Micro: at :: At m => Index m -> Lens' m (Maybe (IxValue m))
+ Lens.Micro: ix :: Ixed m => Index m -> Traversal' m (IxValue m)
+ Lens.Micro: traversed :: Traversable f => Traversal (f a) (f b) a b
+ Lens.Micro.Classes: at :: At m => Index m -> Lens' m (Maybe (IxValue m))
+ Lens.Micro.Classes: class Ixed m => At m
+ Lens.Micro.Classes: class Ixed m where ix = ixAt
+ Lens.Micro.Classes: instance Eq e => Ixed (e -> a)
+ Lens.Micro.Classes: instance Ixed [a]
+ Lens.Micro.Classes: ix :: Ixed m => Index m -> Traversal' m (IxValue m)
+ Lens.Micro.Internal: (#.) :: Coercible c b => (b -> c) -> (a -> b) -> (a -> c)
+ Lens.Micro.Internal: (.#) :: Coercible b a => (b -> c) -> (a -> b) -> (a -> c)
+ Lens.Micro.Internal: foldMapOf :: Getting r s a -> (a -> r) -> s -> r
+ Lens.Micro.Internal: folded :: (Foldable f, Applicative (Const r)) => Getting r (f a) a
+ Lens.Micro.Internal: foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r
+ Lens.Micro.Internal: foldring :: (Applicative (Const r)) => ((a -> Const r a -> Const r a) -> Const r a -> s -> Const r a) -> (a -> Const r b) -> s -> Const r t
+ Lens.Micro.Internal: sets :: ((a -> b) -> s -> t) -> ASetter s t a b
+ Lens.Micro.Internal: traversed :: Traversable f => Traversal (f a) (f b) a b

Files

CHANGELOG.md view
@@ -1,3 +1,10 @@+# 0.1.5.0++* Added `ix` and `at`.+* Added `traversed`.+* Moved some things into `Lens.Micro.Internal`.+* Bumped base version.+ # 0.1.3.0  * Moved some things into `Lens.Micro.Type` and `Lens.Micro.Classes`.
microlens.cabal view
@@ -1,35 +1,24 @@ name:                microlens-version:             0.1.3.0+version:             0.1.5.0 synopsis:            A tiny part of the lens library which you can depend upon 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.+  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.   .   Use this library:   .-    * if you want to define lenses or traversals in your own library, but-      don't want to depend on lens+    * if you want to define lenses or traversals in your own library, but don't want to depend on lens   .-    * if you are new to lenses and want a small library to play with,-      but don't want to use something “inferior” or have to relearn things-      when/if you switch to lens+    * if you are new to lenses and want a small library to play with, but don't want to use something “inferior” or have to relearn things when/if you switch to lens   .   Don't use this library:   .-    * if you need @Iso@s, @Prism@s, indexed traversals, @at@, @ix@,-      or actually anything else which isn't defined here+    * if you need @Iso@s, @Prism@s, indexed traversals, or actually anything else which isn't defined here   .-    * if you think lenses compose “in the wrong order” (in which case-      you're looking for -      <http://hackage.haskell.org/package/fclabels fclabels>)+    * if you think lenses compose “in the wrong order” (in which case you're looking for <http://hackage.haskell.org/package/fclabels fclabels>)   .-  Note that microlens has /no/ dependencies starting from GHC 7.10 -  (base-4.8). Prior to that, it has to depend on transformers-0.2 or above.+  Note that microlens has /no/ dependencies starting from GHC 7.10 (base-4.8). Prior to that, it has to depend on transformers-0.2 or above.   .-  Also note that it's not done yet and there's a lot of things missing-  and I'm still writing a tutorial for it.+  Also note that it's not done yet and there's a lot of things missing. license:             BSD3 license-file:        LICENSE author:              Artyom@@ -51,6 +40,7 @@   exposed-modules:     Lens.Micro                        Lens.Micro.Extras                        Lens.Micro.Classes+                       Lens.Micro.Internal                        Lens.Micro.Type   -- other-modules:          -- other-extensions:    @@ -60,7 +50,7 @@   if impl(ghc>=7.9)     build-depends:     base >=4.8 && <5   if !impl(ghc>=7.9)-    build-depends:     base >=4.4 && <5+    build-depends:     base >=4.5 && <5                      , transformers >=0.2    ghc-options:
src/Lens/Micro.hs view
@@ -35,12 +35,15 @@   -- * Lenses (setters and getters at once)   Lens, Lens',   lens,+  at,   _1, _2, _3, _4, _5,    -- * Traversals (lenses iterating over several elements)   Traversal, Traversal',   both,+  traversed,   each,+  ix,    -- * Prisms (traversals iterating over at most 1 element)   -- $prisms-note@@ -52,15 +55,12 @@  import Lens.Micro.Classes import Lens.Micro.Type+import Lens.Micro.Internal  import Control.Applicative import Data.Functor.Identity import Data.Monoid -#if __GLASGOW_HASKELL__ < 710-import Data.Foldable-#endif- #if __GLASGOW_HASKELL__ >= 710 import Data.Function ((&)) #endif@@ -109,13 +109,6 @@ -- Setting -----------------------------------------------------------------  {- |-'sets' creates an 'ASetter' from an ordinary function. (The only thing it does is wrapping and unwrapping 'Identity'.)--}-sets :: ((a -> b) -> s -> t) -> ASetter s t a b-sets f g = Identity . f (runIdentity . g)-{-# INLINE sets #-}--{- | ('%~') applies a function to the target; an alternative explanation is that it is an inverse of 'sets', which turns a setter into an ordinary function. @'mapped' '%~' 'reverse'@ is the same thing as @'fmap' 'reverse'@.  See 'over' if you want a non-operator synonym.@@ -203,7 +196,7 @@ {-# INLINE set #-}  {- |-'mapped' is a setter for everything contained in a functor. You can use it to map over lists, @Maybe@, or even @IO@ (which is something you can't do with 'traverse' or 'each').+'mapped' is a setter for everything contained in a functor. You can use it to map over lists, @Maybe@, or even @IO@ (which is something you can't do with 'traversed' or 'each').  Here 'mapped' is used to turn a value to all non-'Nothing' values in a list: @@ -256,15 +249,6 @@  -- Folds ------------------------------------------------------------------- --- | A 'Monoid' for a 'Contravariant' 'Applicative'.-newtype Folding f a = Folding { getFolding :: f a }--instance (Applicative (Const r)) => Monoid (Folding (Const r) a) where-  mempty = Folding (Const . getConst $ pure ())-  {-# INLINE mempty #-}-  Folding fr `mappend` Folding fs = Folding (fr *> fs)-  {-# INLINE mappend #-}- {- | @s ^.. t@ returns the list of all values that @t@ gets from @s@. @@ -324,22 +308,6 @@ {-# INLINE (^?!) #-}  infixl 8 ^?!--foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r-foldrOf l f z = flip appEndo z . foldMapOf l (Endo . f)-{-# INLINE foldrOf #-}--foldMapOf :: Getting r s a -> (a -> r) -> s -> r-foldMapOf l f = getConst . l (Const . f)-{-# INLINE foldMapOf #-}--{- |-'folded' is a fold for anything 'Foldable'. In a way, it's an opposite of-'mapped' – the most powerful getter, but can't be used as a setter.--}-folded :: (Foldable f, Applicative (Const r)) => Getting r (f a) a-folded f = Const . getConst . getFolding . foldMap (Folding . f)-{-# INLINE folded #-}  {- | 'has' checks whether a getter (any getter, including lenses, traversals, and folds) returns at least 1 value.
src/Lens/Micro/Classes.hs view
@@ -2,7 +2,9 @@ CPP, MultiParamTypeClasses, FunctionalDependencies,-GADTs,+RankNTypes,+TypeFamilies,+KindSignatures, FlexibleInstances, UndecidableInstances, DefaultSignatures@@ -12,6 +14,10 @@ module Lens.Micro.Classes (   Each(..),+  Index,+  IxValue,+  Ixed(..),+  At(..),   Field1(..),   Field2(..),   Field3(..),@@ -22,6 +28,7 @@   import Lens.Micro.Type+import Lens.Micro.Internal  #if __GLASGOW_HASKELL__ < 710 import Data.Traversable@@ -33,7 +40,7 @@  class Each s t a b | s -> a, t -> b, s b -> t, t a -> s where   {- |-'each' tries to be a universal 'Traversal' – it behaves like 'traverse' in most situations, but also adds support for e.g. tuples with same-typed values:+'each' tries to be a universal 'Traversal' – it behaves like 'traversed' in most situations, but also adds support for e.g. tuples with same-typed values:  >>> (1,2) & each %~ succ (2,3)@@ -41,7 +48,7 @@ >>> ["x", "y", "z"] ^. each "xyz" -However, note that 'each' doesn't work on /every/ instance of 'Traversable'. If you have a 'Traversable' which isn't supported by 'each', you can use 'traverse' instead. Personally, I like using 'each' instead of 'traverse' whenever possible – it's shorter and more descriptive.+However, note that 'each' doesn't work on /every/ instance of 'Traversable'. If you have a 'Traversable' which isn't supported by 'each', you can use 'traversed' instead. Personally, I like using 'each' instead of 'traversed' whenever possible – it's shorter and more descriptive.  You can use 'each' with these things: @@ -57,6 +64,8 @@  '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 importing @Lens.Micro.GHC@ from the <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> package.   -}   each :: Traversal s t a b   default each :: (Traversable g, s ~ g a, t ~ g b) => Traversal s t a b@@ -82,9 +91,100 @@   each f (a :+ b) = (:+) <$> f a <*> f b   {-# INLINE each #-} -instance Each [a] [b] a b+instance Each [a] [b] a b where+  each = traversed+  {-# INLINE each #-}  instance Each (Maybe a) (Maybe b) a b++type family Index (s :: *) :: *++type family IxValue (m :: *) :: *++type instance Index   (e -> a) = e+type instance IxValue (e -> a) = a+type instance Index   [a] = Int+type instance IxValue [a] = a++class Ixed m where+  {- |+This traversal lets you access (and update) an arbitrary element in a list, array, @Map@, etc. (If you want to insert or delete elements as well, look at 'at'.)++An example for lists:++>>> [0..5] & ix 3 .~ 10+[0,1,2,100,4,5]++You can use it for getting, too:++>>> [0..5] ^? ix 3+Just 3++Of course, the element may not be present (which means that you can use 'ix' as a safe variant of ('!!')):++>>> [0..5] ^? ix 10+Nothing++Another useful instance is the one for functions – it lets you modify their outputs for specific inputs. For instance, here's 'maximum' that returns 0 when the list is empty (instead of throwing an exception):++@+maximum0 = 'maximum' 'Lens.Micro.&' 'ix' [] 'Lens.Micro..~' 0+@++The following instances are provided in this package:++@+'ix' :: 'Int' -> 'Traversal'' [a] a++'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 importing @Lens.Micro.GHC@ from the <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> package.+  -}+  ix :: Index m -> Traversal' m (IxValue m)+  default ix :: (At m) => Index m -> Traversal' m (IxValue m)+  ix = ixAt+  {-# INLINE ix #-}++class Ixed m => At m where+  {- |+This lens lets you read, write, or delete elements in @Map@-like structures. It returns 'Nothing' when the value isn't found, just like @lookup@:++@+Data.Map.lookup k m = m 'Lens.Micro.^.' at k+@++However, it also lets you insert and delete values by setting the value to @'Just' value@ or 'Nothing':++@+Data.Map.insert k a m = m 'Lens.Micro.&' at k 'Lens.Micro..~' Just a++Data.Map.delete k m = m 'Lens.Micro.&' at k 'Lens.Micro..~' Nothing+@++'at' doesn't work for arrays, because you can't delete an arbitrary element from an array.++If you want to modify an already existing value, you should use 'ix' instead because then you won't have to deal with 'Maybe' ('ix' is available for all types that have 'at').++This package doesn't actually provide any instances for 'at', but you can import @Lens.Micro.GHC@ from the <http://hackage.haskell.org/package/microlens-ghc microlens-ghc> package and get instances for @Map@ and @IntMap@.+  -}+  at :: Index m -> Lens' m (Maybe (IxValue m))++ixAt :: At m => Index m -> Traversal' m (IxValue m)+ixAt i = at i . traverse+{-# INLINE ixAt #-}++instance Eq e => Ixed (e -> a) where+  ix e p f = (\a e' -> if e == e' then a else f e') <$> p (f e)+  {-# INLINE ix #-}++instance Ixed [a] where+  ix k f xs0 | k < 0     = pure xs0+             | otherwise = go xs0 k where+    go [] _ = pure []+    go (a:as) 0 = (:as) <$> f a+    go (a:as) i = (a:) <$> (go as $! i - 1)+  {-# INLINE ix #-}  class Field1 s t a b | s -> a, t -> b, s b -> t, t a -> s where   {- |
+ src/Lens/Micro/Internal.hs view
@@ -0,0 +1,119 @@+{-# LANGUAGE+CPP,+FlexibleContexts,+FlexibleInstances,+RankNTypes,+ScopedTypeVariables+  #-}++{- |+This module is needed to give other packages from the microlens family (like <http://hackage.haskell.org/package/microlens-ghc microlens-ghc>) access to functions that don't need to be exported from "Lens.Micro" (because they just clutter the namespace). Also, okay, uh, e.g. 'traversed' is here because otherwise there'd be a dependency cycle.+-}+module Lens.Micro.Internal+(+  traversed,+  folded,+  foldring,+  foldrOf,+  foldMapOf,+  sets,+  ( #. ),+  ( .# ),+)+where+++import Lens.Micro.Type++import Data.Functor.Identity+import Data.Monoid+import Control.Applicative+import Data.Foldable as F++#if __GLASGOW_HASKELL__ < 710+import Data.Traversable+#endif++#if __GLASGOW_HASKELL__ >= 708+import Data.Coerce+#else+import Unsafe.Coerce+#endif+  ++{- |+'traversed' traverses any 'Traversable' container (list, vector, @Map@, 'Maybe', you name it):++>>> Just 1 ^.. traversed+[1]++'traversed' is the same as 'traverse', but can be faster thanks to magic rewrite rules.+-}+traversed :: Traversable f => Traversal (f a) (f b) a b+traversed = traverse+{-# INLINE [0] traversed #-}++{-# RULES+"traversed -> mapped"+  traversed = sets fmap :: Functor f => ASetter (f a) (f b) a b;+"traversed -> folded"+  traversed = folded :: Foldable f => Getting (Endo r) (f a) a;+  #-}++{- |+'folded' is a fold for anything 'Foldable'. In a way, it's an opposite of+'mapped' – the most powerful getter, but can't be used as a setter.+-}+folded :: (Foldable f, Applicative (Const r)) => Getting r (f a) a+folded = foldring F.foldr+{-# INLINE folded #-}++foldring :: (Applicative (Const r)) => ((a -> Const r a -> Const r a) -> Const r a -> s -> Const r a) -> (a -> Const r b) -> s -> Const r t+foldring fr f = phantom . fr (\a fa -> f a *> fa) noEffect+{-# INLINE foldring #-}++foldrOf :: Getting (Endo r) s a -> (a -> r -> r) -> r -> s -> r+foldrOf l f z = flip appEndo z . foldMapOf l (Endo #. f)+{-# INLINE foldrOf #-}++foldMapOf :: Getting r s a -> (a -> r) -> s -> r+foldMapOf l f = getConst #. l (Const #. f)+{-# INLINE foldMapOf #-}++{- |+'sets' creates an 'ASetter' from an ordinary function. (The only thing it does is wrapping and unwrapping 'Identity'.)+-}+sets :: ((a -> b) -> s -> t) -> ASetter s t a b+sets f g = Identity #. f (runIdentity #. g)+{-# INLINE sets #-}++------------------------------------------------------------------------------+-- Control.Lens.Internal.Getter+------------------------------------------------------------------------------++-- was renamed from “coerce”+phantom :: Const r a -> Const r b+phantom = Const . getConst+{-# INLINE phantom #-}++noEffect :: Applicative (Const r) => Const r a+noEffect = phantom (pure ())+{-# INLINE noEffect #-}++------------------------------------------------------------------------------+-- Data.Profunctor.Unsafe+------------------------------------------------------------------------------++#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++( .# ) :: Coercible b a => (b -> c) -> (a -> b) -> (a -> c)+( .# ) pbc _ = coerce pbc+#else+( #. ) :: (b -> c) -> (a -> b) -> (a -> c)+( #. ) _ = unsafeCoerce++( .# ) :: (b -> c) -> (a -> b) -> (a -> c)+( .# ) pbc _ = unsafeCoerce pbc+#endif