Hi,
I'd like to use optics in a purescript code but I think my question would apply to Haskell.
I have a type data PerformanceAggregate = PerfAgg { cpuSum :: Int, categories :: List PerformanceAggregate}. I added a lens for the cpuSum (_cpuSum) and categories (_categories) field. Obviously I'd like to use an optic to access element in the list, in my case I'm mostly interessed by the first one.
I built an optic _categories . (Lens.ix 0) for this first item in the list. I'm using it with the preview function and it works just fine, returning a Maybe PerformanceAggregate. But updating it is a little more complicated.
What I want is :
if there is an element in the list, I want to update it.
if there is no element in the list, I want to insert one.
I'd like to know if there is a way to have the 2 bullet points with a single function call. I'm not familiar with prism at all, I don't know what is the equivalent of over for instance, and for the second I don't know if it's possible to add an element a List with the composed optics. I currently call Lens.set _categories and use fromMaybe + List.tailie something quite unelegant.
Hi,
I'd like to use optics in a purescript code but I think my question would apply to Haskell.
I have a type
data PerformanceAggregate = PerfAgg { cpuSum :: Int, categories :: List PerformanceAggregate}
. I added a lens for the cpuSum (_cpuSum
) and categories (_categories
) field. Obviously I'd like to use an optic to access element in the list, in my case I'm mostly interessed by the first one.I built an optic
_categories . (Lens.ix 0)
for this first item in the list. I'm using it with the preview function and it works just fine, returning a Maybe PerformanceAggregate. But updating it is a little more complicated.What I want is :
I'd like to know if there is a way to have the 2 bullet points with a single function call. I'm not familiar with prism at all, I don't know what is the equivalent of
over
for instance, and for the second I don't know if it's possible to add an element a List with the composed optics. I currently callLens.set _categories
and usefromMaybe
+List.tail
ie something quite unelegant.it doesn't work with
over
?oh eh, with
at
?you're right, over works, I though it didn't (assuming we needed a preview instead of a view, and a review instead of... a set I guess)
like
_categories . at 0 %~ (f :: Maybe a -> Maybe a)
so
ix
also works?yes
what is the difference between ix and at ?
not sure, I always use
at
. I thinkix
is more flexibleit seems however that
at
is not available in purescriptthere is no lens that will append an object if there is no object in a container ?
ah, right. then the specific semantics are probably not that important here
well my impression was that if you get a
Nothing
inf
and returnJust
, it will appendwill give a try. thanks !
actually there is
at
in purescript too, but not in the module I imported