Skip to content

Commit 0397cd7

Browse files
committed
Addressed comments on previous version
1 parent 5bc0644 commit 0397cd7

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

src/Data/Lens/Prism.purs

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,40 @@
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

3869
module 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)
5788
import Data.Profunctor.Choice (right)
5889
import 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.
69104
prism :: forall s t a b. (b -> t) -> (s -> Either t a) -> Prism s t a b
70105
prism 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))
81116
prism' :: forall s a. (a -> s) -> (s -> Maybe a) -> Prism' s a
82117
prism' 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-
107131
nearly :: forall a. a -> (a -> Boolean) -> Prism' a Unit
108132
nearly 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.
120148
only :: forall a. Eq a => a -> Prism a a Unit Unit
121149
only x = nearly x (_ == x)
122150

src/Data/Lens/Traversal.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- | `Traversal` is an optic that focuses on zero or more functor values. An
1+
-- | `Traversal` is an optic that focuses on zero or more values. An
22
-- | `Array` would be a typical example:
33
-- |
44
-- | ```purescript
@@ -103,13 +103,15 @@ failover t f s = case unwrap (t $ Star $ Tuple (Disj true) <<< f) s of
103103
Tuple (Disj false) _ -> empty
104104

105105
-- | Combine an index and a traversal to narrow the focus to a single
106-
-- | element. This is called an "affine traversal". Compare to `Data.Lens.Index`.
106+
-- | element. Compare to `Data.Lens.Index`.
107107
-- |
108108
-- | ```purescript
109109
-- | set (element 2 traversed) 8888 [0, 0, 3] == [0, 0, 8888]
110110
-- | preview (element 2 traversed) [0, 0, 3] == Just 3
111111
-- | ```
112-
112+
-- | The resulting traversal is called an *affine traversal*, which
113+
-- | means that the traversal focuses on one or zero (if the index is out of range)
114+
-- | results.
113115
element
114116
:: forall p s t a
115117
. Wander p

0 commit comments

Comments
 (0)