Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Python-like slice operator for lists and strings #983

Open
justinfagnani opened this issue Dec 27, 2011 · 16 comments
Open

Add a Python-like slice operator for lists and strings #983

justinfagnani opened this issue Dec 27, 2011 · 16 comments
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). core-m type-enhancement A request for a change that isn't a bug

Comments

@justinfagnani
Copy link
Contributor

String.substring() and List.getRange() are somewhat verbose, but very common operations. Python's slice operator provides an easy-to-read notation to replace these.

Examples of substring() and slice [:]

string.substring(a) -> string[a:]

string.substring(a, b) -> string[a:b]

string.substring(0, a) -> string[:a]

@dgrove
Copy link
Contributor

dgrove commented Jan 2, 2012

Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@DartBot
Copy link

DartBot commented Jan 16, 2012

This comment was originally written by @jinmingjian


great. more my idea is to enhance the slicing operator to an general indexing technology for array like objects, like:

1: a:b = a:1:b = a,a+1,...,b
2: a:2:b = a, a+2,...b
3: a:-1:b = a, a-1,...b
4: [:,a:b] = [1,a:b],[2,a:b]...

@DartBot
Copy link

DartBot commented Jan 18, 2012

This comment was originally written by thebinarysearc...@gmail.com


I would argue that it is not easier to read. substring says what it is doing in English, whereas string[3:2] is symbolism.

@DartBot
Copy link

DartBot commented Jan 18, 2012

This comment was originally written by @jinmingjian


yes or no. Yes, because this is just a syntax magic. No, ":" for slicing is the tradition of numerical computing software[1]. My suggestion is more mathematical. That is, it does not pursue symbolism, it pursues intuition. The ":" is used for generating a vectorized index(i.e.,1D array). And,

  1. the basic form is [start:step:end];
  2. [start:end] is the simplified form of [start:1:end];
  3. : is the simplified form of [head:tail];
  4. generally, start<=end and step>0, but allow start>=end and step<0 makes reversed indexing easier.
  5. get nothing if the index does not exist mathematically.

So,
[3:2] -> [3:1:2], 3 can not reach 2 by step 1 -> nothig. may want [3:-1:2]
[:a] -> not syntax right, may want [0:a]

[1] http://en.wikipedia.org/wiki/Array_slicing

@DartBot
Copy link

DartBot commented Jan 18, 2012

This comment was originally written by @jinmingjian


numerical computing software -> numerical computing language, more exactly.

@DartBot
Copy link

DartBot commented Jan 19, 2012

This comment was originally written by @seaneagan


I think this would require a user-definable operator:

List#getRange becomes:
List<T> operator [:]([int first, int last]);

String#substring becomes:
String operator [:]([int first, int last]);

And it would probably make sense to support slicing then as well, by replacing List#setRange with:

List<T> operator [:]= (List other, [int first, int last]);

List#removeRange also becomes unnecessary, instead of:

list.removeRange(0, 5);

just do:

list[:5] = [];
// or
list[:5] = null;

@floitschG
Copy link
Contributor

Reassigning to language. This requires new syntax and is thus not a library bug.


Removed Area-Library label.
Added Area-Language label.

@floitschG
Copy link
Contributor

Issue #8977 has been merged into this issue.

@Zectbumo
Copy link

Zectbumo commented Aug 1, 2014

Please support negative indexes (relative from the end) as Python does.
At the very least make String.substring() accept negative indexes.

@gbracha
Copy link
Contributor

gbracha commented Aug 28, 2014

Set owner to @gbracha.
Removed Priority-Medium label.
Added Priority-Low, Accepted labels.

@justinfagnani justinfagnani added Type-Enhancement P3 A lower priority bug or feature request area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Aug 28, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed accepted type-enhancement A request for a change that isn't a bug labels Feb 29, 2016
@lrhn lrhn added the core-m label Dec 6, 2018
@lrhn lrhn added this to Non-Breaking and Complex in Language Enhancement Categories Dec 14, 2018
@lrhn lrhn removed the P3 A lower priority bug or feature request label Oct 14, 2019
@deadsoul44
Copy link

It has been 8.5 years. substring method should accept negative values.

@Zectbumo
Copy link

Zectbumo commented Mar 6, 2020

Yeah, this is really weird since even Javascript String.prototype.slice() takes negative values.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice

@pauleohare
Copy link

Any news on this? Is it ever going to happen? Doesn't sound too difficult. Is it?

