
Hello, I have a data whcih contain a Maybe value data A = A (_a = Maybe a) I would like to use a lens in order to merge two values with this log replace the value only if it is a (Just a) So My question is is there something more elegant than let myA' = case ma of Nothing -> conf (Just na) -> (a .~ na) myA Thanks for your help Frederic

Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do. On Tue, Dec 20, 2022, 11:13 AM PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
Hello,
I have a data whcih contain a Maybe value
data A = A (_a = Maybe a)
I would like to use a lens in order to merge two values with this log
replace the value only if it is a (Just a)
So My question is
is there something more elegant than
let myA' = case ma of Nothing -> conf (Just na) -> (a .~ na) myA
Thanks for your help
Frederic _______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

What is conf? If you write a small example that actually compiles, this will be easier to think about. On Tue, Dec 20, 2022, 11:25 AM PICCA Frederic-Emmanuel < frederic-emmanuel.picca@synchrotron-soleil.fr> wrote:
----- Le 20 Déc 22, à 17:18, David Feuer david.feuer@gmail.com a écrit :
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
Is it clearer ?

import Control.Lens (makeLenses) data A = A { _a = (Maybe Int) } makeLenses A myA = A Nothing ma = Just 1 newA = case ma of Nothing -> myA (Just _) -> (a .~ ma) myA

On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote:
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about over a (mb <|>) ?

That's nice and clean, but it leaves a thunk inside (since the field is lazy). Is there a strict version of `over` that avoids this? We could write one thus: over' :: ALens s t a b -> (a -> b) -> s -> t over' l f = getSolo . cloneLens l (\old -> Solo $! f old) where getSolo (Solo x) = x On Tue, Dec 20, 2022, 11:33 AM Tom Ellis < tom-lists-haskell-cafe-2017@jaguarpaw.co.uk> wrote:
On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote:
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about
over a (mb <|>)
?
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

over can be only strict for traversals. Consider mapped [1], you cannot turn ordinary `fmap` into strict one. [1]: https://hackage.haskell.org/package/lens-5.2/docs/Control-Lens-Combinators.h... Secondly, don't ask for ALens, if you don't need it. LensLike Solo is enough (and works with traversals "for free"). - Oleg On 20.12.2022 19.43, David Feuer wrote:
That's nice and clean, but it leaves a thunk inside (since the field is lazy). Is there a strict version of `over` that avoids this? We could write one thus:
over' :: ALens s t a b -> (a -> b) -> s -> t over' l f = getSolo . cloneLens l (\old -> Solo $! f old) where getSolo (Solo x) = x
On Tue, Dec 20, 2022, 11:33 AM Tom Ellis
wrote: On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote: > > Please clarify your question. Your code doesn't compile, and I can't tell > > what it's supposed to do. > > replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about
over a (mb <|>)
?
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Ah, yes, I should've thought of LensLike. I know it only works for
traversals, but why doesn't it exist in Control.Lens.Traversal?
On Tue, Dec 20, 2022, 2:30 PM Oleg Grenrus
over can be only strict for traversals. Consider mapped [1], you cannot turn ordinary `fmap` into strict one.
[1]: https://hackage.haskell.org/package/lens-5.2/docs/Control-Lens-Combinators.h...
Secondly, don't ask for ALens, if you don't need it. LensLike Solo is enough (and works with traversals "for free").
- Oleg On 20.12.2022 19.43, David Feuer wrote:
That's nice and clean, but it leaves a thunk inside (since the field is lazy). Is there a strict version of `over` that avoids this? We could write one thus:
over' :: ALens s t a b -> (a -> b) -> s -> t over' l f = getSolo . cloneLens l (\old -> Solo $! f old) where getSolo (Solo x) = x
On Tue, Dec 20, 2022, 11:33 AM Tom Ellis < tom-lists-haskell-cafe-2017@jaguarpaw.co.uk> wrote:
On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote:
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about
over a (mb <|>)
?
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to:http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

What's the principle for choosing between LensLike Solo and ATraversal?
On Tue, Dec 20, 2022, 2:30 PM Oleg Grenrus
over can be only strict for traversals. Consider mapped [1], you cannot turn ordinary `fmap` into strict one.
[1]: https://hackage.haskell.org/package/lens-5.2/docs/Control-Lens-Combinators.h...
Secondly, don't ask for ALens, if you don't need it. LensLike Solo is enough (and works with traversals "for free").
- Oleg On 20.12.2022 19.43, David Feuer wrote:
That's nice and clean, but it leaves a thunk inside (since the field is lazy). Is there a strict version of `over` that avoids this? We could write one thus:
over' :: ALens s t a b -> (a -> b) -> s -> t over' l f = getSolo . cloneLens l (\old -> Solo $! f old) where getSolo (Solo x) = x
On Tue, Dec 20, 2022, 11:33 AM Tom Ellis < tom-lists-haskell-cafe-2017@jaguarpaw.co.uk> wrote:
On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote:
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about
over a (mb <|>)
?
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to:http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Actually, a lens is overkill. Any traversal will do.
On Tue, Dec 20, 2022, 12:43 PM David Feuer
That's nice and clean, but it leaves a thunk inside (since the field is lazy). Is there a strict version of `over` that avoids this? We could write one thus:
over' :: ALens s t a b -> (a -> b) -> s -> t over' l f = getSolo . cloneLens l (\old -> Solo $! f old) where getSolo (Solo x) = x
On Tue, Dec 20, 2022, 11:33 AM Tom Ellis < tom-lists-haskell-cafe-2017@jaguarpaw.co.uk> wrote:
On Tue, Dec 20, 2022 at 05:25:35PM +0100, PICCA Frederic-Emmanuel wrote:
Please clarify your question. Your code doesn't compile, and I can't tell what it's supposed to do.
replace a (Maybe a) value in a data using lens only if the new (Maybe a) is a Just
How about
over a (mb <|>)
?
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (4)
-
David Feuer
-
Oleg Grenrus
-
PICCA Frederic-Emmanuel
-
Tom Ellis