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

Exclude the upper bound of range #36283

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Exclude the upper bound of range #36283

wants to merge 2 commits into from

Conversation

geryogam
Copy link
Contributor

@geryogam geryogam commented Oct 9, 2024

Description

We make the stop upper bound of the range(start, stop, step) function exclusive in the example given in the Array.from() page.

Motivation

The built-in Python and Clojure range(start, stop, step) functions use the preferred range convention that includes the lower bound and excludes the upper bound (cf. Edsger Dijkstra’s notes Why Numbering Should Start at Zero for the rationale). The built-in PHP range(start, stop, step) function uses another range convention that includes both the lower bound and upper bound.

Additional details

More complete TypeScript implementations of range(start, stop, step) with the range(stop) overload (the first is not lazy and the second one is lazy).

@geryogam geryogam requested a review from a team as a code owner October 9, 2024 13:03
@geryogam geryogam requested review from Josh-Cena and removed request for a team October 9, 2024 13:03
@github-actions github-actions bot added Content:JS JavaScript docs size/s [PR only] 6-50 LoC changed labels Oct 9, 2024
Copy link
Contributor

github-actions bot commented Oct 9, 2024

Preview URLs

(comment last updated: 2024-10-09 13:10:11)

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@geryogam geryogam changed the title Make range upper bound exclusive Make the range upper bound exclusive Oct 9, 2024
@geryogam geryogam changed the title Make the range upper bound exclusive Exclude the upper bound of range Oct 9, 2024
const range = (start, stop, step) =>
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
Array.from(
{ length: Math.ceil((stop - start) / step - 1) + 1 },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ length: Math.ceil((stop - start) / step - 1) + 1 },
{ length: Math.ceil((stop - start) / step) },

Right? I didn't think too much about it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs size/s [PR only] 6-50 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants