3434-- |
3535-- | For more information, see `PrismsForSumTypes.purs` in the
3636-- | `examples/src` directory.
37+ -- |
38+ -- | ---------------
39+ -- |
40+ -- | A well-behaved `Prism` will follow these laws:
41+ -- |
42+ -- | **review-preview**: `preview` retrieves what `review` creates. Equationally:
43+ -- |
44+ -- | ```purescript
45+ -- | review prism >>> preview prism ≡ Just
46+ -- | ```
47+ -- |
48+ -- | An example:
49+ -- |
50+ -- | ```purescript
51+ -- | Color.white # review solidFocus # preview solidFocus
52+ -- | == Just Color.white
53+ -- | ```
54+ -- |
55+ -- | **preview-review**: If `preview` retrieves something, `review` can create
56+ -- | the original from that something. Equationally:
57+ -- |
58+ -- | ```purescript
59+ -- | if preview prism s ≡ Just a then review prism a ≡ s
60+ -- | ```
61+ -- |
62+ -- | An example:
63+ -- |
64+ -- | ```purescript
65+ -- | Solid Color.white # preview solidFocus <#> review solidFocus
66+ -- | == Solid Color.white
67+ -- | ```
3768
3869module Data.Lens.Prism
39- ( prism , prism'
70+ ( prism' , prism
4071 , only , nearly
4172 , review
4273 , is , isn't , matching
@@ -57,7 +88,7 @@ import Data.Profunctor (dimap, rmap)
5788import Data.Profunctor.Choice (right )
5889import Data.Newtype (under )
5990
60- -- | Create a `Prism` from a constructor and a "focus" function that
91+ -- | Create a `Prism` from a constructor and a matcher function that
6192-- | produces an `Either`:
6293-- |
6394-- | ```purescript
@@ -66,10 +97,14 @@ import Data.Newtype (under)
6697-- | Solid color -> Right color
6798-- | anotherCase -> Left anotherCase
6899-- | ```
100+ -- |
101+ -- | _Note_: The matcher function returns a result wrapped in `Either t`
102+ -- | to allow for type-changing prisms in the case where the input does
103+ -- | not match.
69104prism :: forall s t a b . (b -> t ) -> (s -> Either t a ) -> Prism s t a b
70105prism to fro pab = dimap fro (either id id) (right (rmap to pab))
71106
72- -- | Create a `Prism` from a constructor and a "focus" function that
107+ -- | Create a `Prism` from a constructor and a matcher function that
73108-- | produces a `Maybe`:
74109-- |
75110-- | ```purescript
@@ -81,29 +116,18 @@ prism to fro pab = dimap fro (either id id) (right (rmap to pab))
81116prism' :: forall s a . (a -> s ) -> (s -> Maybe a ) -> Prism' s a
82117prism' to fro = prism to (\s -> maybe (Left s) Right (fro s))
83118
84- -- | Create a prism that focuses on only some of the values of a case,
85- -- | such as solid colors that are "bright enough":
119+ -- | `nearly` is a variant of `only`. Like `only`, `nearly` produces
120+ -- | a prism that matches
121+ -- | a single value. Unlike `only`, it uses a predicate you supply
122+ -- | instead of depending on `class Eq`:
86123-- |
87124-- | ```purescript
88- -- | brightSolidFocus :: Prism' Fill Unit
89- -- | brightSolidFocus = nearly (Solid referenceColor ) predicate
125+ -- | solidWhiteFocus :: Prism' Fill Unit
126+ -- | solidWhiteFocus = nearly (Solid Color.white ) predicate
90127-- | where
91- -- | referenceColor = Color.graytone 0.8
92- -- | predicate = case _ of
93- -- | Solid color ->
94- -- | Color.brightness color >= Color.brightness referenceColor
95- -- | _ ->
96- -- | false
97- -- |
98- -- | preview brightSolidFocus (Solid Color.white) == Just unit
99- -- | preview brightSolidFocus (Solid Color.black) == Nothing
100- -- | preview brightSolidFocus NoFill == Nothing
101- -- |
102- -- | is brightSolidFocus (Solid Color.white) == true
103- -- | review brightSolidFocus unit == Color.graytone 0.8
128+ -- | predicate candidate =
129+ -- | color.toHexString == Color.white.toHexString
104130-- | ```
105-
106-
107131nearly :: forall a . a -> (a -> Boolean ) -> Prism' a Unit
108132nearly x f = prism' (const x) (guard <<< f)
109133
@@ -117,6 +141,10 @@ nearly x f = prism' (const x) (guard <<< f)
117141-- | preview solidWhiteFocus (Solid Color.white) == Just unit
118142-- | review solidWhiteFocus unit == Solid Color.white
119143-- | ```
144+ -- |
145+ -- | *Note*: `only` depends on `Eq`. Strange definitions of `(==)`
146+ -- | (for example, that it counts any `Fill` as being equal to `Solid Color.white`)
147+ -- | will create a prism that violates the preview-review law.
120148only :: forall a . Eq a => a -> Prism a a Unit Unit
121149only x = nearly x (_ == x)
122150
0 commit comments