Skip to content

Commit 84f4285

Browse files
committed
review and preview for prisms.
1 parent 34da452 commit 84f4285

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/Data/Lens/Internal/Tagged.purs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- | This module defines the `Tagged` profunctor
2+
3+
module Data.Lens.Internal.Tagged where
4+
5+
import Data.Profunctor
6+
import Data.Profunctor.Choice
7+
import Data.Either
8+
9+
newtype Tagged a b = Tagged b
10+
11+
instance taggedProfunctor :: Profunctor Tagged where
12+
dimap _ g (Tagged x) = Tagged (g x)
13+
14+
instance taggedChoice :: Choice Tagged where
15+
left (Tagged x) = Tagged (Left x)
16+
right (Tagged x) = Tagged (Right x)
17+
18+
unTagged :: forall a b. Tagged a b -> b
19+
unTagged (Tagged x) = x

src/Data/Lens/Prism.purs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
-- | This module defines functions for working with lenses.
22

33
module Data.Lens.Prism where
4-
4+
55
import Prelude
6-
6+
77
import Data.Either
8+
import Data.Profunctor.Star
9+
import Data.Const
10+
import Data.Maybe
11+
import Data.Maybe.First
812
import Data.Lens.Types
13+
import Data.Lens.Internal.Tagged
914
import Data.Profunctor (dimap, rmap)
1015
import Data.Profunctor.Choice (left)
1116

1217
-- | Create a `Prism` from a constructor/pattern pair.
1318
prism :: forall s t a b. (b -> t) -> (s -> Either a t) -> Prism s t a b
1419
prism to fro pab = dimap fro (either id id) (left (rmap to pab))
1520

21+
-- | Review a value through a `Prism`.
22+
review :: forall s t a b. Prism s t a b -> b -> t
23+
review p = unTagged <<< p <<< Tagged
24+
25+
-- | Previews the value of a `Prism`, if there is any.
26+
preview :: forall s t a b. Prism s t a b -> s -> Maybe a
27+
preview p = runFirst <<< getConst <<< runStar (p (Star (Const <<< pure)))

0 commit comments

Comments
 (0)