Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed given to `indices` (and added `index` to match elements/eleme…

…nt).
  • Loading branch information...
commit ff3d2d5519832a9e79bfe62fe70b958f177b6f4b 1 parent 5291c96
@ekmett authored
View
2  CHANGELOG.markdown
@@ -15,7 +15,7 @@
* Added `Control.Lens.Equality` for witnessing type equality, effectively a stronger form of `Iso` that can be used to safely cast.
* Added combinators and prisms to `Control.Exception.Lens` that enable you to work with GHC's extensible exceptions more easily.
* Added Numeric.Lens: `base` shows and reads integers at base-2 through base-36.
-* Added `given`, and removed `iwhere` and `iwhereOf`. Use `itraversed.given foo` and `bar.given foo` instead.
+* Added `index` and `indices`, and removed `iwhere` and `iwhereOf`. Use `itraversed.indices even` and `bar.indices (>3)` instead.
* Generalized combinators to pass through indices unmodified wherever possible.
3.7.1.2 [maintenance release]
View
5 src/Control/Lens/At.hs
@@ -43,8 +43,7 @@ module Control.Lens.At
import Control.Applicative
import Control.Lens.Combinators
import Control.Lens.Each
-import Control.Lens.Fold
-import Control.Lens.Indexed
+import Control.Lens.Indexed as Lens
import Control.Lens.Type
import Control.Lens.Traversal
import Data.Array.IArray as Array
@@ -122,7 +121,7 @@ ixAt i = at i <. traverse
-- | A definition of 'ix' for types with an 'Each' instance.
ixEach :: (Indexable (IxKey m) p, Applicative f, Eq (IxKey m), Each (IxKey m) f m m (IxValue m) (IxValue m)) => IxKey m -> IndexedLensLike' p f m (IxValue m)
-ixEach i = each.given (i ==)
+ixEach i = each . Lens.index i
{-# INLINE ixEach #-}
type instance IxKey [a] = Int
View
11 src/Control/Lens/Fold.hs
@@ -90,7 +90,6 @@ module Control.Lens.Fold
, (^@?!)
-- ** Indexed Folding
- , given
, ifoldMapOf
, ifoldrOf
, ifoldlOf
@@ -1630,16 +1629,6 @@ ifiltering :: (Applicative f, Indexable i p)
ifiltering p l f = l . Indexed $ \ i c -> if p i c then indexed f i c else pure c
{-# INLINE ifiltering #-}
--- | This allows you to filter an 'IndexedFold', 'IndexedGetter', 'IndexedTraversal' or 'IndexedLens' based on an index.
---
--- >>> ["hello","the","world","!!!"]^..traversed.given even
--- ["hello","world"]
---
--- >>> over (traversed.given (>0)) Prelude.reverse $ ["He","was","stressed","o_O"]
--- ["He","saw","desserts","O_o"]
-given :: (Indexable i p, Applicative f) => (i -> Bool) -> Overloading' p (Indexed i) f a a
-given p f = Indexed $ \i a -> if p i then indexed f i a else pure a
-
-- | Obtain an 'IndexedFold' by taking elements from another
-- 'IndexedFold', 'IndexedLens',
-- 'IndexedGetter' or 'IndexedTraversal'
View
32 src/Control/Lens/Indexed.hs
@@ -59,6 +59,9 @@ module Control.Lens.Indexed
, itoList
-- * Converting to Folds
, withIndex
+ , asIndex
+ -- * Restricting by Index
+ , indices
, index
-- * Indexed Traversables
, TraversableWithIndex(..)
@@ -148,10 +151,35 @@ withIndex f = Indexed $ \i a -> snd <$> indexed f i (i, a)
-- | When composed with an indexed fold or indexed traversal this yields an
-- (indexed) fold of the indices.
-index :: (Indexable i p, Functor f, Gettable f) => Overloading' p (Indexed i) f s i
-index f = Indexed $ \i _ -> coerce (indexed f i i)
+asIndex :: (Indexable i p, Functor f, Gettable f) => Overloading' p (Indexed i) f s i
+asIndex f = Indexed $ \i _ -> coerce (indexed f i i)
+{-# INLINE asIndex #-}
+
+-------------------------------------------------------------------------------
+-- Restricting by index
+-------------------------------------------------------------------------------
+
+-- | This allows you to filter an 'IndexedFold', 'IndexedGetter', 'IndexedTraversal' or 'IndexedLens' based on a predicate
+-- on the indices.
+--
+-- >>> ["hello","the","world","!!!"]^..traversed.indices even
+-- ["hello","world"]
+--
+-- >>> over (traversed.indices (>0)) Prelude.reverse $ ["He","was","stressed","o_O"]
+-- ["He","saw","desserts","O_o"]
+indices :: (Indexable i p, Applicative f) => (i -> Bool) -> Overloading' p (Indexed i) f a a
+indices p f = Indexed $ \i a -> if p i then indexed f i a else pure a
+{-# INLINE indices #-}
+
+-- | This allows you to filter an 'IndexedFold', 'IndexedGetter', 'IndexedTraversal' or 'IndexedLens' based on an index.
+--
+-- >>> ["hello","the","world","!!!"]^?traversed.index 2
+-- Just "world"
+index :: (Indexable i p, Eq i, Applicative f) => i -> Overloading' p (Indexed i) f a a
+index j f = Indexed $ \i a -> if j == i then indexed f i a else pure a
{-# INLINE index #-}
+
-------------------------------------------------------------------------------
-- FunctorWithIndex
-------------------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.