Skip to content

Commit 772540c

Browse files
Roxxikpaf31
authored andcommitted
Indexed lenses (#50)
* add IndexedLens * Fixed errors * renamed variables, to match up lens and ilens
1 parent 8127f59 commit 772540c

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- | This module defines the `IndexedShop` profunctor
2+
module Data.Lens.Internal.IndexedShop where
3+
4+
import Prelude
5+
6+
import Data.Profunctor (class Profunctor)
7+
import Data.Profunctor.Strong (class Strong)
8+
import Data.Tuple (Tuple(..))
9+
10+
-- | The `IndexedShop` profunctor characterizes an `IndexedLens`.
11+
data IndexedShop i a b s t = IndexedShop (s -> (Tuple i a)) (s -> b -> t)
12+
13+
instance profunctorIndexedShop :: Profunctor (IndexedShop i a b) where
14+
dimap f g (IndexedShop x y) = IndexedShop (x <<< f) (\s -> g <<< y (f s))
15+
16+
instance strongShop :: Strong (IndexedShop i a b) where
17+
first (IndexedShop x y) =
18+
IndexedShop (\(Tuple a _) -> x a) (\(Tuple s c) b -> Tuple (y s b) c)
19+
second (IndexedShop x y) =
20+
IndexedShop (\(Tuple _ a) -> x a) (\(Tuple c s) b -> Tuple c (y s b))

src/Data/Lens/Lens.purs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@ module Data.Lens.Lens
44
, lens'
55
, withLens
66
, cloneLens
7+
, ilens
8+
, ilens'
9+
, withIndexedLens
10+
, cloneIndexedLens
711
, module Data.Lens.Types
812
) where
913

1014
import Prelude
1115

1216
import Data.Lens.Internal.Shop (Shop(..))
13-
import Data.Lens.Types (Lens, Lens', ALens, ALens')
17+
import Data.Lens.Internal.IndexedShop (IndexedShop(..))
18+
import Data.Lens.Internal.Indexed (Indexed(..))
19+
import Data.Lens.Types
20+
( Lens, Lens', ALens, ALens'
21+
, IndexedLens, IndexedLens', AnIndexedLens, AnIndexedLens'
22+
)
1423
import Data.Profunctor (dimap)
1524
import Data.Profunctor.Strong (first)
1625
import Data.Tuple (Tuple(..))
26+
import Data.Newtype(un)
1727

1828
lens' :: forall s t a b. (s -> Tuple a (b -> t)) -> Lens s t a b
1929
lens' to pab = dimap to (\(Tuple b f) -> f b) (first pab)
@@ -27,3 +37,20 @@ withLens l f = case l (Shop id \_ b -> b) of Shop x y -> f x y
2737

2838
cloneLens :: forall s t a b. ALens s t a b -> Lens s t a b
2939
cloneLens l = withLens l \x y p -> lens x y p
40+
41+
42+
ilens' :: forall i s t a b.
43+
(s -> Tuple (Tuple i a) (b -> t)) -> IndexedLens i s t a b
44+
ilens' to pab = dimap to (\(Tuple b f) -> f b) (first ((un Indexed) pab))
45+
46+
-- create an `IndexedLens` from a getter/setter pair.
47+
ilens :: forall i s t a b.
48+
(s -> Tuple i a) -> (s -> b -> t) -> IndexedLens i s t a b
49+
ilens get set = ilens' \s -> Tuple (get s) \b -> set s b
50+
51+
withIndexedLens :: forall i s t a b r.
52+
(AnIndexedLens i s t a b) -> ((s -> (Tuple i a)) -> (s -> b -> t) -> r) -> r
53+
withIndexedLens l f = case l (Indexed (IndexedShop id \_ b -> b)) of IndexedShop x y -> f x y
54+
55+
cloneIndexedLens :: forall i s t a b. AnIndexedLens i s t a b -> IndexedLens i s t a b
56+
cloneIndexedLens l = withIndexedLens l \x y p -> ilens x y p

src/Data/Lens/Types.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module Data.Lens.Types
44
, module Data.Lens.Internal.Exchange
55
, module Data.Lens.Internal.Market
66
, module Data.Lens.Internal.Shop
7+
, module Data.Lens.Internal.IndexedShop
78
, module Data.Lens.Internal.Tagged
89
, module Data.Lens.Internal.Forget
910
, module Data.Lens.Internal.Wander
@@ -17,6 +18,7 @@ import Data.Lens.Internal.Indexed (Indexed(..))
1718
import Data.Lens.Internal.Market (Market(..))
1819
import Data.Lens.Internal.Re (Re(..))
1920
import Data.Lens.Internal.Shop (Shop(..))
21+
import Data.Lens.Internal.IndexedShop (IndexedShop(..))
2022
import Data.Lens.Internal.Tagged (Tagged(..))
2123
import Data.Lens.Internal.Wander (class Wander, wander)
2224
import Data.Profunctor (class Profunctor)
@@ -41,6 +43,13 @@ type Lens' s a = Lens s s a a
4143
type ALens s t a b = Optic (Shop a b) s t a b
4244
type ALens' s a = ALens s s a a
4345

46+
-- | An indexed lens.
47+
type IndexedLens i s t a b = forall p. Strong p => IndexedOptic p i s t a b
48+
type IndexedLens' i s a = IndexedLens i s s a a
49+
50+
type AnIndexedLens i s t a b = IndexedOptic (IndexedShop i a b) i s t a b
51+
type AnIndexedLens' i s a = AnIndexedLens i s s a a
52+
4453
-- | A prism.
4554
type Prism s t a b = forall p. Choice p => Optic p s t a b
4655
type Prism' s a = Prism s s a a

0 commit comments

Comments
 (0)