From 5846864d6321bb3c686ba1c9c16c132fbc171b5e Mon Sep 17 00:00:00 2001 From: Edward Kmett Date: Mon, 9 Feb 2015 22:35:17 -0500 Subject: [PATCH] Added `like` and `ilike`. --- src/Control/Lens/Getter.hs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Control/Lens/Getter.hs b/src/Control/Lens/Getter.hs index 7780e64d0..8389864d8 100644 --- a/src/Control/Lens/Getter.hs +++ b/src/Control/Lens/Getter.hs @@ -51,6 +51,8 @@ module Control.Lens.Getter -- * Building Getters , to , ito + , like + , ilike -- * Combinators for Getters and Folds , (^.) , view, views @@ -131,6 +133,33 @@ ito :: (Indexable i p, Contravariant f) => (s -> (i, a)) -> Optical' p (->) f s ito k = dimap k (contramap (snd . k)) . uncurry . indexed {-# INLINE ito #-} + +-- | Build an constant-valued (index-preserving) 'Getter' from an arbitrary Haskell value. +-- +-- @ +-- 'like' a '.' 'like' b ≡ 'like' b +-- a '^.' 'like' b ≡ b +-- a '^.' 'like' b ≡ a '^.' 'to' ('const' b) +-- @ +-- +-- This can be useful as a second case 'failing' a 'Fold' +-- e.g. @foo `failing` 'like' 0@ +-- +-- @ +-- 'like' :: a -> 'IndexPreservingGetter' s a +-- @ +like :: (Profunctor p, Contravariant f) => a -> Optical' p p f s a +like a = to (const a) +{-# INLINE like #-} + +-- | +-- @ +-- 'ilike' :: i -> a -> 'IndexedGetter' i s a +-- @ +ilike :: (Indexable i p, Contravariant f) => i -> a -> Optical' p (->) f s a +ilike i a = ito (const (i, a)) +{-# INLINE ilike #-} + -- | When you see this in a type signature it indicates that you can -- pass the function a 'Lens', 'Getter', -- 'Control.Lens.Traversal.Traversal', 'Control.Lens.Fold.Fold',