Skip to content

Commit ebb8443

Browse files
committed
Merge pull request #11 from jonsterling/ix-and-at
Add ix, at, contains
2 parents 48288c8 + 05b583f commit ebb8443

File tree

10 files changed

+166
-11
lines changed

10 files changed

+166
-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/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 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/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

src/Data/Lens/Index.purs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Data.Lens.Index
2+
( Index
3+
, ix
4+
) where
5+
6+
import Prelude
7+
8+
import Data.Identity (Identity(..))
9+
import Data.Map as M
10+
import Data.Maybe (maybe, fromMaybe, Maybe(..))
11+
import Data.Set as S
12+
import Data.StrMap as SM
13+
import Data.Array as A
14+
15+
import Data.Traversable (traverse)
16+
import Data.Lens.Internal.Wander
17+
import Data.Lens.Types (TraversalP())
18+
19+
class Index m a b where
20+
ix :: a -> TraversalP m b
21+
22+
instance indexArr :: (Eq i) => Index (i -> a) i a where
23+
ix i =
24+
wander \coalg f ->
25+
coalg (f i) <#> \a j ->
26+
if i == j then a else f j
27+
28+
instance indexMaybe :: Index (Maybe a) Unit a where
29+
ix _ = wander traverse
30+
31+
instance indexIdentity :: Index (Identity a) Unit a where
32+
ix _ = wander traverse
33+
34+
instance indexArray :: Index (Array a) Int a where
35+
ix n =
36+
wander \coalg xs ->
37+
xs A.!! n #
38+
maybe
39+
(pure xs)
40+
(coalg >>> map \x -> fromMaybe xs (A.updateAt n x xs))
41+
42+
instance indexSet :: (Ord a) => Index (S.Set a) a Unit where
43+
ix x =
44+
wander \coalg ->
45+
pure <<< S.insert x
46+
47+
instance indexMap :: (Ord k) => Index (M.Map k v) k v where
48+
ix k =
49+
wander \coalg m ->
50+
M.lookup k m #
51+
maybe
52+
(pure m)
53+
(coalg >>> map \v -> M.insert k v m)
54+
55+
instance indexStrMap :: Index (SM.StrMap v) String v where
56+
ix k =
57+
wander \coalg m ->
58+
SM.lookup k m #
59+
maybe
60+
(pure m)
61+
(coalg >>> map \v -> SM.insert k v m)

src/Data/Lens/Types.purs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type APrismP s a = APrism s s a a
4444

4545
-- | A traversal.
4646
type Traversal s t a b = forall p. (Wander p) => Optic p s t a b
47+
type TraversalP s a = Traversal s s a a
4748

4849
-- | A getter.
4950
type Getter s t a b = Fold a s t a b

0 commit comments

Comments
 (0)