Comments (7)
I think if you have two "commuting" lenses then you can concatenate them and get a lens again.
By orthogonal I mean that the following holds:
lens1(obj) == lens1(@set lens2(obj) = val2))
lens2(obj) == lens2(@set lens1(obj) = val1))
So for instance @optic _.a
and @optic _.b
are orthogonal (unless you have weird overloads), while
@optic _[1:2]
and @optic _[2:3]
are not.
struct ConcatLens{L1,L2}
lens1::L1
lens2::L2
end
function (lens::ConcatLens)(obj)
(lens.lens1(obj), lens.lens2(obj))
end
function set(obj, l::ConcatLens, (val1, val2))
obj1 = set(obj, l.lens1, val1)
set(obj1, l.lens2, val2)
end
function concatenate(lens1, lens2)
ConcatLens(lens1, lens2)
end
There are all kinds of fancy optimizations one could do for instance when concatenating property lenses.
You could also make concatenation of arbitrary optics possible in a similar way, by overloading modify, setall, getall
.
I think it is only a sane operation for optics that are commuting in the sense that
getall(obj, optic1) == getall(setall(obj, optic2, vals2), optic1)
getall(obj, optic2) == getall(setall(obj, optic1, vals1), optic2)
We don't want to check that at runtime, it would be the user's responsibility.
Syntax can be improved later on. But a recurrent request is to do "batch mode" @set
.
from accessors.jl.
Syntax can be improved later on. But a recurrent request is to do #15.
Yeah, indeed this is basically "multi-set", or "multi-optics" more generally. Here, I intentionally avoided talking about any convenience syntax for the operation, because it's pretty hard (possible at all?..) to come up with something general that's shorter than the full form of two lenses.
Another difference that simplifies the potential implementation is that "ConcatLens" semantically doesn't have to perform all set
operations at once. It's explicitly combined from multiple independent lenses, and users should easily understand that these lenses can be applied one by one - requiring that each intermediate state is valid, cf jw3126/Setfield.jl#144 (comment).
There are all kinds of fancy optimizations one could do for instance when concatenating property lenses.
Given the "concat semantic", combining multiple compatible lenses (eg PropertyLens
or IndexLens
), can be considered a performance optimization and implemented later.
I think it is only a sane operation for optics that are commuting
We don't want to check that at runtime, it would be the user's responsibility.
Totally agree!
I may attempt implementing concatlens soon. Is "concat" the right term here? We need a separate function anyway - cannot reuse eg Base.vcat
because lenses can be arbitrary objects/functions.
from accessors.jl.
I may attempt implementing concatlens soon. Is "concat" the right term here?
I think in haskell it is called productLens.
from accessors.jl.
Thanks! I understand the reference to the cartesian product here, but to me seems weird to refer to this operation on lenses as a "product" in practice.
from accessors.jl.
Yup I agree. There is also 'alongside' in Haskell, which does what I would intuitively have called product.
from accessors.jl.
This functionality is now available in AccessorsExtra in two variants:
optic ++ optic ++ ...
or@optics ...
: multivalued optics, supportmodify/getall/setall
@opticβ ...
: lense whose value is a container, tuple/array/...; support all operations.
Two variants may seem redundant, but both appear to be useful. See examples, comments and suggestions are welcome.
from accessors.jl.
Related Issues (20)
- `Base.show` ambiguity
- @reset naming convention HOT 4
- fate of insert and delete after these functions get into Base
- How to @reset when the field is known only by its equivalent symbol. HOT 2
- Only a single function argument can be the optic target HOT 3
- Breaks down on parametric types HOT 8
- Does not work with outer-only constructors HOT 1
- expose API to build lenses HOT 4
- API to query if something is a lens/optic HOT 6
- Still experimental? HOT 4
- get/set all values referred by an optic HOT 39
- A setfield function in functional form HOT 1
- AccessorsExtra.jl HOT 1
- `@set` with slices or broadcasting HOT 5
- Set multiple fields HOT 2
- insert not working for stucts? HOT 6
- Assemble an object from optics HOT 7
- `hasproperty()` analogue for optics HOT 3
- IndexLens and changing the container size HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from accessors.jl.