Skip to content

Commit 24e2ec0

Browse files
committed
Added commentary
1 parent d093f3b commit 24e2ec0

File tree

4 files changed

+82
-18
lines changed

4 files changed

+82
-18
lines changed

src/Data/Lens/At.purs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Data.Lens.At
33
, at
44
) where
55

6+
67
import Prelude
78

89
import Data.Identity (Identity(..))
@@ -15,6 +16,25 @@ import Data.Newtype (unwrap)
1516
import Data.Lens (Lens', lens)
1617
import Data.Lens.Index (class Index)
1718

19+
20+
-- | `At` is useful for types like `Data.Map`:
21+
-- |
22+
-- | * The result of getting an element is a `Maybe a`.
23+
-- | * New elements can be added.
24+
-- | * Existing elements can be deleted.
25+
-- |
26+
-- | ```purescript
27+
-- | > optic = at "key"
28+
-- | > view optic $ Map.singleton "key" "value"
29+
-- | (Just "value")
30+
-- |
31+
-- | > set optic (Just "NEW") $ Map.singleton "key" "value"
32+
-- | (fromFoldable [(Tuple "key" "NEW")])
33+
-- |
34+
-- | > set optic Nothing $ Map.singleton "key" "value"
35+
-- | (fromFoldable [])
36+
-- | ```
37+
1838
class Index m a b <= At m a b | m -> a, m -> b where
1939
at :: a -> Lens' m (Maybe b)
2040

src/Data/Lens/Index.purs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ import Data.Set as S
1515
import Data.StrMap as SM
1616
import Data.Traversable (traverse)
1717

18+
-- | `Index` is useful when a focus element might not be present.
19+
-- | It allows you to `set` an existing focus element, but you cannot add a
20+
-- | new one or delete an old one.
21+
-- |
22+
-- | ```purescript
23+
-- |> optic = ix 1
24+
-- |> preview optic [0, 1, 2]
25+
-- |(Just 1)
26+
-- |
27+
-- |> set optic 8888 [0, 1, 2]
28+
-- |[0,8888,2]
29+
-- |
30+
-- |-- Note that `Index` lenses work with many types:
31+
-- |> over optic negate $ Map.singleton 1 8888
32+
-- |(fromFoldable [(Tuple 1 -8888)])
33+
-- | ```
34+
1835
class Index m a b | m -> a, m -> b where
1936
ix :: a -> Traversal' m b
2037

src/Data/Lens/Lens.purs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,24 @@ import Data.Profunctor.Strong (first)
2424
import Data.Tuple (Tuple(..))
2525
import Data.Newtype(un)
2626

27-
lens' :: forall s t a b. (s -> Tuple a (b -> t)) -> Lens s t a b
28-
lens' to pab = dimap to (\(Tuple b f) -> f b) (first pab)
29-
3027
-- | Create a `Lens` from a getter/setter pair.
28+
-- |
29+
-- | ```purescript
30+
-- | > species = lens _.species $ _ {species = _}
31+
-- | > view species {species : "bovine"}
32+
-- | "bovine"
33+
-- |
34+
-- | > _2 = lens Tuple.snd $ \(Tuple keep _) new -> Tuple keep new
35+
-- | ```
36+
-- |
37+
-- | Note: `_2` is predefined in `Data.Lens.Tuple`.
38+
3139
lens :: forall s t a b. (s -> a) -> (s -> b -> t) -> Lens s t a b
3240
lens get set = lens' \s -> Tuple (get s) \b -> set s b
3341

42+
lens' :: forall s t a b. (s -> Tuple a (b -> t)) -> Lens s t a b
43+
lens' to pab = dimap to (\(Tuple b f) -> f b) (first pab)
44+
3445
withLens :: forall s t a b r. ALens s t a b -> ((s -> a) -> (s -> b -> t) -> r) -> r
3546
withLens l f = case l (Shop id \_ b -> b) of Shop x y -> f x y
3647

src/Data/Lens/Types.purs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,45 @@ import Data.Profunctor.Choice (class Choice)
2727
import Data.Profunctor.Closed (class Closed)
2828
import Data.Profunctor.Strong (class Strong)
2929

30-
-- | A general-purpose Data.Lens.
31-
type Optic p s t a b = p a b -> p s t
32-
type Optic' p s a = Optic p s s a a
30+
-- | Given a type whose "focus element" can always be retrieved,
31+
-- | a lens provides a convenient way to view, get, and transform
32+
-- | that element.
33+
-- |
34+
-- | `_2` is a Tuple-specific `Lens` available from `Data.Lens`, so:
35+
-- | ```purescript
36+
-- | > import Data.Lens
37+
-- | > over _2 String.length $ Tuple "ignore" "four"
38+
-- | (Tuple "ignore" 4)
39+
-- | ```
40+
type Lens s t a b = forall p. Strong p => Optic p s t a b
41+
42+
-- | `Lens` allows `set` to change the type of the focus. Often, a
43+
-- | particular lens won't do that. This type alias declares that `set`
44+
-- | only changes values, not types.
45+
type Lens' s a = Lens s s a a
46+
47+
-- | A prism.
48+
type Prism s t a b = forall p. Choice p => Optic p s t a b
49+
type Prism' s a = Prism s s a a
3350

3451
-- | A generalized isomorphism.
3552
type Iso s t a b = forall p. Profunctor p => Optic p s t a b
3653
type Iso' s a = Iso s s a a
3754

55+
-- | A traversal.
56+
type Traversal s t a b = forall p. Wander p => Optic p s t a b
57+
type Traversal' s a = Traversal s s a a
58+
59+
60+
61+
62+
-- | A general-purpose Data.Lens.
63+
type Optic p s t a b = p a b -> p s t
64+
type Optic' p s a = Optic p s s a a
65+
3866
type AnIso s t a b = Optic (Exchange a b) s t a b
3967
type AnIso' s a = AnIso s s a a
4068

41-
-- | A lens.
42-
type Lens s t a b = forall p. Strong p => Optic p s t a b
43-
type Lens' s a = Lens s s a a
44-
4569
type ALens s t a b = Optic (Shop a b) s t a b
4670
type ALens' s a = ALens s s a a
4771

@@ -52,17 +76,9 @@ type IndexedLens' i s a = IndexedLens i s s a a
5276
type AnIndexedLens i s t a b = IndexedOptic (Shop (Tuple i a) b) i s t a b
5377
type AnIndexedLens' i s a = AnIndexedLens i s s a a
5478

55-
-- | A prism.
56-
type Prism s t a b = forall p. Choice p => Optic p s t a b
57-
type Prism' s a = Prism s s a a
58-
5979
type APrism s t a b = Optic (Market a b) s t a b
6080
type APrism' s a = APrism s s a a
6181

62-
-- | A traversal.
63-
type Traversal s t a b = forall p. Wander p => Optic p s t a b
64-
type Traversal' s a = Traversal s s a a
65-
6682
-- | A grate (http://r6research.livejournal.com/28050.html)
6783
type Grate s t a b = forall p. Closed p => Optic p s t a b
6884
type Grate' s a = Grate s s a a

0 commit comments

Comments
 (0)