You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking for a function similar to lensStore but where some constructors of the ADT did not contain the inner value. After a day of learning lenses I came up with the following:
affineStore
::forallstt'ab
. (t'->t)
->ALensst'ab->s->Tuple (b->t) (Eitherta)
affineStore f l = withLens l go
where
go get set value =
Tuple
(f <<< set value)
(Right $ get value)
ignoredAffineStore::foralltab. t->Tuple (b->t) (Eitherta)
ignoredAffineStore wrapper = Tuple (const wrapper) (Left wrapper)
dataAdt=X{x::Int, y::String}
| Y{a::Int, b::Number}
| Z{e::Char}_x::forallsr. Lens' { x::s | r } s
_x = prop (Proxy::_ "x")
_a::forallsr. Lens' { a::s | r } s
_a = prop (Proxy::_ "a")
_valueInAdt::AffineTraversal'AdtInt
_valueInAdt = affineTraversal' case _ ofX value -> affineStore X _x value
Y value -> affineStore Y _a value
adt@(Z _) -> ignoredAffineStore adt
Here the functions affineStore and ignoredAffineStore can be used in a similar way to lensStore. I have checked that it all follows affine traversal laws, I can send over the tests.
My question is whether these are useful functions to have in the lenses package. It is very useful for what I'm building and it seems there is no other way of doing it this comfortably.
The text was updated successfully, but these errors were encountered:
After developing a affineStore' function accepting prisms/affine traversals, I found out that an affine traversal is a superset of a lens. So here is the more general version:
affineStore
::forallstab
. (s->t)
-> (AnAffineTraversalssab)
->s->Tuple (b->t) (Eitherta)
affineStore f l = withAffineTraversal l go
where
go setl previewl value =
Tuple
(f <<< setl value)
(lmap f $ previewl value)
dataAdt=X{x::Int, y::String}
| Y{a::MaybeInt, b::Number}
| Z{e::Char}
| W (MaybeInt)
derive instancegenericAdt :: GenericAdt_derive instanceeqAdt :: EqAdtinstanceshowAdt :: ShowAdtwhere show = genericShow
_x::forallsr. Lens' { x::s | r } s
_x = prop (Proxy::_ "x")
_a::forallsr. AffineTraversal' { a::Maybes | r } s
_a = prop (Proxy::_ "a") <<< _Just
_valueInAdt::AffineTraversal'AdtInt
_valueInAdt = affineTraversal' case _ ofX value -> affineStore X _x value
Y value -> affineStore Y _a value
W value -> affineStore W _Just value
adt@(Z _) -> ignoredAffineStore adt
I was looking for a function similar to
lensStore
but where some constructors of the ADT did not contain the inner value. After a day of learning lenses I came up with the following:Here the functions
affineStore
andignoredAffineStore
can be used in a similar way tolensStore
. I have checked that it all follows affine traversal laws, I can send over the tests.My question is whether these are useful functions to have in the lenses package. It is very useful for what I'm building and it seems there is no other way of doing it this comfortably.
The text was updated successfully, but these errors were encountered: