Skip to content

Commit

Permalink
Merge pull request #720 from janpath/getting-indexed
Browse files Browse the repository at this point in the history
Generalise getting to work with any profunctor optical
  • Loading branch information
ekmett committed Mar 24, 2017
2 parents 9b80460 + cce90cd commit dbfc099
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Control/Lens/Getter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,16 @@ iuses l f = gets (getConst #. l (Const #. Indexed f))
s ^@. l = getConst $ l (Indexed $ \i -> Const #. (,) i) s
{-# INLINE (^@.) #-}

-- | Coerce a 'Getter'-compatible 'LensLike' to a 'LensLike''. This
-- | Coerce a 'Getter'-compatible 'Optical' to an 'Optical''. This
-- is useful when using a 'Traversal' that is not simple as a 'Getter' or a
-- 'Fold'.
getting :: (Functor f, Contravariant f) => LensLike f s t a b -> LensLike' f s a
getting l f = phantom . l (phantom . f)
--
-- @
-- 'getting' :: 'Traversal' s t a b -> 'Fold' s a
-- 'getting' :: 'Lens' s t a b -> 'Getter' s a
-- 'getting' :: 'IndexedTraversal' i s t a b -> 'IndexedFold' i s a
-- 'getting' :: 'IndexedLens' i s t a b -> 'IndexedGetter' i s a
-- @
getting :: (Profunctor p, Profunctor q, Functor f, Contravariant f)
=> Optical p q f s t a b -> Optical' p q f s a
getting l f = rmap phantom . l $ rmap phantom f

0 comments on commit dbfc099

Please sign in to comment.