Comments (16)
How is that slice to be interpreted?
Well, you can consider iterating slice as: starting at "start" index, adding "step" on each iteration until index is outside of range (something close to "for" control structure in many languages). Note that I've said "index", not "start":
>>> x = [1,2,3]
>>> x[1]
2
>>> x[-2]
2
You can see that both 1
and -2
"start value" ponts to the same array item; there's no difference which one we use:
>>> x[1:3]
[2, 3]
>>> x[-2:3]
[2, 3]
Let's follow the steps of the last example:
- Given: start is -2, index at start is 1; end is 3, index at end is 3.
- We set current index to index at start which is 1; current index is lesser than index at end which is 3, so the value at index 1 is 2.
- We add step (which is 1 by default) to current index, setting it to 2; it is still lesser than 3, so the value at index 2 is 3.
- We add step (which is 1) to current index, setting it to 3; now it is not lessser than 3, so we stop.
Now let's look at your exapmple:
>>> x=['first','second','third','fourth']
>>> x[2:1]
[]
Let's follow the steps:
- Given: start is 2, index at start is 2; end is 1, index at end is 1.
- We set current index to index at start which is 2; it's not lesser that index at end which is 1, so we stop.
So, back to your question:
Is the -1 implied by the fact that the start is greater than the end?
The default step is still 1 for [2:1]
, but interval of the range can be considered as -1 (because 1 - 2 = -1). But I agree that it's not very intuitive.
from json-path-comparison.
Just a proposal: Array slice with negative range and positive step
and so on?
from json-path-comparison.
Secondarily, since the consensus is []
, wouldn't that mean that most implementations interpret this as "if start <= end, return empty"? If the implied index is -1, I'd expect the output to be ["third"]
.
from json-path-comparison.
Goessner mentions ECMASCRIPT 4 proposal for the array slices syntax; ES4 was abandoned, but the proposal itself can be seen here. In fact, the proposal tells us that it's "strikingly similar" to the one implemented in Python; that allows to use Python as a convenient acceptance testing tool for our own slices implementations:
>>> x = [1,2,3]
>>> x[2:1]
[]
from json-path-comparison.
While interesting, you don't really answer the specifics of my question. How is that slice to be interpreted?
from json-path-comparison.
from json-path-comparison.
from json-path-comparison.
I guess you can operate pozitive/negative/zero range
and positive/negative/zero step
and propbably array overflow/underflow
for "stepping" out of array's natural range.
from json-path-comparison.
And I think it would be good to clearly separate the query description from test data. For example:
Query: array slice with negative range and positive step.
Data: array containing whole range.
In this case, you can group similar (not always exactly the same) queries running on different data, thus giving more clear picture of query behavior.
from json-path-comparison.
from json-path-comparison.
Can you see the relevant paragraph in the naming schema description?
Yep, and that will work, of course. Yet I feel that splitting the description into separate descriptions for query and data:
- will work better that seeking for a small
on
in a long and complex description (that can theoretically contain anotheron
for some reason); - will allow to describe each part with more than one sentence.
As I've said, it's just a proposal. On my job I deal a lot with all sorts of unit-like tests and it's always a huge problem to put all the details in standard 3-part (object - pre-condition - behavior).
from json-path-comparison.
Let's separate the two issues.
Feel free to raise a PR for the "interval" issue here.
For a clearer separation of selector and document (the pair I call query) I also welcome a concrete suggestion as issue or PR.
from json-path-comparison.
@remorhaz thank you for the explanation. Separating the concepts of interval vs step is the key here.
from json-path-comparison.
Does PR #31 match your expectation?
from json-path-comparison.
Yeah. My main source of confusion was the "interval" thing. I didn't understand and was reading it as the step. It's all good.
from json-path-comparison.
Merged.
from json-path-comparison.
Related Issues (20)
- Is it possible to provide JSONPath test cases based on consensus results HOT 7
- Show footnote 4 if applicable on the query detail page
- Need test for filter expression checking for local key in array with a null value
- Alignment with spec in its current state (a report) HOT 7
- Incorporating and merging with the compliance test suite HOT 3
- Add nimma
- Include github.com/SteelBridgeLabs/jsonpath HOT 1
- Failing build of Java implementations in the docker container HOT 1
- Add serde_json_path HOT 2
- Add jpt HOT 2
- Bump JsonPath.Net to v1.0.0 HOT 3
- Comparing dotNET_JsonPathLib questionable
- Project governance HOT 7
- Link to reference implementation
- Support for path axis navigation HOT 11
- Issue with display of queries containing the * character HOT 1
- Expand on type of consensus HOT 2
- Tests for root reference in filter expressions. HOT 3
- Analysis HOT 2
- provide key for table contents HOT 8
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 json-path-comparison.