Skip to content

Commit

Permalink
get combinator for partial lens
Browse files Browse the repository at this point in the history
  • Loading branch information
tonymorris committed Feb 28, 2012
1 parent 0fa9676 commit 531c07d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Data/Lens/Partial/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Data.Lens.Common (Lens(..))
import Control.Comonad.Trans.Store
import Data.Functor.Identity
import Data.Functor.Coproduct
import Data.Maybe

newtype PartialLens a b = PLens (a -> Maybe (Store b a))

Expand All @@ -33,6 +34,10 @@ totalLens (Lens f) = PLens (Just . f)
getPL :: PartialLens a b -> a -> Maybe b
getPL (PLens f) a = pos <$> f a

-- If the PartialLens is null, then return the given default value.
getorPL :: PartialLens a b -> a -> b -> b
getorPL l a b = fromMaybe b (getPL l a)

trySetPL :: PartialLens a b -> a -> Maybe (b -> a)
trySetPL (PLens f) a = flip peek <$> f a

Expand All @@ -52,6 +57,12 @@ infixr 0 ^$
infixr 9 ^.
(^.) = flip getPL

infixr 0 ^|$
(^|$) = getorPL

infixr 9 ^|.
(^|.) = flip getorPL

infixr 4 ^=
(^=) :: PartialLens a b -> b -> a -> a
(^=) = setPL
Expand Down

0 comments on commit 531c07d

Please sign in to comment.