dart-bot pushed a commit that referenced this issue Jan 25, 2021
2021-01-24 irina.arkhipets@gmail.com Issue #983: Static warning checks corrected.
2021-01-22 irina.arkhipets@gmail.com Issue #983: triple-shift folders re-named correctly, typo in the library paths updated for 2 tests.
2021-01-21 sgrekhov@unipro.ru Fixes #985. LateInitializationError removed
2021-01-20 sgrekhov@unipro.ru Fixes #982. Add tests for The future value type of an asynchronous non-generator function part of NNBD spec
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD type-normalization tests for weak mode added.
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD type-aliases tests for weak mode added.
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD tripple-shift tests for weak mode added.
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD tests for weak mode added for the override checkings.
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD tests for weak mode added (Leatest-greatest-closures).
2021-01-19 irina.arkhipets@gmail.com Issue #462: NNBD tests for weak mode added (Leatest-greatest-closures).
2021-01-18 sgrekhov@unipro.ru Fixes #981. Add tests for Return statements part of NNBD spec
2021-01-18 irina.arkhipets@gmail.com Issue #462: NNBD tests for weak mode corrected according to the current Spec changes.
2021-01-14 irina.arkhipets@gmail.com Issue #462: Additional comments regarding the constant evaluation issues added to weak mode tests.
2021-01-14 sgrekhov@unipro.ru Fixes #980. Remove warnings expectations for overriding default values of optional parameters tests
2021-01-13 irina.arkhipets@gmail.com Issue #462: Tests for weak mode added to nnbd/weak/flow-analysis directory.
2021-01-12 sgrekhov@unipro.ru #978. Weak mode test changes
2021-01-12 irina.arkhipets@gmail.com Issue #462: Tests for weak mode added to nnbd/weak directory.
2021-01-11 sgrekhov@unipro.ru Fixes #978. Tests updated according to the "flow analysis boolean variable" feature
2020-12-25 irina.arkhipets@gmail.com Issues #463: Tests for weak mode added.
2020-12-25 irina.arkhipets@gmail.com Issues #462, #463: Moved or added new nnbd tests for weak mode, added new tests for strong mode.
2020-12-24 sgrekhov@unipro.ru #970. Usr correct values for ProcessSignal.sigusr1 and ProcessSignal.sigusr2 on Mac
2020-12-24 irina.arkhipets@gmail.com Issue #462: 1. Added nnbd tests for weak mode: exports_*, expression_typing_*, extension_method_resolution_*, future_flattening_*. 2. Moved and re-factored nnbd extension_method_resolution_*, future_flattening_* tests for weak mode into weak folder. 3. Copyrights updated Issue #463: Two exports* tests for strong mode added.
2020-12-24 sgrekhov@unipro.ru Fixes #496. Change tested date to 1 second instead of 1 day to reduce probability to change timezone on DST
2020-12-23 sgrekhov@unipro.ru Fixes #473. Remove UtilsHtml directory
2020-12-23 irina.arkhipets@gmail.com Fix for Issue #462: 1. Added nnbd tests for weak mode: assignability_*, const_evaluation_*, const_objects_*, const_type_var_elimination_*. 2. Moved and re-factored nnbd exports_* tests for weak mode into weak folder.
2020-12-10 sgrekhov@unipro.ru Fixes #976. Remove obsolete errors expectations
2020-12-10 sgrekhov@unipro.ru Fixes #975. Fix wrong expected compile time error type
2020-12-07 sgrekhov@unipro.ru Flag Requirements=nnbd-weak added to the tests that run legacy code

Cq-Include-Trybots: dart/try:analyzer-nnbd-linux-release-try,dart2js-nnbd-linux-x64-chrome-try,ddc-nnbd-linux-release-chrome-try,front-end-nnbd-linux-release-x64-try,vm-kernel-nnbd-linux-release-simarm64-try,vm-kernel-nnbd-linux-release-x64-try,vm-kernel-nnbd-mac-release-x64-try,vm-kernel-nnbd-win-release-x64-try,vm-kernel-precomp-nnbd-linux-release-x64-try
Change-Id: I69acc5c0a28fd2e7a2d38f6c9062e5a5854bdfa6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/180823
Reviewed-by: William Hesse <whesse@google.com>
@jabbalaci
Copy link

Any update? Will it be added to the language?

@mraleph
Copy link
Member

mraleph commented May 17, 2021

Any update? Will it be added to the language?

By default the answer to this sort of question is: there is no update and there are no immediate plans to add it to the language.

If there was any sort of update it would be posted here. Language features which are being worked on are part of the language funnel.

@jabbalaci
Copy link

@mraleph Thanks. Then maybe this issue could be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). core-m type-enhancement A request for a change that isn't a bug
Projects
Language Enhancement Categories
Non-Breaking and Complex
Development

No branches or pull requests