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 open interval iterators for numbers #77

Open
rossberg opened this issue Jun 25, 2020 · 7 comments
Open

Add open interval iterators for numbers #77

rossberg opened this issue Jun 25, 2020 · 7 comments

Comments

@rossberg
Copy link
Contributor

Currently, range has type (Nat, Int) -> Iter. The Int was introduced by @chenyan-dfinity to safely support patterns like range(0, array.size()-1).

Instead, we should provide variants of range and revRange that interpret the bounds as an open interval on the max end. We probably shouldn't change range itself, though, since it is too natural to (mis)interpret range(0, 10) as a closed interval, which could lead to bugs if it wasn't.

Any ideas for a nice suggestive naming scheme for open range functions?

@kritzcreek
Copy link
Contributor

Here are the trait names Rust uses for these (they also have Syntax for ranges, but maybe the names are helpful anyway):

1..2;   // std::ops::Range
3..;    // std::ops::RangeFrom
..4;    // std::ops::RangeTo
..;     // std::ops::RangeFull
5..=6;  // std::ops::RangeInclusive
..=7;   // std::ops::RangeToInclusive

@nomeata
Copy link
Contributor

nomeata commented Jun 25, 2020

Haskell says enumFromTo for the inclusive variant; so maybe Nat.iterFromTo and Nat.iterFromUntil? (Not crystal clear)

@crusso
Copy link
Contributor

crusso commented Jun 26, 2020

And where should these live? In each integral type?

@crusso
Copy link
Contributor

crusso commented Jun 26, 2020

Or can we write a generic one that takes a module (perhaps with min an max values) and does the right thing now?

@rossberg
Copy link
Contributor Author

Per @nomeata's earlier suggestion, I think it's appropriate if they live in each integral type's module.

As for naming, I'd keep range as is and add rangeBelow for the exclusive version perhaps? Is that good English?

Not sure we desperately need all the open variants.

@nomeata
Copy link
Contributor

nomeata commented Jun 28, 2020

enumUntil and enumUpTo?

Should it really be range, or do we want a method name that sounds like a verb?

@rossberg
Copy link
Contributor Author

We don't use verbs for other iterators, like keys, vals, entries -- I suppose nouns are established practice because they read more naturally with for ... in X. Probably for the same reason, range is more commonly used, AFAICT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants