Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dependency injection patterns for optics. #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions profunctor-optics/profunctor-optics.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library

Data.Profunctor.Optic
Data.Profunctor.Optic.Types
Data.Profunctor.Optic.Module
Data.Profunctor.Optic.Property
Data.Profunctor.Optic.Carrier
Data.Profunctor.Optic.Combinator
Expand Down
2 changes: 2 additions & 0 deletions profunctor-optics/src/Data/Profunctor/Optic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{-# LANGUAGE TypeFamilies #-}
module Data.Profunctor.Optic (
module Types
, module Module
, module Carrier
, module Operator
, module Iso
Expand All @@ -21,6 +22,7 @@ module Data.Profunctor.Optic (
) where

import Data.Profunctor.Optic.Types as Types
import Data.Profunctor.Optic.Module as Module
import Data.Profunctor.Optic.Carrier as Carrier
import Data.Profunctor.Optic.Combinator as Operator
import Data.Profunctor.Optic.Iso as Iso
Expand Down
41 changes: 12 additions & 29 deletions profunctor-optics/src/Data/Profunctor/Optic/Fold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ module Data.Profunctor.Optic.Fold (
, (^?)
, preview
, preuse
, is
, isnt
, fits
, lists
, (^..)
, nelists
Expand All @@ -60,8 +59,7 @@ module Data.Profunctor.Optic.Fold (
, endo
, endoM
, finds
, has
, hasnt
, exists
, contains
-- * Auxilliary Types
, Nedl(..)
Expand Down Expand Up @@ -128,7 +126,7 @@ fold0 f = to (\s -> maybe (Left s) Right (f s)) . right'

infixl 3 `failing`

-- | If the first 'Fold0' has no focus then try the second one.
-- | If the first 'Fold0' exists no focus then try the second one.
--
failing :: AFold0 a s a -> AFold0 a s a -> Fold0 s a
failing a b = fold0 $ \s -> maybe (preview b s) Just (preview a s)
Expand Down Expand Up @@ -335,23 +333,14 @@ preuse :: MonadState s m => AFold0 a s a -> m (Maybe a)
preuse o = State.gets $ preview o
{-# INLINE preuse #-}

-- | Check whether the optic is matched.
-- | Check whether the optic fits.
--
-- >>> is just Nothing
-- >>> fits just Nothing
-- False
--
is :: AFold0 a s a -> s -> Bool
is o s = isJust (preview o s)
{-# INLINE is #-}

-- | Check whether the optic isn't matched.
--
-- >>> isnt just Nothing
-- True
--
isnt :: AFold0 a s a -> s -> Bool
isnt o s = not (isJust (preview o s))
{-# INLINE isnt #-}
fits :: AFold0 a s a -> s -> Bool
fits o s = isJust (preview o s)
{-# INLINE fits #-}

-- | Collect the foci of an optic into a list.
--
Expand Down Expand Up @@ -566,17 +555,11 @@ finds :: AFold ((Maybe-Endo) a) s a -> (a -> Bool) -> s -> Maybe a
finds o f = foldsr o (\a y -> if f a then Just a else y) Nothing
{-# INLINE finds #-}

-- | Determine whether an optic has at least one focus.
--
has :: AFold (Additive Bool) s a -> s -> Bool
has o s = unAdditive $ withFold o (const $ Additive True) s
{-# INLINE has #-}

-- | Determine whether an optic does not have a focus.
-- | Determine whether an optic exists at least one focus.
--
hasnt :: AFold (Multiplicative Bool) s a -> s -> Bool
hasnt o s = unMultiplicative $ withFold o (const $ Multiplicative False) s
{-# INLINE hasnt #-}
exists :: AFold (Additive Bool) s a -> s -> Bool
exists o s = unAdditive $ withFold o (const $ Additive True) s
{-# INLINE exists #-}

-- | Determine whether the targets of a `Fold` contain a given element.
--
Expand Down
9 changes: 8 additions & 1 deletion profunctor-optics/src/Data/Profunctor/Optic/Lens.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module Data.Profunctor.Optic.Lens (
, voided
, represented
, distributed
, distributed1
, endomorphed
, precomposed
, dotted
Expand Down Expand Up @@ -104,7 +105,7 @@ lens sa sbt = dimap (id &&& sa) (uncurry sbt) . second'

-- | Transform a Van Laarhoven lens into a profunctor lens.
--
-- Compare 'Data.Profunctor.Optic.Grate.grateVl' and 'Data.Profunctor.Optic.Traversal.traversalVl'.
-- Compare 'grateVl' and 'Data.Profunctor.Optic.Traversal.traversalVl'.
--
-- /Caution/: In order for the generated optic to be well-defined,
-- you must ensure that the input satisfies the following properties:
Expand Down Expand Up @@ -297,6 +298,12 @@ distributed :: Distributive f => Grate (f a) (f b) a b
distributed = grate (`cotraverse` id)
{-# INLINE distributed #-}

-- | Obtain a 'Grate' from a partially distributive functor.
--
distributed1 :: Monoid (f a) => Distributive1 f => Grate (f a) (f b) a b
distributed1 = grate (`cotraverse1` id)
{-# INLINE distributed1 #-}

-- | Obtain a 'Grate' from an endomorphism.
--
-- >>> flip appEndo 2 $ zipsWith2 endomorphed (+) (Endo (*3)) (Endo (*4))
Expand Down
Loading