Open pevnak opened 3 years ago
Hi @pevnak glad you are using Setfield.jl. We can include some form of this functionality, but I have some objections to the exact proposal.
What I am happy to include is some helper like this:
using Accessors
import Accessors: ComposedOptic
decompose(optic) = (optic,)
decompose(optic::ComposedOptic) = (decompose(optic.outer)..., decompose(optic.inner)...)
A PR to Accessors.jl would be welcome!
You could then do the indexing like ∘(decompose(@optic(_.a.b.c))[[1,3]]...)
. Note that this is also much more general and not restricted to PropertyLens
.
Actually I think the decompose function can even go to CompositionsBase.jl.
I like the decompose
a lot. It is nicer and cleaner over what I have written. The indexing is also very nice.
To which part you refer as not being type stable? The normalization was mean to ensure that the outer part of ComposedLens
is simple, which I think is not against the type stability. In my ideal view, the output of a composition would be a normalized Lens, but that might not be compatible some people use cases.
I can create these PRs with Tests to Accessors.jl. What should be the part of it?
You can add decompose
to [CompositionsBase.jl](https://github.com/JuliaFunctional/CompositionsBase.jl)
. That way it is useful to even more people. The direction Lens -> Vector{Symbol}
is type stable. But there is no type stable way back.
You can also add to some helper like propname(::PropertyLens{name}) where {name} = name
to Accessors.jl. So lenskeys
becomes map(propname, decompose(lens))
.
It would also be nice to have normalization
in Accessors.jl
. Note that the composition order influences performance, so normalization should give the fast order.
I would prefer not to have indexing. I think it's usefulness is in a too limited situation to justify haveing it in Accessors
.
This is crystal clear answer.
I will work on relevant PRs.
Dear All,
I am using setfield for indexing in terribly complicated tree structures naturally present in tree data. Since I frequently need programmatically adapt lenses, it helps a lot, if they are in normalized form. I have therefore made a few functions, which simplifies my life a lot and I wonder, if that would be something you would consider interesting.
The first function "normalizes" lenses, such that outerlens in CompoundLens is simple.
And when I have the
lenskeys
, I can easily implement indexing asI would like to know your opinion and if they would fit to the library. If so, I would prepare a full PR.
Thanks a lot, Tomas