Skip to content

Commit 3ffb5b1

Browse files
committed
Merge pull request #13 from purescript-contrib/refracting
Add Coproduct prisms, split up Data.Lens.Common
2 parents fecef98 + 989d47f commit 3ffb5b1

File tree

11 files changed

+132
-47
lines changed

11 files changed

+132
-47
lines changed

bower.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
"name": "purescript-profunctor-lenses",
3-
"version": "1.0.0",
4-
"moduleType": [
5-
"node"
6-
],
73
"ignore": [
84
"**/.*",
95
"node_modules",
@@ -12,13 +8,15 @@
128
],
139
"repository": {
1410
"type": "git",
15-
"url": "git://github.com/paf31/purescript-profunctor-lenses.git"
11+
"url": "git://github.com/purescript-contrib/purescript-profunctor-lenses.git"
1612
},
1713
"devDependencies": {
1814
"purescript-console": "^0.1.0"
1915
},
2016
"dependencies": {
2117
"purescript-const": "~0.5.0",
18+
"purescript-coproducts": "~0.4.0",
19+
"purescript-functors": "~0.1.0",
2220
"purescript-identity": "~0.4.0",
2321
"purescript-lists": "~0.7.0",
2422
"purescript-maps": "~0.5.2",

src/Data/Lens/Common.purs

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,15 @@
11
-- | This module defines common lenses and prisms.
22

3-
module Data.Lens.Common where
4-
5-
import Prelude (Unit(), unit, const, ($), (<<<))
6-
7-
import Data.Either (Either(..), either)
8-
import Data.Lens.Internal.Void (Void(), absurd)
9-
import Data.Lens.Lens (Lens(), LensP(), lens)
10-
import Data.Lens.Prism (Prism(), prism)
11-
import Data.Maybe (Maybe(..), maybe)
12-
import Data.Tuple (Tuple(..))
13-
14-
-- | Prism for the `Nothing` constructor of `Maybe`.
15-
_Nothing :: forall a b. Prism (Maybe a) (Maybe b) Unit Unit
16-
_Nothing = prism (const Nothing) $ maybe (Right unit) (const $ Left Nothing)
17-
18-
-- | Prism for the `Just` constructor of `Maybe`.
19-
_Just :: forall a b. Prism (Maybe a) (Maybe b) a b
20-
_Just = prism Just $ maybe (Left Nothing) Right
21-
22-
-- | Prism for the `Left` constructor of `Either`.
23-
_Left :: forall a b c. Prism (Either a c) (Either b c) a b
24-
_Left = prism Left $ either Right (Left <<< Right)
25-
26-
-- | Prism for the `Right` constructor of `Either`.
27-
_Right :: forall a b c. Prism (Either c a) (Either c b) a b
28-
_Right = prism Right $ either (Left <<< Left) Right
29-
30-
-- | Lens for the first component of a `Tuple`.
31-
_1 :: forall a b c. Lens (Tuple a c) (Tuple b c) a b
32-
_1 = lens (\(Tuple a _) -> a) \(Tuple _ c) b -> Tuple b c
33-
34-
-- | Lens for the second component of a `Tuple`.
35-
_2 :: forall a b c. Lens (Tuple c a) (Tuple c b) a b
36-
_2 = lens (\(Tuple _ a) -> a) \(Tuple c _) b -> Tuple c b
37-
38-
-- | There is a `Unit` in everything.
39-
united :: forall a. LensP a Unit
40-
united = lens (const unit) const
41-
42-
-- | There is everything in `Void`.
43-
devoid :: forall a. LensP Void a
44-
devoid = lens absurd const
3+
module Data.Lens.Common
4+
( module Data.Lens.Lens.Tuple
5+
, module Data.Lens.Lens.Unit
6+
, module Data.Lens.Lens.Void
7+
, module Data.Lens.Prism.Either
8+
, module Data.Lens.Prism.Maybe
9+
) where
10+
11+
import Data.Lens.Lens.Tuple
12+
import Data.Lens.Lens.Unit
13+
import Data.Lens.Lens.Void
14+
import Data.Lens.Prism.Either
15+
import Data.Lens.Prism.Maybe

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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Data.Lens.Lens.Product where
2+
3+
import Prelude ((<<<))
4+
5+
import Data.Functor.Product (Product())
6+
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`.
12+
_1 :: forall f g h a. Lens (Product f g a) (Product h g a) (f a) (h a)
13+
_1 = _Product <<< T._1
14+
15+
-- | Lens for the `right` of a `Product`.
16+
_2 :: forall f g h a. Lens (Product f g a) (Product f h a) (g a) (h a)
17+
_2 = _Product <<< T._2

src/Data/Lens/Lens/Tuple.purs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Data.Lens.Lens.Tuple where
2+
3+
import Data.Tuple (Tuple(..))
4+
5+
import Data.Lens.Lens (Lens(), lens)
6+
7+
-- | Lens for the first component of a `Tuple`.
8+
_1 :: forall a b c. Lens (Tuple a c) (Tuple b c) a b
9+
_1 = lens (\(Tuple a _) -> a) \(Tuple _ c) b -> Tuple b c
10+
11+
-- | Lens for the second component of a `Tuple`.
12+
_2 :: forall a b c. Lens (Tuple c a) (Tuple c b) a b
13+
_2 = lens (\(Tuple _ a) -> a) \(Tuple c _) b -> Tuple c b

src/Data/Lens/Lens/Unit.purs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module Data.Lens.Lens.Unit where
2+
3+
import Prelude
4+
5+
import Data.Lens.Lens (LensP(), lens)
6+
7+
-- | There is a `Unit` in everything.
8+
united :: forall a. LensP a Unit
9+
united = lens (const unit) const

src/Data/Lens/Lens/Void.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Data.Lens.Lens.Void where
2+
3+
import Prelude (const)
4+
5+
import Data.Lens.Internal.Void (Void(), absurd)
6+
7+
import Data.Lens.Lens (LensP(), lens)
8+
9+
-- | There is everything in `Void`.
10+
devoid :: forall a. LensP Void a
11+
devoid = lens absurd const

src/Data/Lens/Prism/Coproduct.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module Data.Lens.Prism.Coproduct where
2+
3+
import Prelude ((<<<))
4+
5+
import Data.Functor.Coproduct (Coproduct())
6+
7+
import Data.Lens.Iso.Coproduct (_Coproduct)
8+
import Data.Lens.Prism (Prism())
9+
import Data.Lens.Prism.Either as E
10+
11+
-- | Prism for the `left` of a `Coproduct`.
12+
_Left :: forall f g h a. Prism (Coproduct f g a) (Coproduct h g a) (f a) (h a)
13+
_Left = _Coproduct <<< E._Left
14+
15+
-- | Prism for the `right` of a `Coproduct`.
16+
_Right :: forall f g h a. Prism (Coproduct f g a) (Coproduct f h a) (g a) (h a)
17+
_Right = _Coproduct <<< E._Right

src/Data/Lens/Prism/Either.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Data.Lens.Prism.Either where
2+
3+
import Prelude
4+
5+
import Data.Either (Either(..), either)
6+
7+
import Data.Lens.Prism (Prism(), prism)
8+
9+
-- | Prism for the `Left` constructor of `Either`.
10+
_Left :: forall a b c. Prism (Either a c) (Either b c) a b
11+
_Left = prism Left $ either Right (Left <<< Right)
12+
13+
-- | Prism for the `Right` constructor of `Either`.
14+
_Right :: forall a b c. Prism (Either c a) (Either c b) a b
15+
_Right = prism Right $ either (Left <<< Left) Right

0 commit comments

Comments
 (0)