@@ -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
1014import Prelude
1115
1216import 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+ )
1423import Data.Profunctor (dimap )
1524import Data.Profunctor.Strong (first )
1625import Data.Tuple (Tuple (..))
26+ import Data.Newtype (un )
1727
1828lens' :: forall s t a b . (s -> Tuple a (b -> t )) -> Lens s t a b
1929lens' 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
2838cloneLens :: forall s t a b . ALens s t a b -> Lens s t a b
2939cloneLens 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
0 commit comments