Giter VIP home page Giter VIP logo

Comments (6)

STRd6 avatar STRd6 commented on September 8, 2024 3

Another possibility could be comma operator as an extension to ranges like:

items[0, 2, 3]
[0, 2, 3].map((index) => items[index])

from civet.

edemaine avatar edemaine commented on September 8, 2024

In general, Civet is a bit more restrictive on where it allows the comma operator, to prevent certain ambiguities. We should probably document this, though it's a bit subtle exactly where it's allowed (and is still a work-in-progress).

That said, we could support comma operators in MemberBracketContent by changing PostfixedExpression to PostfixedCommaExpression (which allows for a postfixed expression or a comma operator expression). Do you have an application where this is useful/used, or is it just a JS-compatibility/documentation issue?

I could imagine that it might be useful to define x[a,b] to mean something else. For example, I use x[[a,b]] in a bunch of code (equivalent to x[`${a},${b}`]), and x[a,b] might be reasonable shorthand for this... Leaving room for notation like this might be a reason to not allow comma operator in this context.

from civet.

bbrk24 avatar bbrk24 commented on September 8, 2024

I believe C++ had a breaking change relatively recently that allowed multi-argument indexing, changing a[1, 2] from using the comma operator to calling a 2-argument overload of a::operator[]. You can still force the comma operator with parentheses, i.e. a[(1, 2)].

from civet.

JayXon avatar JayXon commented on September 8, 2024

Do you have an application where this is useful/used, or is it just a JS-compatibility/documentation issue?

I'm only using it in code golf, so it's just an undocumented compatibility issue, especially this incompatibility doesn't bring any benefit.

I think using it as an extension to map the index is quite nice.

from civet.

Steffan153 avatar Steffan153 commented on September 8, 2024

Another idea could be for indexing with length like Ruby's syntax, e.g. a[b,2] = a[b...b+2]

from civet.

edemaine avatar edemaine commented on September 8, 2024

I added to comparison.md a note about comma operator being forbidden in some contexts, including this one, and mentioning the workaround of wrapping in parentheses. If there are others you run into, we can add to the list. So I think the original issue is done.

It sounds like we have several possibilities for the meaning of a[b,c]:

  • Multidimensional matrices:
    • a[[b,c]] (poor man's matrices)
    • a[b][c] (slightly less poor matrices, and generalizes to all sorts of indexing, but questionable whether it's any more concise)
    • If TypeArrays had a specifiable dimensions, a[b*a.dim0+c] would be neat... but I think there are too many unknowns here for a language feature. Should be a library feature.
  • [a[b], a[c]] (disconnected range slice)
  • a[b...b+c] (Ruby's range slice with length)

It'd be interesting to see motivating examples for any of these, either here or in Discord.

I currently like the disconnected range slice, which could be generalized to fun things like a[0..10, -10..] (show beginning and end of string, though doesn't work great if the string is shorter than 10, and a real application might want to put some text like ... in between...).

from civet.

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.