Skip to content

Commit 989d47f

Browse files
committed
Reformulate Product/Coproduct with Isos
1 parent 6be1b8a commit 989d47f

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

src/Data/Lens/Iso/Coproduct.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Data.Lens.Iso.Coproduct where
2+
3+
import Data.Functor.Coproduct (Coproduct(..), runCoproduct)
4+
import Data.Either (Either())
5+
6+
import Data.Lens.Iso (Iso(), iso)
7+
8+
_Coproduct :: forall f g h i a b. Iso (Coproduct f g a) (Coproduct h i b) (Either (f a) (g a)) (Either (h b) (i b))
9+
_Coproduct = iso runCoproduct Coproduct

src/Data/Lens/Iso/Product.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Data.Lens.Iso.Product where
2+
3+
import Data.Functor.Product (Product(..), runProduct)
4+
import Data.Tuple (Tuple())
5+
6+
import Data.Lens.Iso (Iso(), iso)
7+
8+
_Product :: forall f g h i a b. Iso (Product f g a) (Product h i b) (Tuple (f a) (g a)) (Tuple (h b) (i b))
9+
_Product = iso runProduct Product

src/Data/Lens/Lens/Product.purs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module Data.Lens.Lens.Product where
22

3-
import Data.Functor.Product (Product(..))
4-
import Data.Tuple (Tuple(..))
3+
import Prelude ((<<<))
54

6-
import Data.Lens.Lens (Lens(), lens)
5+
import Data.Functor.Product (Product())
76

8-
-- | Lens for the first component of a `Product`.
7+
import Data.Lens.Iso.Product (_Product)
8+
import Data.Lens.Lens (Lens())
9+
import Data.Lens.Lens.Tuple as T
10+
11+
-- | Lens for the `left` of a `Product`.
912
_1 :: forall f g h a. Lens (Product f g a) (Product h g a) (f a) (h a)
10-
_1 = lens (\(Product (Tuple a _)) -> a) \(Product (Tuple _ c)) b -> Product (Tuple b c)
13+
_1 = _Product <<< T._1
1114

12-
-- | Lens for the second component of a `Product`.
15+
-- | Lens for the `right` of a `Product`.
1316
_2 :: forall f g h a. Lens (Product f g a) (Product f h a) (g a) (h a)
14-
_2 = lens (\(Product (Tuple _ a)) -> a) \(Product (Tuple c _)) b -> Product (Tuple c b)
17+
_2 = _Product <<< T._2

src/Data/Lens/Prism/Coproduct.purs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
module Data.Lens.Prism.Coproduct where
22

3-
import Prelude
3+
import Prelude ((<<<))
44

5-
import Data.Either (Either(..))
6-
import Data.Functor.Coproduct (Coproduct(), coproduct, left, right)
5+
import Data.Functor.Coproduct (Coproduct())
76

8-
import Data.Lens.Prism (Prism(), prism)
7+
import Data.Lens.Iso.Coproduct (_Coproduct)
8+
import Data.Lens.Prism (Prism())
9+
import Data.Lens.Prism.Either as E
910

1011
-- | Prism for the `left` of a `Coproduct`.
1112
_Left :: forall f g h a. Prism (Coproduct f g a) (Coproduct h g a) (f a) (h a)
12-
_Left = prism left $ coproduct Right (Left <<< right)
13+
_Left = _Coproduct <<< E._Left
1314

1415
-- | Prism for the `right` of a `Coproduct`.
1516
_Right :: forall f g h a. Prism (Coproduct f g a) (Coproduct f h a) (g a) (h a)
16-
_Right = prism right $ coproduct (Left <<< left) Right
17+
_Right = _Coproduct <<< E._Right

0 commit comments

Comments
 (0)