Skip to content

Commit ab3727d

Browse files
Marcin Szamotulskipaf31
authored andcommitted
both & takeBoth (#74)
* both & takeBoth * both: combine two `Lens` * takeBoth: combine two `Getters` * Remove both lens.
1 parent 5338cee commit ab3727d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/Data/Lens.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ import Data.Lens.Prism (APrism, APrism', Prism, Prism', Review, Review', clonePr
3030
import Data.Lens.Traversal (Traversal, Traversal', element, elementsOf, failover, itraverseOf, sequenceOf, traverseOf, traversed)
3131
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)
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, use, view, viewOn, (^.))
33+
import Data.Lens.Getter (Fold, Getter, IndexedFold, IndexedGetter, Optic, Indexed(..), iuse, iview, to, takeBoth, use, view, viewOn, (^.))
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
-- | This module defines functions for working with getters.
22
module Data.Lens.Getter
33
( (^.), viewOn
4-
, view, to, use, iview, iuse
4+
, view, to, takeBoth, use, iview, iuse
55
, module Data.Lens.Types
66
) where
77

88
import Prelude
99

1010
import Control.Monad.State.Class (class MonadState, gets)
11-
1211
import Data.Lens.Internal.Forget (Forget(..))
1312
import Data.Lens.Types (Getter, Fold, Optic, IndexedGetter, Indexed(..), IndexedFold)
1413
import Data.Newtype (unwrap)
14+
import Data.Profunctor.Strong ((&&&))
1515
import Data.Tuple (Tuple)
1616

1717
infixl 8 viewOn as ^.
@@ -32,6 +32,13 @@ viewOn s l = view l s
3232
to :: forall r s t a b. (s -> a) -> Fold r s t a b
3333
to f p = Forget (unwrap p <<< f)
3434

35+
-- | 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 id)) (r (Forget id))
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)
41+
3542
-- | View the focus of a `Getter` in the state of a monad.
3643
use :: forall s t a b m. MonadState s m => Getter s t a b -> m a
3744
use p = gets (_ ^. p)

test/Main.purs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
module Test.Main where
22

33
import Prelude
4+
45
import Control.Monad.Eff (Eff)
56
import Control.Monad.Eff.Console (CONSOLE, logShow)
67
import Control.Monad.State (evalState, get)
78
import Data.Distributive (class Distributive)
89
import Data.Either (Either(..))
9-
import Data.Lens (view, traversed, _1, _2, _Just, _Left, lens, collectOf)
10+
import Data.Lens (Getter', _1, _2, _Just, _Left, collectOf, lens, takeBoth, traversed, view)
1011
import Data.Lens.Fold ((^?))
1112
import Data.Lens.Fold.Partial ((^?!), (^@?!))
1213
import Data.Lens.Grate (Grate, cloneGrate, grate, zipWithOf)
@@ -28,6 +29,9 @@ foo = prop (SProxy :: SProxy "foo")
2829
bar :: forall a b r. Lens { bar :: a | r } { bar :: b | r } a b
2930
bar = prop (SProxy :: SProxy "bar")
3031

32+
barAndFoo :: forall a b r. Getter' { bar :: a, foo :: b | r } (Tuple a b)
33+
barAndFoo = takeBoth bar foo
34+
3135
type Foo a = { foo :: Maybe { bar :: Array a } }
3236

3337
doc :: Foo String
@@ -83,6 +87,7 @@ summing = zipWithOf (cloneGrate aGrateExample) (+)
8387
main :: forall e. Eff (console :: CONSOLE | e) Unit
8488
main = do
8589
logShow $ view bars doc
90+
logShow $ view barAndFoo { bar: "bar", foo: "foo" }
8691
logShow $ doc2 ^? _1bars
8792
logShow $ unsafePartial $ doc2 ^?! _1bars
8893
logShow $ unsafePartial $ Tuple 0 1 ^@?! i_2

0 commit comments

Comments
 (0)