Skip to content

Commit abb7c59

Browse files
committed
more sensible lenses
1 parent 93c9c82 commit abb7c59

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/Control/Optics/Linear/Internal.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Control.Optics.Linear.Internal
3232
, over, over'
3333
, traverseOf, traverseOf'
3434
, lengthOf
35-
, withIso
35+
, withIso, withLens
3636
, toListOf
3737
-- * Constructing optics
3838
, iso, prism, lens
@@ -83,9 +83,13 @@ assoc = iso Bifunctor.lassoc Bifunctor.rassoc
8383
(.>) :: Optic_ arr a b s t -> Optic_ arr x y a b -> Optic_ arr x y s t
8484
Optical f .> Optical g = Optical (f P.. g)
8585

86-
-- c is the complement (probably)
87-
lens :: (s ->. (c,a)) -> ((c,b) ->. t) -> Lens a b s t
88-
lens sca cbt = Optical $ \f -> dimap sca cbt (second f)
86+
lens :: (s ->. (a, b ->. t)) -> Lens a b s t
87+
lens k = Optical $ \f -> dimap k eval (first f)
88+
where eval :: (b, b ->. t) ->. t
89+
eval (x,g) = g x
90+
91+
withLens :: Optic_ (Linear.Kleisli (OtherFunctor a b)) a b s t -> s ->. (a, b ->. t)
92+
withLens (Optical l) s = runOtherFunctor (Linear.runKleisli (l (Linear.Kleisli (\a -> OtherFunctor (a, id)))) s)
8993

9094
prism :: (b ->. t) -> (s ->. Either t a) -> Prism a b s t
9195
prism b s = Optical $ \f -> dimap s (either id id) (second (rmap b f))
@@ -167,7 +171,10 @@ gets' :: Optic_ (Linear.Kleisli (Const (Top, r))) a b s t -> (a ->. r) -> s ->.
167171
gets' (Optical l) f s = getConst' (Linear.runKleisli (l (Linear.Kleisli (\a -> Const (mempty, f a)))) s)
168172

169173
set' :: Optic_ (Linear.Kleisli (MyFunctor a b)) a b s t -> s ->. b ->. (a, t)
170-
set' (Optical l) = runMyFunctor . Linear.runKleisli (l (Linear.Kleisli (\a -> MyFunctor (\b -> (a,b)))))
174+
set' (Optical l) s = runMyFunctor (Linear.runKleisli (l (Linear.Kleisli (\a -> MyFunctor (\b -> (a,b))))) s)
175+
176+
set'' :: Optic_ (NonLinear.Kleisli (Reader b)) a b s t -> b ->. s -> t
177+
set'' (Optical l) b s = runReader (NonLinear.runKleisli (l (NonLinear.Kleisli (const (Reader id)))) s) b
171178

172179
set :: Optic_ (->) a b s t -> b -> s -> t
173180
set (Optical l) x = l (const x)

src/Data/Profunctor/Linear.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Data.Profunctor.Linear
2222
, Exchange(..)
2323
, Top
2424
, MyFunctor(..), runMyFunctor
25+
, OtherFunctor(..), runOtherFunctor
2526
) where
2627

2728
import qualified Data.Functor.Linear as Data
@@ -141,3 +142,11 @@ instance Control.Functor (MyFunctor a b) where
141142
fmap f (MyFunctor g) = MyFunctor (thing f . g)
142143
where thing :: (c ->. d) ->. (e, c) ->. (e, d)
143144
thing k (x,y) = (x, k y)
145+
146+
newtype OtherFunctor a b t = OtherFunctor (a, b ->. t)
147+
runOtherFunctor :: OtherFunctor a b t ->. (a, b ->. t)
148+
runOtherFunctor (OtherFunctor f) = f
149+
instance Data.Functor (OtherFunctor a b) where
150+
fmap f (OtherFunctor (a,g)) = OtherFunctor (a,f . g)
151+
instance Control.Functor (OtherFunctor a b) where
152+
fmap f (OtherFunctor (a,g)) = OtherFunctor (a,f . g)

0 commit comments

Comments
 (0)