Skip to content

Commit 3d21f77

Browse files
committed
Add ix, at, contains
1 parent 48288c8 commit 3d21f77

File tree

14 files changed

+269
-11
lines changed

14 files changed

+269
-11
lines changed

bower.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020
"dependencies": {
2121
"purescript-const": "~0.5.0",
2222
"purescript-identity": "~0.4.0",
23-
"purescript-profunctor": "~0.3.0",
2423
"purescript-lists": "~0.7.0",
24+
"purescript-maps": "~0.5.2",
25+
"purescript-profunctor": "~0.3.0",
26+
"purescript-sets": "~0.5.1",
2527
"purescript-unsafe-coerce": "~0.1.0"
2628
}
2729
}

docs/Data/Lens/At.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Module Data.Lens.At
2+
3+
#### `At`
4+
5+
``` purescript
6+
class (Index m a b) <= At m a b where
7+
at :: a -> LensP m (Maybe b)
8+
```
9+
10+
##### Instances
11+
``` purescript
12+
instance atIdentity :: At (Identity a) Unit a
13+
instance atMaybe :: At (Maybe a) Unit a
14+
instance atSet :: (Ord v) => At (Set v) v Unit
15+
instance atMap :: (Ord k) => At (Map k v) k v
16+
instance atStrMap :: At (StrMap v) String v
17+
```
18+
19+

docs/Data/Lens/Contains.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## Module Data.Lens.Contains
2+
3+
#### `Contains`
4+
5+
``` purescript
6+
class (IndexKey m a) <= Contains m a where
7+
contains :: a -> LensP m Boolean
8+
```
9+
10+
##### Instances
11+
``` purescript
12+
instance containsSet :: (Ord k) => Contains (Set k) k
13+
```
14+
15+

docs/Data/Lens/Fold.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Filters on a predicate.
218218
#### `replicated`
219219

220220
``` purescript
221-
replicated :: forall r a b t f. (Applicative f, Contravariant f) => Int -> Optic (Star f) a b a t
221+
replicated :: forall a b t f. (Applicative f, Contravariant f) => Int -> Optic (Star f) a b a t
222222
```
223223

224224
Replicates the elements of a fold.

docs/Data/Lens/Index.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## Module Data.Lens.Index
2+
3+
#### `Index`
4+
5+
``` purescript
6+
class (IndexKey m a, IndexValue m b) <= Index m a b where
7+
ix :: a -> TraversalP m b
8+
```
9+
10+
##### Instances
11+
``` purescript
12+
instance indexArr :: (Eq i) => Index (i -> a) i a
13+
instance indexMaybe :: Index (Maybe a) Unit a
14+
instance indexIdentity :: Index (Identity a) Unit a
15+
instance indexArray :: Index (Array a) Int a
16+
instance indexSet :: (Ord a) => Index (Set a) a Unit
17+
instance indexMap :: (Ord k) => Index (Map k v) k v
18+
instance indexStrMap :: Index (StrMap v) String v
19+
```
20+
21+

docs/Data/Lens/Index/Class.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Module Data.Lens.Index.Class
2+
3+
#### `IndexKey`
4+
5+
``` purescript
6+
class IndexKey m k
7+
```
8+
9+
##### Instances
10+
``` purescript
11+
instance indexKeyFn :: IndexKey (a -> b) a
12+
instance indexKeyArray :: IndexKey (Array a) Int
13+
instance indexKeyIdentity :: IndexKey (Identity a) Unit
14+
instance indexKeyMap :: IndexKey (Map k v) k
15+
instance indexKeyMaybe :: IndexKey (Maybe a) Unit
16+
instance indexKeySet :: IndexKey (Set k) k
17+
instance indexKeyStrMap :: IndexKey (StrMap v) String
18+
```
19+
20+
#### `IndexValue`
21+
22+
``` purescript
23+
class IndexValue m v
24+
```
25+
26+
##### Instances
27+
``` purescript
28+
instance indexValueFn :: IndexValue (a -> b) b
29+
instance indexValueArray :: IndexValue (Array a) a
30+
instance indexValueIdentity :: IndexValue (Identity a) a
31+
instance indexValueMap :: IndexValue (Map k v) v
32+
instance indexValueMaybe :: IndexValue (Maybe a) a
33+
instance indexValueSet :: IndexValue (Set k) Unit
34+
instance indexValueStrMap :: IndexValue (StrMap v) v
35+
```
36+
37+

docs/Data/Lens/Internal/Wander.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This module defines the `Wander` type class, which is used to define `Traversal`
66

77
``` purescript
88
class (Strong p, Choice p) <= Wander p where
9-
wander :: forall f s t a b. (forall f. (Applicative f) => (a -> f b) -> s -> f t) -> p a b -> p s t
9+
wander :: forall s t a b. (forall f. (Applicative f) => (a -> f b) -> s -> f t) -> p a b -> p s t
1010
```
1111

