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
When experimenting with parsing and building using Prisms I found a need for "turned-around" Prisms (kind of like re but not becoming uni-directional) and "partial" Isos (conversion to both sides may fail).
I came up with implementations that seem to work nicely, Prisms composed with InvPrisms become PartialIsos, etc:
-- A partial variant of (#) for partial reviewsinfixr8#?(#?)::OpticTaggedMaybestab->b->Maybet
f #? b =Just b &Tagged& f & unTagged
-- A Prism "turned around", i.e a getter but a partial reviewtypeInvPrismstab=forallpf. (Profunctorp, Filterablef) =>Opticpfstab-- A partial iso-morphism, i.e a partial getter and partial reviewtypePartialIsostab=forallpf. (Choicep, Applicativef, Filterablef) =>Opticpfstab-- Turn APrism aroundinvPrism::APrismbats->InvPrismstab
invPrism p =
dimap
(review (reviewing (clonePrism p)))
(mapMaybe (^? getting (clonePrism p)))
-- Create a PartialIso from two partial conversionspartialIso:: (s->Maybea) -> (b->Maybet) ->PartialIsostab
partialIso sma ams =
dimap
(maybe (Left()) Right. sma)
(catMaybes .either (const (pureNothing)) (fmap ams)) .
right'
-- Could this be done by `failing`?failing'::PartialIsostab->Prismstab->Prismstab
failing' i p =
prism
(\b -> fromMaybe (reviewing p # b) (i #? b))
(\s ->maybe (matching p s) Right (s ^? getting i))
failing' is a parsing/building combinator - it first tries the given PartialIso in both parsing and building, and falls back to the given Prism. I called it failing' because to me it seems similar to failing, though trying to see if the latter can be generalised also to fit this use case seems over my head.
Note that InvPrism doesn't carry all of the same power as Prism does, i.e it can't support being an "inverted setter" if such operations existed as it's matching may simply fail and not fall back to type-change the structure.
The text was updated successfully, but these errors were encountered:
When working on early versions of lens we found that the profunctor version of things did lead naturally to 'inverse prisms' and 'inverse lenses', but that the mixed profunctor/functor encoding we wound up using in lens got in the way of flipping. I haven't thought about Filterable as a possible fix.
Sadly it isn't quite as pretty as the version you get out of the profunctor encoding where re and from and invPrism and the operation to turn back from an inverse prism to a prism, (and the equivalents for lenses and inverse lenses) all collapsed into precisely the same from combinator.
When experimenting with parsing and building using
Prism
s I found a need for "turned-around"Prism
s (kind of likere
but not becoming uni-directional) and "partial"Iso
s (conversion to both sides may fail).I came up with implementations that seem to work nicely,
Prism
s composed withInvPrism
s becomePartialIso
s, etc:failing'
is a parsing/building combinator - it first tries the givenPartialIso
in both parsing and building, and falls back to the givenPrism
. I called itfailing'
because to me it seems similar tofailing
, though trying to see if the latter can be generalised also to fit this use case seems over my head.InvPrism
doesn't carry all of the same power asPrism
does, i.e it can't support being an "inverted setter" if such operations existed as it's matching may simply fail and not fall back to type-change the structure.The text was updated successfully, but these errors were encountered: