Skip to content

Some helper functions #150

@pevnak

Description

@pevnak

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.

lenskeys(lens::Setfield.ComposedLens) = vcat(lenskeys(lens.outer), lenskeys(lens.inner))
lenskeys(lens::Setfield.PropertyLens{K}) where {K} = [K]

function normalizelens(lens::Setfield.ComposedLens)
  ls = lenskeys(lens)
  mapfoldr(k -> Setfield.PropertyLens{k}(), ∘, ls)
end

And when I have the lenskeys, I can easily implement indexing as

function Base.getindex(lens::Setfield.ComposedLens, i...)
  ls = lenskeys(lens)
  mapfoldr(k -> Setfield.PropertyLens{k}(), ∘, ls[i...])
end

Base.lastindex(lens::Setfield.ComposedLens) = length(lenskeys(lens))

I 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions