Created
January 14, 2021 21:50
-
-
Save jtmthf/64c238367ed68347ac55a2b5bd77a660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type IterableRange = IterableIterator<number> & { | |
| start: number; | |
| end: number; | |
| step: number; | |
| }; | |
| function range( | |
| startOrEnd: number, | |
| maybeEnd?: number, | |
| maybeStep?: number | |
| ): IterableRange { | |
| const start = maybeEnd == null ? 0 : startOrEnd; | |
| const end = maybeEnd == null ? startOrEnd : maybeEnd; | |
| const step = maybeStep == null ? (end < 0 ? -1 : 1) : maybeStep; | |
| const predicate = | |
| step < 0 | |
| ? (current: number) => current > end | |
| : (current: number) => current < end; | |
| return Object.assign( | |
| (function* () { | |
| let current = start; | |
| while (predicate(current)) { | |
| yield current; | |
| current += step; | |
| } | |
| })(), | |
| { start, end, step } | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment