Comments (8)
Yes, currently Partial Lenses do not support symbols and in non-production mode the library raises an exception as seen here.
I'm not opposed to supporting symbols, but I'm not entirely sure what the best way to support them would be. Ramda, for example, does not currently support symbols. It seems that the use case of symbols is to avoid name clashes. Even JavaScript itself seems rather undecided on how symbol properties should be treated: object spread ignores them, while Object spread and Object.assign
copies them as seen here.Object.assign
copy symbols as seen here.
I did some quick tests and it seems that copying symbol properties (using builtin Object.assign
or Object.getOwnPropertySymbols
) would cause something like a 2x performance hit in latest Node for property lenses. Note that I mean this in the sense of supporting the possibility of having symbol properties—I actually tested this on objects that did not have symbol properties, so there was no extra cost of actually copying more. Hopefully future JS engines will have faster implementations of Object.assign
and then supporting symbol properties could be done without taking a performance hit.
For the above two reasons, namely
- that there currently does not seem to be a consensus on what the best way to support them is (copy or not copy), and
- that copying them (it seems) could could cause a 2x performance hit,
I'd rather not enhance the default property lenses in Partial Lenses to support symbol properties at this point. The above concerns may change in the future (IOW, a consensus may be reached and copying symbol properties might get significantly faster) at which point this could be reconsidered.
Note that you can always write custom optics for accessing objects with symbol properties. Here is a simple example using L.lens
and here is a version that also provides the symbol or string as the index (Oops: Fixed!). At the moment this is what I'd recommend if you need to access objects with symbol properties.
Also, it might be useful to add the above sym
lens to the library. Possibly with a better name? Name suggestions and/or PR is welcome!
from partial.lenses.
Ah... Good question. xi2yF
spells out the signature of the parameter: it is a function that maps the value under scrutiny x
and its index i
to the resulting value y
in functor F
.
from partial.lenses.
I read your example. However, the variable name like xi2yF confuse me. What it's name mean? When I want to read this project source code. This problem tangle me, I want to contribute my code, but first , how to understand this, which knowledge I miss. Thanks
from partial.lenses.
Also, my latter example had a bug—I just fixed the link.
from partial.lenses.
Got it ,thanks
from partial.lenses.
@polytypic
Hi, I think the object spread demo should be {...x, y: 1}
rather than {...foo, y: 1}
.
In this case, the behavior is same to Object.assign
from partial.lenses.
@Justin-ZS Oops. Thanks for the correction! Here is the fixed playground.
from partial.lenses.
If someone, like me, needs to not loose Symbols using lenses this a patch I have done to infestines.
diff --git a/node_modules/infestines/dist/infestines.cjs.js b/node_modules/infestines/dist/infestines.cjs.js
index 3d711b4..a1a4f30 100644
--- a/node_modules/infestines/dist/infestines.cjs.js
+++ b/node_modules/infestines/dist/infestines.cjs.js
@@ -438,14 +438,8 @@ var assocPartialU = function assocPartial(k, v, o) {
var r = {};
if (o instanceof Object) {
if (!isObject(o)) o = toObject(o);
- for (var l in o) {
- if (l !== k) {
- r[l] = o[l];
- } else {
- r[k] = v;
- k = void 0;
- }
- }
+ // Symbol Support
+ Object.assign(r, o);
}
if (isDefined(k)) r[k] = v;
return r;
@@ -456,10 +450,9 @@ var dissocPartialU = function dissocPartial(k, o) {
if (o instanceof Object) {
if (!isObject(o)) o = toObject(o);
for (var l in o) {
- if (l !== k) {
- if (!r) r = {};
- r[l] = o[l];
- } else {
+ // Symbol Support
+ if (!r) {r = {...o}}
+ if (l === k) {
k = void 0;
}
}
from partial.lenses.
Related Issues (20)
- Error with react native HOT 3
- Add Node compatible `L.querystring` isomorphism
- Consider adding more parsing + stringifying combinators or a separate library
- Consider adding support for Maps and Sets
- Consider adding support for splitting an object into disjoint subsets of keys HOT 1
- Change `L.removable` Take a Predicate? HOT 3
- Consider adding `disperse` operation and `partsOf` lens constructor
- Isomorphism in L.array called with undefined HOT 2
- Index isomorphism HOT 5
- Great Work 👍 Thank you (No issue) HOT 5
- Dealing with exceptions?
- I'd like to L.traverse an object HOT 4
- Usage with JSON Patching? HOT 3
- Add example exercise answers to wiki? HOT 4
- Nested matches for L.satisfying HOT 2
- Branches Does not preserve order HOT 2
- Modifying a nested data structure? HOT 12
- `L.get` has unexpected behavior HOT 2
- Does not works with Modules object
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 partial.lenses.