papa-0.7.0: src/Papa/Lens/Optic.hs
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Reading exactly one value through an optic.
--
-- 'Control.Lens.view' alone accepts an affine traversal whose target is a
-- 'Data.Monoid.Monoid', and gives 'Data.Monoid.mempty' where there is no
-- target. 'the' accepts only an optic that always has exactly one target,
-- a lens or a getter, and rejects a prism, an affine traversal or an
-- affine fold at compile time.
--
-- A name is read with 'the', never with 'Control.Lens.view' or
-- 'Control.Lens.^.' alone (classy optics, rule 8).
--
-- @since 0.7.0
module Papa.Lens.Optic (
the,
) where
import Control.Lens (LensLike', ReifiedGetter (Getter, runGetter), view)
import Data.Functor (Functor)
import Data.Functor.Contravariant (Contravariant)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- | The one value through an iso, a lens or a getter.
--
-- @since 0.7.0
--
-- ==== __Examples__
--
-- >>> the _1 ('a', True)
-- 'a'
--
-- >>> check $ withTests 100 $ property $ do a <- forAll Gen.alpha; b <- forAll Gen.bool; the _2 (a, b) === view _2 (a, b)
-- ...passed 100 tests.
-- True
the :: (forall f. (Contravariant f, Functor f) => LensLike' f s a) -> s -> a
the n =
view (runGetter (Getter n))
{-# INLINE the #-}