lens 1.3 → 1.3.1
raw patch · 3 files changed
+158/−4 lines, 3 files
Files
- lens.cabal +2/−4
- src/Control/Lens.hs +47/−0
- src/Data/List/Lens.hs +109/−0
lens.cabal view
@@ -1,6 +1,6 @@ name: lens category: Data, Lenses-version: 1.3+version: 1.3.1 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE@@ -171,6 +171,7 @@ Data.Bits.Lens Data.Complex.Lens Data.Dynamic.Lens+ Data.List.Lens -- containers exposed-modules: Data.IntMap.Lens@@ -196,9 +197,6 @@ build-depends: parallel == 3.2.* exposed-modules: Control.Parallel.Strategies.Lens Control.Seq.Lens-- -- build-depends: time == 1.4.*- -- exposed-modules: Data.Time.Calendar.Lens Data.Time.Clock.Lens other-extensions: CPP
src/Control/Lens.hs view
@@ -93,8 +93,13 @@ , folds , folding , folded+ , unfolded+ , iterated , filtered , reversed+ , repeated+ , replicated+ , cycled , takingWhile , droppingWhile , view, views@@ -1109,6 +1114,48 @@ folded :: Foldable f => Fold (f c) c folded = folds foldMap {-# INLINE folded #-}++-- | Fold by repeating the input forever.+--+-- > repeat = toListOf repeated+repeated :: Fold a a+repeated f a = Const as where as = getConst (f a) <> as++-- | A fold that replicates its input @n@ times.+--+-- > replicate n = toListOf (replicated n)+replicated :: Int -> Fold a a+replicated n0 f a = Const (go n0) where+ m = getConst (f a)+ go 0 = mempty+ go n = m <> go (n - 1)+{-# INLINE replicated #-}++-- | Transform a fold into a fold that loops over its elements over and over.+--+-- > ghci> toListOf (cycled traverse) [1,2,3]+-- > [1,2,3,1,2,3,..]+cycled :: Monoid m => Getting m a b c d -> Getting m a b c d+cycled l f a = Const as where as = getConst (l f a) <> as++-- | Build a fold that unfolds its values from a seed.+--+-- > ghci> unfoldr = toListOf . unfolded+unfolded :: (b -> Maybe (a, b)) -> Fold b a+unfolded f g b0 = go b0 where+ go b = case f b of+ Just (a, b') -> g a *> go b'+ Nothing -> Const mempty+{-# INLINE unfolded #-}+++-- | @x ^. 'iterated' f@ Return an infinite fold of repeated applications of @f@ to @x@.+--+-- > toListOf (iterated f) a = iterate f a+iterated :: (a -> a) -> Fold a a+iterated f g a0 = go a0 where+ go a = g a *> go (f a)+{-# INLINE iterated #-} -- | Obtain a 'Fold' by filtering a 'Lens', 'Iso', 'Getter', 'Fold' or 'Traversal'. filtered :: Monoid m => (c -> Bool) -> Getting m a b c d -> Getting m a b c d
+ src/Data/List/Lens.hs view
@@ -0,0 +1,109 @@+{-# LANGUAGE LiberalTypeSynonyms #-}+{-# LANGUAGE Rank2Types #-}+-----------------------------------------------------------------------------+-- |+-- Module : Data.List.Lens+-- Copyright : (C) 2012 Edward Kmett+-- License : BSD-style (see the file LICENSE)+-- Maintainer : Edward Kmett <ekmett@gmail.com>+-- Stability : provisional+-- Portability : portable+--+-- Traversals for manipulating parts of a list.+--+----------------------------------------------------------------------------+module Data.List.Lens+ ( _head+ , _tail+ , _last+ , _init+ , interspersed+ , intercalated+ , traverseHead+ , traverseTail+ , traverseInit+ , traverseLast+ ) where++import Control.Applicative+import Control.Lens+import Data.List++-- | A lens reading and writing to the head of a /non-empty/ list+--+-- > ghci> [1,2,3]^._head+-- > 1+_head :: Simple Lens [a] a+_head _ [] = error "_head: empty list"+_head f (a:as) = (:as) <$> f a+{-# INLINE _head #-}++-- | A lens reading and writing to the tail of a /non-empty/ list+--+-- > ghci> _tail <~ [3,4,5] $ [1,2]+-- > [1,3,4,5]+_tail :: Simple Lens [a] [a]+_tail _ [] = error "_tail: empty list"+_tail f (a:as) = (a:) <$> f as+{-# INLINE _tail #-}++-- | A lens reading and writing to the last element of a /non-empty/ list+_last :: Simple Lens [a] a+_last _ [] = error "_last: empty list"+_last f [a] = return <$> f a+_last f (a:as) = (a:) <$> _last f as+{-# INLINE _last #-}++-- | A lens reading and replacing all but the a last element of a /non-empty/ list+_init :: Simple Lens [a] [a]+_init _ [] = error "_init: empty list"+_init f as = (++ [Prelude.last as]) <$> f (Prelude.init as)+{-# INLINE _init #-}++-- | Obtain a version of the list with the supplied value interspersed.+--+-- > ghci> "abcde"^.interspersed ','+-- > "a,b,c,d,e"+--+-- > xs^.interspersed a = intersperse a xs+interspersed :: a -> Getter [a] [a]+interspersed = to . intersperse+{-# INLINE interspersed #-}++-- | Obtain a version of the list with the supplied value intercalated+intercalated :: [a] -> Getter [[a]] [a]+intercalated = to . intercalate+{-# INLINE intercalated #-}++-- | The traversal for reading and writing to the head of a list+--+-- > traverseHead :: Applicative f => (a -> f a) -> [a] -> f [a]+traverseHead :: SimpleTraversal [a] a+traverseHead _ [] = pure []+traverseHead f (a:as) = (:as) <$> f a+{-# INLINE traverseHead #-}++-- | Traversal for editing the tail of a list.+--+-- > traverseTail :: Applicative f => (a -> f a) -> [a] -> f [a]+traverseTail :: SimpleTraversal [a] a+traverseTail _ [] = pure []+traverseTail f (a:as) = (a:) <$> traverse f as+{-# INLINE traverseTail #-}++-- | Traverse the last element in a list.+--+-- > traverseLast :: Applicative f => (a -> f a) -> [a] -> f [a]+traverseLast :: SimpleTraversal [a] a+traverseLast _ [] = pure []+traverseLast f [a] = return <$> f a+traverseLast f (a:as) = (a:) <$> traverseLast f as+{-# INLINE traverseLast #-}++-- | Traverse all but the last element of a list+--+-- > traverseInit :: Applicative f => (a -> f a) -> [a] -> f [a]+traverseInit :: SimpleTraversal [a] a+traverseInit _ [] = pure []+traverseInit f as = (++ [Prelude.last as]) <$> traverse f (Prelude.init as)+{-# INLINE traverseInit #-}