Data.Lens.Prism
- Package
- purescript-profunctor-lenses
- Repository
- purescript-contrib/purescript-profunctor-lenses
Prisms are used for selecting cases of a type, most often a sum type. Consider this:
data Fill -- think of a paint program filling a shape
= NoFill
| Solid Color
| ...
A prism that focuses on Solid
fills could be written like this:
solidFocus :: Prism' Fill Color
solidFocus = prism' Solid case _ of
Solid color -> Just color
_ -> Nothing
... and used like this:
preview solidFocus (Solid Color.white) == Just Color.white
preview solidFocus NoFill == Nothing
is solidFocus (Solid Color.white) == true
review
can be used to go from a Color
to a Fill
:
review solidFocus Color.white == Solid Color.white
For more information, see PrismsForSumTypes.purs
in the
examples/src
directory.
A well-behaved Prism
will follow these laws:
review-preview: preview
retrieves what review
creates. Equationally:
review prism >>> preview prism ≡ Just
An example:
Color.white # review solidFocus # preview solidFocus
== Just Color.white
preview-review: If preview
retrieves something, review
can create
the original from that something. Equationally:
if preview prism s ≡ Just a then review prism a ≡ s
An example:
Solid Color.white # preview solidFocus <#> review solidFocus
== Solid Color.white
#prism Source
prism :: forall s t a b. (b -> t) -> (s -> Either t a) -> Prism s t a b
Create a Prism
from a constructor and a matcher function that
produces an Either
:
solidFocus :: Prism' Fill Color
solidFocus = prism Solid case _ of
Solid color -> Right color
anotherCase -> Left anotherCase
Note: The matcher function returns a result wrapped in Either t
to allow for type-changing prisms in the case where the input does
not match.
#only Source
only :: forall a. Eq a => a -> Prism a a Unit Unit
only
focuses not just on a case, but a specific value of that case.
solidWhiteFocus :: Prism' Fill Unit
solidWhiteFocus = only $ Solid Color.white
is solidWhiteFocus (Solid Color.white) == true
preview solidWhiteFocus (Solid Color.white) == Just unit
review solidWhiteFocus unit == Solid Color.white
Note: only
depends on Eq
. Strange definitions of (==)
(for example, that it counts any Fill
as being equal to Solid Color.white
)
will create a prism that violates the preview-review law.
#nearly Source
nearly :: forall a. a -> (a -> Boolean) -> Prism' a Unit
nearly
is a variant of only
. Like only
, nearly
produces
a prism that matches
a single value. Unlike only
, it uses a predicate you supply
instead of depending on class Eq
:
solidWhiteFocus :: Prism' Fill Unit
solidWhiteFocus = nearly (Solid Color.white) predicate
where
predicate candidate =
color.toHexString == Color.white.toHexString
#is Source
is :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> r
Ask if preview prism
would produce a Just
.
#isn't Source
isn't :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> r
Ask if preview prism
would produce a Nothing
.
#clonePrism Source
clonePrism :: forall s t a b. APrism s t a b -> Prism s t a b
Re-exports from Data.Lens.Types
- Modules
- Data.
Lens - Data.
Lens. AffineTraversal - Data.
Lens. At - Data.
Lens. Common - Data.
Lens. Fold - Data.
Lens. Fold. Partial - Data.
Lens. Getter - Data.
Lens. Grate - Data.
Lens. Index - Data.
Lens. Indexed - Data.
Lens. Internal. Bazaar - Data.
Lens. Internal. Exchange - Data.
Lens. Internal. Focusing - Data.
Lens. Internal. Forget - Data.
Lens. Internal. Grating - Data.
Lens. Internal. Indexed - Data.
Lens. Internal. Market - Data.
Lens. Internal. Re - Data.
Lens. Internal. Shop - Data.
Lens. Internal. Stall - Data.
Lens. Internal. Tagged - Data.
Lens. Internal. Wander - Data.
Lens. Internal. Zipping - Data.
Lens. Iso - Data.
Lens. Iso. Newtype - Data.
Lens. Lens - Data.
Lens. Lens. Product - Data.
Lens. Lens. Tuple - Data.
Lens. Lens. Unit - Data.
Lens. Lens. Void - Data.
Lens. Prism - Data.
Lens. Prism. Coproduct - Data.
Lens. Prism. Either - Data.
Lens. Prism. Maybe - Data.
Lens. Record - Data.
Lens. Setter - Data.
Lens. Traversal - Data.
Lens. Types - Data.
Lens. Zoom