Replies: 5 comments
-
I considered it, but I couldn't define an api that I considered idiomatic for Dart. for(final x in RangeInclusive(0,10)) {
}
for(final x in arr(RangeInclusive(0,10))) {
} Didn't sit right with me. While for x in 0..=10 {
}
for x in &arr[0..=10] {
} Is more natural for Rust. for(final x in range(0, 10 + 1)) { // just an example, 11 is obviously preferred
}
for(final x in arr.slice(0, 10 + 1)) {
} I also explored adding inclusive and exclusive to Iterable<int> range(int start, int end, {bool sInc = true, bool eInc = false}) {
if (!sInc) start += 1;
if (eInc) end += 1;
...
} But this doesn't really add anything IMO because setting Open to differing opinions though, and If you have an idea for an api and it flows well with the language, definitely interested in hearing as well! |
Beta Was this translation helpful? Give feedback.
-
After sitting on it for a bit longer, I don't see a reason not to add it. User choice. If you want to work on something I'm for it! |
Beta Was this translation helpful? Give feedback.
-
I had some time today so I ended up implementing it! https://mcmah309.github.io/rust_core/libs/ops/ops.html#rangebounds |
Beta Was this translation helpful? Give feedback.
-
Yeah I like this implementation. I also wish there was some extensions on int to make creating ranges easier, like: import 'range.dart' as r;
extension RangeExtensions on int {
Iterable<int> range(int end) => r.range(this, end);
}
void main() {
for (int index in 0.range(10) {
// ...
}
} I would also be weary of the current implementation of the |
Beta Was this translation helpful? Give feedback.
-
As a matter of opinion, since More often than not, we reach for bounded ranges than unbounded ones, thus that became the default. Which is the same behavior in python. If an unbounded range is desired, one could for(int x in RangeFrom(0)){} // 0, 1, 2, ...
for(int x in RangeFrom(0).iter().stepBy(10).map((e) => -1 * e)){} // 0, -10, -20, ... Maybe something can simplify the second case though, but all the |
Beta Was this translation helpful? Give feedback.
-
With the new addition to the range(int start, int end) function, I was wandering if there are any plans to replicate Rust's Range structs (https://doc.rust-lang.org/std/ops/index.html#structs) to the project, with the options to create different types of Ranges (differentiating between inclusive or exclusive, and bounded or unbounded), and using it on Iterables to create Slices (I know Slice already has a way to do it, but AFAIK only with indexes).
If there isn't any plan, I wouldn't mind doing it myself btw.
Beta Was this translation helpful? Give feedback.
All reactions