1212
Class for profunctors that support polymorphic traversals.

docs/Data/Lens/Setter.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,63 +41,63 @@ Synonym for `set`.
4141
#### `(+~)`
4242

4343
``` purescript
44-
(+~) :: forall s t a a. (Semiring a) => Setter s t a a -> a -> s -> t
44+
(+~) :: forall s t a. (Semiring a) => Setter s t a a -> a -> s -> t
4545
```
4646

4747
_right-associative / precedence 4_
4848

4949
#### `(*~)`
5050

5151
``` purescript
52-
(*~) :: forall s t a a. (Semiring a) => Setter s t a a -> a -> s -> t
52+
(*~) :: forall s t a. (Semiring a) => Setter s t a a -> a -> s -> t
5353
```
5454

5555
_right-associative / precedence 4_
5656

5757
#### `(-~)`
5858

5959
``` purescript
60-
(-~) :: forall s t a a. (Ring a) => Setter s t a a -> a -> s -> t
60+
(-~) :: forall s t a. (Ring a) => Setter s t a a -> a -> s -> t
6161
```
6262

6363
_right-associative / precedence 4_
6464

6565
#### `(//~)`
6666

6767
``` purescript
68-
(//~) :: forall s t a a. (DivisionRing a) => Setter s t a a -> a -> s -> t
68+
(//~) :: forall s t a. (DivisionRing a) => Setter s t a a -> a -> s -> t
6969
```
7070

7171
_right-associative / precedence 4_
7272

7373
#### `(||~)`
7474

7575
``` purescript
76-
(||~) :: forall s t a a. (BooleanAlgebra a) => Setter s t a a -> a -> s -> t
76+
(||~) :: forall s t a. (BooleanAlgebra a) => Setter s t a a -> a -> s -> t
7777
```
7878

7979
_right-associative / precedence 4_
8080

8181
#### `(&&~)`
8282

8383
``` purescript
84-
(&&~) :: forall s t a a. (BooleanAlgebra a) => Setter s t a a -> a -> s -> t
84+
(&&~) :: forall s t a. (BooleanAlgebra a) => Setter s t a a -> a -> s -> t
8585
```
8686

8787
_right-associative / precedence 4_
8888

8989
#### `(<>~)`
9090

9191
``` purescript
92-
(<>~) :: forall s t a a. (Semigroup a) => Setter s t a a -> a -> s -> t
92+
(<>~) :: forall s t a. (Semigroup a) => Setter s t a a -> a -> s -> t
9393
```
9494

9595
_right-associative / precedence 4_
9696

9797
#### `(++~)`
9898

9999
``` purescript
100-
(++~) :: forall s t a a. (Semigroup a) => Setter s t a a -> a -> s -> t
100+
(++~) :: forall s t a. (Semigroup a) => Setter s t a a -> a -> s -> t
101101
```
102102

103103
_right-associative / precedence 4_

docs/Data/Lens/Types.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ type Traversal s t a b = forall p. (Wander p) => Optic p s t a b
102102

103103
A traversal.
104104

105+
#### `TraversalP`
106+
107+
``` purescript
108+
type TraversalP s a = Traversal s s a a
109+
```
110+
105111
#### `Getter`
106112

107113
``` purescript

src/Data/Lens/At.purs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module Data.Lens.At
2+
( At
3+
, at
4+
) where
5+
6+
import Prelude
7+
8+
import Data.Identity (runIdentity, Identity(..))
9+
import Data.Map as M
10+
import Data.Maybe (Maybe(..), maybe)
11+
import Data.Set as S
12+
import Data.StrMap as SM
13+
import Data.Tuple
14+
15+
import Data.Lens (LensP(), lens)
16+
import Data.Lens.Index (Index)
17+
18+
class (Index m a b) <= At m a b where
19+
at :: a -> LensP m (Maybe b)
20+
21+
instance atIdentity :: At (Identity a) Unit a where
22+
at _ = lens (Just <<< runIdentity) (flip maybe Identity)
23+
24+
instance atMaybe :: At (Maybe a) Unit a where
25+
at _ = lens id \_ -> id
26+
27+
instance atSet :: (Ord v) => At (S.Set v) v Unit where
28+
at x = lens get (flip update)
29+
where
30+
get xs =
31+
if S.member x xs
32+
then Just unit
33+
else Nothing
34+
update Nothing = S.delete x
35+
update (Just _) = S.insert x
36+
37+
instance atMap :: (Ord k) => At (M.Map k v) k v where
38+
at k =
39+
lens (M.lookup k) \m ->
40+
maybe (M.delete k m) \v -> M.insert k v m
41+
42+
instance atStrMap :: At (SM.StrMap v) String v where
43+
at k =
44+
lens (SM.lookup k) \m ->
45+
maybe (SM.delete k m) \ v -> SM.insert k v m

0 commit comments

Comments
 (0)