34
34
-- |
35
35
-- | For more information, see `PrismsForSumTypes.purs` in the
36
36
-- | `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
+ -- | ```
37
68
38
69
module Data.Lens.Prism
39
- ( prism , prism'
70
+ ( prism' , prism
40
71
, only , nearly
41
72
, review
42
73
, is , isn't , matching
@@ -57,7 +88,7 @@ import Data.Profunctor (dimap, rmap)
57
88
import Data.Profunctor.Choice (right )
58
89
import Data.Newtype (under )
59
90
60
- -- | Create a `Prism` from a constructor and a "focus" function that
91
+ -- | Create a `Prism` from a constructor and a matcher function that
61
92
-- | produces an `Either`:
62
93
-- |
63
94
-- | ```purescript
@@ -66,10 +97,14 @@ import Data.Newtype (under)
66
97
-- | Solid color -> Right color
67
98
-- | anotherCase -> Left anotherCase
68
99
-- | ```
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.
69
104
prism :: forall s t a b . (b -> t ) -> (s -> Either t a ) -> Prism s t a b
70
105
prism to fro pab = dimap fro (either id id) (right (rmap to pab))
71
106
72
- -- | Create a `Prism` from a constructor and a "focus" function that
107
+ -- | Create a `Prism` from a constructor and a matcher function that
73
108
-- | produces a `Maybe`:
74
109
-- |
75
110
-- | ```purescript
@@ -81,29 +116,18 @@ prism to fro pab = dimap fro (either id id) (right (rmap to pab))
81
116
prism' :: forall s a . (a -> s ) -> (s -> Maybe a ) -> Prism' s a
82
117
prism' to fro = prism to (\s -> maybe (Left s) Right (fro s))
83
118
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`:
86
123
-- |
87
124
-- | ```purescript
88
- -- | brightSolidFocus :: Prism' Fill Unit
89
- -- | brightSolidFocus = nearly (Solid referenceColor ) predicate
125
+ -- | solidWhiteFocus :: Prism' Fill Unit
126
+ -- | solidWhiteFocus = nearly (Solid Color.white ) predicate
90
127
-- | 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
104
130
-- | ```
105
-
106
-
107
131
nearly :: forall a . a -> (a -> Boolean ) -> Prism' a Unit
108
132
nearly x f = prism' (const x) (guard <<< f)
109
133
@@ -117,6 +141,10 @@ nearly x f = prism' (const x) (guard <<< f)
117
141
-- | preview solidWhiteFocus (Solid Color.white) == Just unit
118
142
-- | review solidWhiteFocus unit == Solid Color.white
119
143
-- | ```
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.
120
148
only :: forall a . Eq a => a -> Prism a a Unit Unit
121
149
only x = nearly x (_ == x)
122
150
0 commit comments