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 Range type for representing intervals of values. #190

Merged
merged 1 commit into from
Apr 29, 2018

Conversation

faultyserver
Copy link
Member

This PR introduces the Range type, a generic interface for representing value intervals.

The Range type is particularly useful for working with and enforcing boundaries of acceptable values. It can also be used as a shorthand for creating linear enumerations, though it is not optimized for this case.

From the docs on the Range type itself:

The only values stored by a Range are the lower and upper bounds. The values within the interval are calculated lazily, and only when necessary. With the exceptions of #each and #reverse_each, most methods are implemented using only comparisons between these bounds.

As Myst does not currently provide a literal syntax for Ranges, creating a new Range is done with normal type instantiation, providing the first and last values of the Range as arguments: %Range{10, 20}.

Any value type can be used in a Range so long as it implements the < and <= comparison operators. However, to enable iterating through the Range, value types must also implement a #successor that returns the next element of the interval.

Ranges can also be used in reverse (e.g., with #reverse_each) if the value type defines a #predecessor method returning the previous element of the interval.

Range includes Enumerable, so all of Enumerable's methods can be used directly on Ranges. Where possible, Range provides optimized implementations of Enumerable methods to avoid having to iterate all values in the interval (e.g., #includes?).

The `Range` type is particularly useful for working with and enforcing boundaries of acceptable values. It can also be used as a shorthand for creating linear enumerations, though it is not optimized for this case.
@faultyserver faultyserver merged commit 86bd02a into myst-lang:master Apr 29, 2018
@faultyserver faultyserver deleted the stdlib/range branch April 29, 2018 19:27
@faultyserver faultyserver added this to the Next milestone Apr 29, 2018
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

Successfully merging this pull request may close these issues.

1 participant