Skip to content
Browse files

Remove references to remit from the documentation

  • Loading branch information...
1 parent 3c87e15 commit 544cdaa7fa4e589272ed833ffdace0495933e07a @ehird ehird committed Jan 12, 2013
Showing with 31 additions and 8 deletions.
  1. +4 −4 src/Control/Lens/Fold.hs
  2. +25 −2 src/Control/Lens/Internal/Fold.hs
  3. +2 −2 src/Control/Lens/Type.hs
View
8 src/Control/Lens/Fold.hs
@@ -1276,7 +1276,7 @@ hasn't l = getAll #. foldMapOf l (\_ -> All False)
-- | This converts a 'Fold' to a 'Getter' that returns the first element if it
-- exists as a 'Maybe'.
-pre :: Getting (Endo r) s t a b -> Getting r s t (Maybe a) (Maybe b)
+pre :: Getting (Leftmost r) s t a b -> Getting r s t (Maybe a) (Maybe b)
pre l f = (Accessor #. flip appEndo (runAccessor (f Nothing)) .# runAccessor) `rmap` l (dimap Just (Accessor #. Endo #. const .# runAccessor) f)
------------------------------------------------------------------------------
@@ -1311,8 +1311,8 @@ pre l f = (Accessor #. flip appEndo (runAccessor (f Nothing)) .# runAccessor) `r
-- 'preview' :: 'MonadReader' s m => 'Iso'' s a -> m ('Maybe' a)
-- 'preview' :: 'MonadReader' s m => 'Traversal'' s a -> m ('Maybe' a)
-- @
-preview :: MonadReader s m => Getting (Endo (Maybe a)) s t a b -> m (Maybe a)
-preview l = asks (foldrOf l (\x _ -> Just x) Nothing)
+preview :: MonadReader s m => Getting (Leftmost a) s t a b -> m (Maybe a)
+preview l = asks (getLeftmost . foldMapOf l LLeaf)
{-# INLINE preview #-}
-- | Retrieve a function of the first value targeted by a 'Fold' or
@@ -1394,7 +1394,7 @@ preuses l f = gets (previews l f)
-- This has no practical impact on a 'Getter', 'Setter', 'Lens' or 'Iso'.
--
-- /NB:/ To write back through an 'Iso', you want to use 'Control.Lens.Isomorphic.from'.
--- Similarly, to write back through an 'Prism', you want to use 'Control.Lens.Prism.remit'.
+-- Similarly, to write back through an 'Prism', you want to use 'Control.Lens.Review.re'.
backwards :: (Profunctor p, Profunctor q) => Overloading p q (Backwards f) s t a b -> Overloading p q f s t a b
backwards l f = forwards #. l (Backwards #. f)
{-# INLINE backwards #-}
View
27 src/Control/Lens/Internal/Fold.hs
@@ -20,6 +20,7 @@ module Control.Lens.Internal.Fold
, Sequenced(..)
, Max(..), getMax
, Min(..), getMin
+ , Leftmost(..), getLeftmost
, Rightmost(..), getRightmost
) where
@@ -110,9 +111,32 @@ getMax (Max a) = Just a
{-# INLINE getMax #-}
------------------------------------------------------------------------------
--- Rightmost
+-- Leftmost and Rightmost
------------------------------------------------------------------------------
+-- | Used for 'Control.Lens.Fold.preview'
+data Leftmost a = LPure | LLeaf a | LStep (Leftmost a)
+
+instance Monoid (Leftmost a) where
+ mempty = LPure
+ {-# INLINE mempty #-}
+ mappend x y = LStep $ case x of
+ RPure -> y
+ RLeaf _ -> x
+ RStep x' -> case y of
+ -- The last two cases make firstOf produce a Just as soon as any element
+ -- is encountered, and possibly serve as a micro-optimisation; this
+ -- behaviour can be disabled by replacing them with _ -> mappend x y'.
+ -- Note that this means that firstOf (backwards folded) [1..] is Just _|_.
+ LPure -> x'
+ LLeaf a -> LLeaf $ fromMaybe a (getLeftmost x')
+ LStep x' -> mappend x' y'
+
+getLeftmost :: Leftmost a -> Maybe a
+getLeftmost LPure = Nothing
+getLeftmost (LLeaf a) = Just a
+getLeftmost (LStep x) = getLeftmost x
+
-- | Used for 'Control.Lens.Fold.lastOf'
data Rightmost a = RPure | RLeaf a | RStep (Rightmost a)
@@ -135,4 +159,3 @@ getRightmost :: Rightmost a -> Maybe a
getRightmost RPure = Nothing
getRightmost (RLeaf a) = Just a
getRightmost (RStep x) = getRightmost x
-
View
4 src/Control/Lens/Type.hs
@@ -267,12 +267,12 @@ type Iso' s a = Iso s s a a
------------------------------------------------------------------------------
-- | A 'Prism' @l@ is a 0-or-1 target 'Traversal' that can also be turned
--- around with 'Control.Lens.Review.remit' to obtain a 'Getter' in the
+-- around with 'Control.Lens.Review.re' to obtain a 'Getter' in the
-- opposite direction.
--
-- There are two laws that a 'Prism' should satisfy:
--
--- First, if I 'Control.Lens.Review.remit' or 'Control.Lens.Prism.review' a value with a 'Prism' and then 'Control.Lens.Prism.preview' or use ('Control.Lens.Fold.^?'), I will get it back:
+-- First, if I 'Control.Lens.Review.re' or 'Control.Lens.Prism.review' a value with a 'Prism' and then 'Control.Lens.Prism.preview' or use ('Control.Lens.Fold.^?'), I will get it back:
--
-- * @'Control.Lens.Prism.preview' l ('Control.Lens.Prism.review' l b) ≡ 'Just' b@
--

0 comments on commit 544cdaa

Please sign in to comment.
Something went wrong with that request. Please try again.