Skip to content

Commit

Permalink
Added loupe doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Dec 7, 2012
1 parent d3d2506 commit 81ea7ca
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Control/Lens/Loupe.hs
Expand Up @@ -74,30 +74,45 @@ type Loupe s t a b = LensLike (Context a b) s t a b
type SimpleLoupe s a = Loupe s s a a

-- | A 'Loupe'-specific version of ('Control.Lens.Getter.^.')
--
-- >>> ("hello","world")^#_2
-- "world"
(^#) :: s -> Loupe s t a b -> a
s ^# l = case l (Context id) s of
Context _ a -> a
{-# INLINE (^#) #-}

-- | A 'Loupe'-specific version of 'Control.Lens.Setter.set'
--
-- >>> storing _2 "world" ("hello","there")
-- ("hello","world")
storing :: Loupe s t a b -> b -> s -> t
storing l b s = case l (Context id) s of
Context g _ -> g b
{-# INLINE storing #-}

-- | A 'Loupe'-specific version of ('Control.Lens.Setter..~')
--
-- >>> ("hello","there") & _2 #~ "world"
-- ("hello","world")
(#~) :: Loupe s t a b -> b -> s -> t
(#~) l b s = case l (Context id ) s of
Context g _ -> g b
{-# INLINE (#~) #-}

-- | A 'Loupe'-specific version of ('Control.Lens.Setter.%~')
--
-- >>> ("hello","world") & _2 #%~ length
-- ("hello",5)
(#%~) :: Loupe s t a b -> (a -> b) -> s -> t
(#%~) l f s = case l (Context id) s of
Context g a -> g (f a)
{-# INLINE (#%~) #-}

-- | A 'Loupe'-specific version of ('Control.Lens.Type.%%~')
--
-- >>> ("hello","world") & _2 #%%~ \x -> (length x, x ++ "!")
-- (5,("hello","world!"))
(#%%~) :: Functor f => Loupe s t a b -> (a -> f b) -> s -> f t
(#%%~) l f s = case l (Context id) s of
Context g a -> g <$> f a
Expand All @@ -113,6 +128,9 @@ l #%= f = modify (l #%~ f)
{-# INLINE (#%=) #-}

-- | Modify the target of a 'Loupe' and return the result.
--
-- >>> ("hello","world") & _2 <#%~ length
-- (5,("hello",5))
(<#%~) :: Loupe s t a b -> (a -> b) -> s -> (b, t)
l <#%~ f = \s -> case l (Context id) s of
Context g a -> let b = f a in (b, g b)
Expand All @@ -137,6 +155,9 @@ l #%%= f = do
#endif

-- | Replace the target of a 'Loupe' and return the new value.
--
-- >>> ("hello","there") & _2 <#~ "world"
-- ("world",("hello","world"))
(<#~) :: Loupe s t a b -> b -> s -> (b, t)
l <#~ b = \s -> (b, storing l b s)

Expand Down

0 comments on commit 81ea7ca

Please sign in to comment.