microlens 0.4.12.0 → 0.4.13.0
raw patch · 3 files changed
+78/−6 lines, 3 filesnew-uploader
Files
- CHANGELOG.md +4/−0
- microlens.cabal +7/−6
- src/Lens/Micro.hs +67/−0
CHANGELOG.md view
@@ -1,3 +1,7 @@+# 0.4.13.0++* Added `_Show`, `worded`, and `lined`.+ # 0.4.12.0 * Added instance `Ixed (NonEmpty a)` for GHC >= 8.
microlens.cabal view
@@ -1,5 +1,5 @@ name: microlens-version: 0.4.12.0+version: 0.4.13.0 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!).@@ -32,7 +32,7 @@ license: BSD3 license-file: LICENSE author: Edward Kmett, Artyom Kazak-maintainer: Monadfix <hi@monadfix.com>+maintainer: Steven Fontanella <steven.fontanella@gmail.com> homepage: http://github.com/monadfix/microlens bug-reports: http://github.com/monadfix/microlens/issues -- copyright:@@ -41,16 +41,17 @@ extra-source-files: CHANGELOG.md cabal-version: >=1.10-tested-with: GHC==7.4.2- GHC==7.6.3+tested-with: 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.5- GHC==8.8.1- GHC==8.10.1+ GHC==8.8.4+ GHC==8.10.7+ GHC==9.0.1+ GHC==9.2.1 source-repository head type: git
src/Lens/Micro.hs view
@@ -93,11 +93,13 @@ ix, _head, _tail, _init, _last, mapAccumLOf,+ worded, lined, -- * Prism: a traversal iterating over at most 1 element -- $prisms-note _Left, _Right, _Just, _Nothing,+ _Show, -- * Other types LensLike, LensLike',@@ -111,6 +113,7 @@ import Control.Applicative import Control.Monad import Data.Functor.Identity+import Data.List (intercalate) import Data.Monoid import Data.Maybe import Data.Tuple@@ -118,6 +121,8 @@ #if MIN_VERSION_base(4,8,0) import Data.Function ((&))+#else+import Data.Traversable (traverse) #endif #if MIN_VERSION_base(4,11,0)@@ -1260,6 +1265,37 @@ g a = state $ \acc -> swap (f acc a) {-# INLINE mapAccumLOf #-} +{- |+Focus on the 'words' of a string.++>>> "avoid success at all costs" & worded . _head %~ toUpper+"Avoid Success At All Costs"++This violates the traversal laws when whitespace is set or when the source has+space at the ends or more than one contiguous space anywhere.+-}+worded :: Traversal' String String+worded f = fmap unwords . traverse f . words+{-# INLINE worded #-}++{- |+Focus on the 'lines' of a string.++@+countAndMarkEmptyLines :: String -> State Int String+countAndMarkEmptyLines s = runState (f s) 0 where+ f = 'traverseOf' (lined . 'filtered' null) $ \\_ -> do+ modify' (+ 1)+ return "# Empty line"+@++This violates the traversal laws when newlines are set or when the source has+more than one contiguous newline anywhere.+-}+lined :: Traversal' String String+lined f = fmap (intercalate "\n") . traverse f . lines+{-# INLINE lined #-}+ -- Prisms ------------------------------------------------------------------ {- $prisms-note@@ -1381,6 +1417,37 @@ _Nothing f Nothing = const Nothing <$> f () _Nothing _ j = pure j {-# INLINE _Nothing #-}++{- |+'_Show' targets the Haskell value in a @String@ using 'Read', or nothing if parsing fails. Likewise, setting a Haskell value through this prism renders a @String@ using 'Show'.++>>> ["abc","8","def","9"] & mapped . _Show %~ \x -> x + 1 :: Int+["abc","9","def","10"]++Note that this prism is improper for types that don\'t satisfy @read . show = id@:++>>> "25.9999999" & _Show %~ \x -> x :: Float+"26.0"++These functions from @base@ can be expressed in terms of '_Show':++ * Unsafely parsing a value from a 'String':++ @+ 'read' = ('^?!' '_Show')+ @++ * Safely parsing a value from a 'String':++ @+ 'Text.Read.readMaybe' = ('^?' '_Show')+ @+-}+_Show :: (Show a, Read a) => Traversal' String a+_Show f s = case reads s of+ [(a,"")] -> show <$> f a+ _ -> pure s+{-# INLINE _Show #-} -- Some of the guts of lens