Giter VIP home page Giter VIP logo

Comments (16)

remorhaz avatar remorhaz commented on May 20, 2024 1

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:

  1. Given: start is -2, index at start is 1; end is 3, index at end is 3.
  2. 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.
  3. 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.
  4. 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:

  1. Given: start is 2, index at start is 2; end is 1, index at end is 1.
  2. 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.

remorhaz avatar remorhaz commented on May 20, 2024 1

Just a proposal: Array slice with negative range and positive step and so on?

from json-path-comparison.

gregsdennis avatar gregsdennis commented on May 20, 2024

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.

remorhaz avatar remorhaz commented on May 20, 2024

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.

gregsdennis avatar gregsdennis commented on May 20, 2024

While interesting, you don't really answer the specifics of my question. How is that slice to be interpreted?

from json-path-comparison.

cburgmer avatar cburgmer commented on May 20, 2024

from json-path-comparison.

cburgmer avatar cburgmer commented on May 20, 2024

from json-path-comparison.

remorhaz avatar remorhaz commented on May 20, 2024

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.

remorhaz avatar remorhaz commented on May 20, 2024

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.

cburgmer avatar cburgmer commented on May 20, 2024

from json-path-comparison.

remorhaz avatar remorhaz commented on May 20, 2024

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 another on 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.

cburgmer avatar cburgmer commented on May 20, 2024

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.

gregsdennis avatar gregsdennis commented on May 20, 2024

@remorhaz thank you for the explanation. Separating the concepts of interval vs step is the key here.

from json-path-comparison.

cburgmer avatar cburgmer commented on May 20, 2024

Does PR #31 match your expectation?

from json-path-comparison.

gregsdennis avatar gregsdennis commented on May 20, 2024

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.

cburgmer avatar cburgmer commented on May 20, 2024

Merged.

from json-path-comparison.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.