microlens 0.4.4.3 → 0.4.5.0
raw patch · 3 files changed
+39/−3 lines, 3 files
Files
- CHANGELOG.md +4/−0
- microlens.cabal +1/−1
- src/Lens/Micro.hs +34/−2
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.4.5.0++* Added `<&>` (which makes lens creation easier).+ # 0.4.4.3 * Fixed markup in the .cabal file.
microlens.cabal view
@@ -1,5 +1,5 @@ name: microlens-version: 0.4.4.3+version: 0.4.5.0 synopsis: A tiny lens library with no dependencies. If you're writing an app, you probably want microlens-platform, not this. 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!).
src/Lens/Micro.hs view
@@ -32,6 +32,7 @@ ( (&), -- $ampersand-note+ (<&>), -- * Setter: modifies something in a structure -- $setters-note@@ -155,6 +156,35 @@ @ -} +{- |+('<&>') is flipped ('<$>'):++@+x '<&>' f = f '<$>' x+@++It's often useful when writing lenses. For instance, let's say you're writing 'ix' for @Map@; if the key is found in the map, you have to apply a function to it and then change the map based on the new value – which requires a lambda, like this:++@+'ix' key f map = case Map.lookup key map of+ Just val -> (\\val' -> Map.insert key val' map) '<$>' f val+ Nothing -> 'pure' map+@++With ('<&>') you can get rid of parentheses and move the long lambda expression to the right of the value (like when you use '>>='):++@+'ix' key f map = case Map.lookup key map of+ Just val -> f val '<&>' \\val' -> Map.insert key val' map+ Nothing -> 'pure' map+@+-}+(<&>) :: Functor f => f a -> (a -> b) -> f b+(<&>) x f = f <$> x+{-# INLINE (<&>) #-}++infixl 1 <&>+ -- Setting ----------------------------------------------------------------- {- $setters-note@@ -795,8 +825,10 @@ filtered p s = if p s then f s else 'pure' s @ -By the way, note that 'filtered' can generate illegal traversals – sometimes this can bite you. For instance, take @evens@:+By the way, note that 'filtered' can generate illegal traversals – sometimes this can bite you. In particular, an optimisation that should be safe becomes unsafe. (To the best of my knowledge, this optimisation never happens automatically. If you just use 'filtered' to modify/view something, you're safe. If you don't define any traversals that use 'filtered', you're safe too.) +Let's use @evens@ as an example:+ @ evens = 'filtered' 'even' @@@ -813,7 +845,7 @@ * the right-side variant applies @f@ and @g@ to all even numbers -Of course, when you are careful and know what you're doing, you won't try to make such an optimisation. However, if you export an illegal traversal created with 'filtered' and someone tries to use it, ne might mistakenly assume that it's legal, do the optimisation, and silently get an incorrect result.+Of course, when you are careful and know what you're doing, you won't try to make such an optimisation. However, if you export an illegal traversal created with 'filtered' and someone tries to use it, they might mistakenly assume that it's legal, do the optimisation, and silently get an incorrect result. If you are using 'filtered' with some another traversal that doesn't overlap with -whatever the predicate checks-, the resulting traversal will be legal. For instance, here the predicate looks at the 1st element of a tuple, but the resulting traversal only gives you access to the 2nd: