Skip to content

Commit 20df41e

Browse files
committed
Make Getter compose and define AGetter/cloneGetter
1 parent 3e549ca commit 20df41e

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/Data/Lens.purs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import Data.Lens.Grate (Grate, Grate', zipWithOf, zipFWithOf, collectOf)
2828
import Data.Lens.Lens (ALens, ALens', Lens, Lens', cloneLens, lens, lens', withLens)
2929
import Data.Lens.Prism (APrism, APrism', Prism, Prism', Review, Review', clonePrism, is, isn't, matching, nearly, only, prism, prism', review, withPrism)
3030
import Data.Lens.Traversal (Traversal, Traversal', element, elementsOf, failover, itraverseOf, sequenceOf, traverseOf, traversed)
31-
import Data.Lens.Types (class Wander, ALens, ALens', APrism, APrism', AnIso, AnIso', Fold, Fold', Getter, Getter', IndexedFold, IndexedFold', IndexedGetter, IndexedGetter', IndexedOptic, IndexedOptic', IndexedSetter, IndexedSetter', IndexedTraversal, IndexedTraversal', Iso, Iso', Lens, Lens', Optic, Optic', Prism, Prism', Review, Review', Setter, Setter', Traversal, Traversal', Exchange(..), Forget(..), Indexed(..), Market(..), Re(..), Shop(..), Tagged(..), wander)
31+
import Data.Lens.Types (class Wander, ALens, ALens', APrism, APrism', AnIso, AnIso', Fold, Fold', Getter, Getter', AGetter, AGetter', IndexedFold, IndexedFold', IndexedGetter, IndexedGetter', IndexedOptic, IndexedOptic', IndexedSetter, IndexedSetter', IndexedTraversal, IndexedTraversal', Iso, Iso', Lens, Lens', Optic, Optic', Prism, Prism', Review, Review', Setter, Setter', Traversal, Traversal', Exchange(..), Forget(..), Indexed(..), Market(..), Re(..), Shop(..), Tagged(..), wander)
3232
import Data.Lens.Setter (IndexedSetter, Setter, Setter', Indexed(..), addModifying, addOver, appendModifying, appendOver, assign, assignJust, conjModifying, conjOver, disjModifying, disjOver, divModifying, divOver, iover, modifying, mulModifying, mulOver, over, set, setJust, subModifying, subOver, (%=), (%~), (&&=), (&&~), (*=), (*~), (++=), (++~), (+=), (+~), (-=), (-~), (.=), (.~), (//=), (//~), (<>=), (<>~), (?=), (?~), (||=), (||~))
33-
import Data.Lens.Getter (Fold, Getter, IndexedFold, IndexedGetter, Optic, Indexed(..), iuse, iview, to, takeBoth, use, view, viewOn, (^.))
33+
import Data.Lens.Getter (Fold, Getter, IndexedFold, IndexedGetter, Optic, Indexed(..), iuse, iview, to, takeBoth, use, view, viewOn, (^.), cloneGetter)
3434
import Data.Lens.Fold (Fold, Fold', allOf, andOf, anyOf, elemOf, filtered, findOf, firstOf, foldMapOf, foldOf, folded, foldlOf, foldrOf, has, hasn't, iallOf, ianyOf, ifoldMapOf, ifoldlOf, ifoldrOf, itoListOf, itraverseOf_, lastOf, lengthOf, maximumOf, minimumOf, notElemOf, orOf, preview, previewOn, productOf, replicated, sequenceOf_, sumOf, toListOf, toListOfOn, unfolded, (^..), (^?))
3535
import Data.Lens.Common (_1, _2, _Just, _Left, _Nothing, _Right, first, left, right, second, united)

src/Data/Lens/Getter.purs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,43 @@
22
module Data.Lens.Getter
33
( (^.), viewOn
44
, view, to, takeBoth, use, iview, iuse
5+
, cloneGetter
56
, module Data.Lens.Types
67
) where
78

89
import Prelude
910

1011
import Control.Monad.State.Class (class MonadState, gets)
1112
import Data.Lens.Internal.Forget (Forget(..))
12-
import Data.Lens.Types (Getter, Fold, Optic, IndexedGetter, Indexed(..), IndexedFold)
13+
import Data.Lens.Types (Getter, AGetter, Fold, Optic, IndexedGetter, Indexed(..), IndexedFold)
1314
import Data.Newtype (unwrap)
1415
import Data.Profunctor.Strong ((&&&))
1516
import Data.Tuple (Tuple)
1617

1718
infixl 8 viewOn as ^.
1819

1920
-- | View the focus of a `Getter`.
20-
view :: forall s t a b. Getter s t a b -> s -> a
21+
view :: forall s t a b. AGetter s t a b -> s -> a
2122
view l = unwrap (l (Forget identity))
2223

2324
-- | View the focus of a `Getter` and its index.
2425
iview :: forall i s t a b. IndexedFold (Tuple i a) i s t a b -> s -> Tuple i a
2526
iview l = unwrap (l (Indexed $ Forget identity))
2627

2728
-- | Synonym for `view`, flipped.
28-
viewOn :: forall s t a b. s -> Getter s t a b -> a
29+
viewOn :: forall s t a b. s -> AGetter s t a b -> a
2930
viewOn s l = view l s
3031

3132
-- | Convert a function into a getter.
32-
to :: forall r s t a b. (s -> a) -> Fold r s t a b
33+
to :: forall s t a b. (s -> a) -> Getter s t a b
3334
to f p = Forget (unwrap p <<< f)
3435

36+
cloneGetter :: forall s t a b. AGetter s t a b -> Getter s t a b
37+
cloneGetter g = to (view g)
38+
3539
-- | Combine two getters.
36-
takeBoth :: forall s t a b c d. Getter s t a b -> Getter s t c d -> Getter s t (Tuple a c) (Tuple b d)
37-
takeBoth l r a = cmps (l (Forget identity)) (r (Forget identity))
38-
where
39-
cmps :: Forget a s t -> Forget c s t -> Forget (Tuple a c) s t
40-
cmps (Forget f) (Forget g) = Forget (f &&& g)
40+
takeBoth :: forall s t a b c d. AGetter s t a b -> AGetter s t c d -> Getter s t (Tuple a c) (Tuple b d)
41+
takeBoth l r = to (view l &&& view r)
4142

4243
-- | View the focus of a `Getter` in the state of a monad.
4344
use :: forall s t a b m. MonadState s m => Getter s t a b -> m a

src/Data/Lens/Types.purs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ type AGrate s t a b = Optic (Grating a b) s t a b
119119
type AGrate' s a = AGrate s s a a
120120

121121
-- | A getter.
122-
type Getter s t a b = Fold a s t a b
122+
type Getter s t a b = forall r. Fold r s t a b
123123
type Getter' s a = Getter s s a a
124124

125+
type AGetter s t a b = Fold a s t a b
126+
type AGetter' s a = AGetter s s a a
127+
125128
-- | A setter.
126129
type Setter s t a b = Optic Function s t a b
127130
type Setter' s a = Setter s s a a

test/Main.purs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ bar = prop (SProxy :: SProxy "bar")
3232
barAndFoo :: forall a b r. Getter' { bar :: a, foo :: b | r } (Tuple a b)
3333
barAndFoo = takeBoth bar foo
3434

35+
fooGetter :: forall x. Getter' { foo :: x } x
36+
fooGetter = foo
37+
38+
barGetter :: forall x. Getter' { bar :: x } x
39+
barGetter = bar
40+
41+
-- check we can compose getters
42+
fooBarGetter :: forall x. Getter' { foo :: { bar :: x } } x
43+
fooBarGetter = foo <<< bar
44+
3545
type Foo a = { foo :: Maybe { bar :: Array a } }
3646

3747
doc :: Foo String

0 commit comments

Comments
 (0)