Skip to content

Commit 5b31641

Browse files
committed
Merge branch 'new'
2 parents 4507fd8 + ef9e53e commit 5b31641

File tree

2 files changed

+159
-10
lines changed

2 files changed

+159
-10
lines changed

src/Data/Lens/Prism.purs

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
1-
-- | This module defines functions for working with prisms.
1+
-- | Prisms are used for selecting cases of a type, most often a sum
2+
-- | type. Consider this:
3+
-- |
4+
-- | ```purescript
5+
-- | data Fill -- think of a paint program filling a shape
6+
-- | = Solid Color
7+
-- | ...
8+
-- | ```
9+
-- |
10+
-- | A prism that focuses on `Solid` fills would be written like this:
11+
-- |
12+
-- | ```purescript
13+
-- | solidFocus :: Prism' Fill Color
14+
-- | solidFocus = prism' Solid case _ of
15+
-- | Solid color -> Just color
16+
-- | _ -> Nothing
17+
-- | ```
18+
-- |
19+
-- | ... and used like this:
20+
-- |
21+
-- | ```purescript
22+
-- | preview solidFocus (Solid Color.white) == Just Color.white
23+
-- | preview solidFocus NoFill == Nothing
24+
-- |
25+
-- | is solidFocus (Solid Color.white) == true
26+
-- | ```
27+
-- |
28+
-- | `review` can be used to go from a `Color` to a `Fill`:
29+
-- |
30+
-- | ```purescript
31+
-- | review solidFocus Color.white == Solid Color.white
32+
-- | ```
33+
-- |
34+
-- | For more information, see `PrismsForSumTypes` in the
35+
-- | `examples/src` directory.
36+
237
module Data.Lens.Prism
3-
( prism, prism', review, nearly, only, clonePrism, withPrism, matching
38+
( prism, prism'
39+
, only, nearly
40+
, review
441
, is, isn't
42+
, clonePrism, withPrism, matching
543
, module ExportTypes
644
) where
745

@@ -18,23 +56,79 @@ import Data.Profunctor (dimap, rmap)
1856
import Data.Profunctor.Choice (right)
1957
import Data.Newtype (under)
2058

21-
-- | Create a `Prism` from a constructor/pattern pair.
59+
-- | Create a `Prism` from a constructor and a "focus" function that
60+
-- | produces an `Either`:
61+
-- |
62+
-- | ```purescript
63+
-- | solidFocus' :: Prism' Fill Color
64+
-- | solidFocus' = prism Solid case _ of
65+
-- | Solid color -> Right color
66+
-- | anotherCase -> Left anotherCase
67+
-- | ```
2268
prism :: forall s t a b. (b -> t) -> (s -> Either t a) -> Prism s t a b
2369
prism to fro pab = dimap fro (either id id) (right (rmap to pab))
2470

71+
-- | Create a `Prism` from a constructor and a "focus" function that
72+
-- | produces an `Maybe`:
73+
-- |
74+
-- | ```purescript
75+
-- | solidFocus' :: Prism' Fill Color
76+
-- | solidFocus' = prism' Solid case _ of
77+
-- | Solid color -> Just color
78+
-- | _ -> Nothing
79+
-- | ```
2580
prism' :: forall s a. (a -> s) -> (s -> Maybe a) -> Prism' s a
2681
prism' to fro = prism to (\s -> maybe (Left s) Right (fro s))
2782

28-
-- | Review a value through a `Prism`.
29-
review :: forall s t a b. Review s t a b -> b -> t
30-
review = under Tagged
83+
-- | Create a prism that focuses on only some of the values of a case,
84+
-- | such as solid colors that are "bright enough":
85+
-- |
86+
-- | ```purescript
87+
-- | brightSolidFocus :: Prism' Fill Unit
88+
-- | brightSolidFocus = nearly (Solid referenceColor) predicate
89+
-- | where
90+
-- | referenceColor = Color.graytone 0.8
91+
-- | predicate = case _ of
92+
-- | Solid color ->
93+
-- | Color.brightness color >= Color.brightness referenceColor
94+
-- | _ ->
95+
-- | false
96+
-- |
97+
-- | preview brightSolidFocus (Solid Color.white) == Just unit
98+
-- | preview brightSolidFocus (Solid Color.black) == Nothing
99+
-- | preview brightSolidFocus NoFill == Nothing
100+
-- |
101+
-- | is brightSolidFocus (Solid Color.white) == true
102+
-- | review brightSolidFocus unit == Color.graytone 0.8
103+
-- | ```
104+
31105

32106
nearly :: forall a. a -> (a -> Boolean) -> Prism' a Unit
33107
nearly x f = prism' (const x) (guard <<< f)
34108

109+
-- | `only` focuses not just on a case, but a specific value of that case.
110+
-- |
111+
-- | ```purescript
112+
-- | solidWhiteFocus :: Prism' Fill Unit
113+
-- | solidWhiteFocus = only $ Solid Color.white
114+
-- |
115+
-- | is solidWhiteFocus (Solid Color.white) == true
116+
-- | preview solidWhiteFocus (Solid Color.white) == Just unit
117+
-- | review solidWhiteFocus unit == Solid Color.white
118+
-- | ```
35119
only :: forall a. Eq a => a -> Prism a a Unit Unit
36120
only x = nearly x (_ == x)
37121

122+
123+
-- | Create the "whole" corresponding to a specific "part":
124+
-- |
125+
-- | ```purescript
126+
-- | -- solidFocus is a `Prism Fill Color`
127+
-- | review solidFocus Color.white == Solid Color.white
128+
-- | ```
129+
review :: forall s t a b. Review s t a b -> b -> t
130+
review = under Tagged
131+
38132
clonePrism :: forall s t a b. APrism s t a b -> Prism s t a b
39133
clonePrism l = withPrism l \x y p -> prism x y p
40134

@@ -45,8 +139,10 @@ withPrism l f = case l (Market id Right) of
45139
matching :: forall s t a b. APrism s t a b -> s -> Either t a
46140
matching l = withPrism l \_ f -> f
47141

142+
--| Would `preview prism` produce a `Just`?
48143
is :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> r
49144
is l = either (const ff) (const tt) <<< matching l
50145

146+
--| Would `preview prism` produce a `Nothing`?
51147
isn't :: forall s t a b r. HeytingAlgebra r => APrism s t a b -> s -> r
52148
isn't l = not <<< is l

src/Data/Lens/Traversal.purs

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
-- | This module defines functions for working with traversals.
1+
-- | `Traversal` is an optic that focuses on zero or more functor values. An
2+
-- | `Array` would be a typical example:
3+
-- |
4+
-- | ```purescript
5+
-- | over traversed negate [1, 2, 3] == [-1, -2, -3]
6+
-- | preview traversed [1, 2, 3] == Just 1
7+
-- | firstOf traversed [1, 2, 3] == Just 1 -- same as `preview`
8+
-- | lastOf traversed [1, 2, 3] == Just 3
9+
-- | ```
10+
-- |
11+
-- | `view` might surprise you. It assumes that the wrapped values
12+
-- | are a monoid, and `append`s them together:
13+
-- |
14+
-- | ```purescript
15+
-- | view traversed ["D", "a", "w", "n"] == "Dawn"
16+
-- | ```
17+
-- |
18+
-- | Many of the functions you'll use are documented in `Data.Lens.Fold`.
19+
220
module Data.Lens.Traversal
321
( traversed
22+
, element
423
, traverseOf
524
, sequenceOf
625
, failover
726
, elementsOf
827
, itraverseOf
9-
, element
1028
, module ExportTypes
1129
) where
1230

@@ -24,7 +42,12 @@ import Data.Traversable (class Traversable, traverse)
2442
import Data.Tuple (Tuple(..), uncurry)
2543
import Data.Newtype (under, unwrap)
2644

27-
-- | Create a `Traversal` which traverses the elements of a `Traversable` functor.
45+
-- | A `Traversal` for the elements of a `Traversable` functor.
46+
-- |
47+
-- | ```purescript
48+
-- | over traversed negate [1, 2, 3] == [-1,-2,-3]
49+
-- | over traversed negate (Just 3) == Just -3
50+
-- | ```
2851
traversed :: forall t a b. Traversable t => Traversal (t a) (t b) a b
2952
traversed = wander traverse
3053

@@ -37,6 +60,29 @@ traverseOf = under Star
3760

3861
-- | Sequence the foci of a `Traversal`, pulling out an `Applicative` effect.
3962
-- | If you do not need the result, see `sequenceOf_` for `Fold`s.
63+
-- |
64+
-- | `sequenceOf traversed` has the same result as `Data.Traversable.sequence`:
65+
-- |
66+
-- | ```purescript
67+
-- | sequenceOf traversed (Just [1, 2]) == [Just 1, Just 2]
68+
-- | sequence (Just [1, 2]) == [Just 1, Just 2]
69+
-- | ```
70+
-- |
71+
-- | An example with effects:
72+
-- | ```purescript
73+
-- | > array = [random, random]
74+
-- | > :t array
75+
-- | Array (Eff ... Number)
76+
-- |
77+
-- | > effect = sequenceOf traversed array
78+
-- | > :t effect
79+
-- | Eff ... (Array Number)
80+
-- |
81+
-- | > effect >>= logShow
82+
-- | [0.15556037108154985,0.28500369615270515]
83+
-- | unit
84+
-- | ```
85+
4086
sequenceOf
4187
:: forall f s t a
4288
. Applicative f
@@ -56,7 +102,14 @@ failover t f s = case unwrap (t $ Star $ Tuple (Disj true) <<< f) s of
56102
Tuple (Disj true) x -> pure x
57103
Tuple (Disj false) _ -> empty
58104

59-
-- | Affine traversal the `n`-th focus of a `Traversal`.
105+
-- | 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`.
107+
-- |
108+
-- | ```purescript
109+
-- | set (element 2 traversed) 8888 [0, 0, 3] == [0, 0, 8888]
110+
-- | preview (element 2 traversed) [0, 0, 3] == Just 3
111+
-- | ```
112+
60113
element
61114
:: forall p s t a
62115
. Wander p

0 commit comments

Comments
 (0)