Skip to content

Commit

Permalink
Add range() to generate sequences for a given number range
Browse files Browse the repository at this point in the history
  • Loading branch information
winterbe committed Oct 17, 2017
1 parent ac3dbb0 commit 915ce56
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,22 @@ Sequency is centered around a single class called `Sequence` to process any kind
Sequences can be created by utilizing one of the following functions:

```js
import {sequenceOf, asSequence, emptySequence, generateSequence} from 'sequency';
import {
asSequence,
sequenceOf,
emptySequence,
range,
generateSequence,
extendSequence
} from 'sequency';
```

- `sequenceOf` accepts one or many values and returns a new sequence.
- `asSequence` accepts an iterable (e.g. an array, set or map) and returns a new sequence.
- `emptySequence` returns a new empty sequence.
- `range` returns as number sequence consisting of all numbers between `startInclusive` and `endExclusive`.
- `generateSequence` returns a sequence generated from the given generator function.
- `extendSequence` allows extending sequences with user-defined operations (see [example](https://github.com/winterbe/sequency/blob/ac3dbb0f212bb08783d970472c7a76dc921b60ba/test/extendSequence.test.ts)).

Each `Sequence` provides a fluent functional API consisting of intermediate and terminal operations. Intermediate functions (e.g. `filter`, `map`, `sorted`) return a new sequence, thus enabling method chaining. Terminal functions (e.g. `toArray`, `groupBy`, `findLast`) return an arbitrary result. Detailed descriptions of all operations are available in the [API docs](https://winterbe.github.io/sequency/).

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sequency",
"version": "0.15.0",
"version": "0.16.0",
"description": "Functional sequences for processing iterable data in JavaScript",
"main": "lib/Sequence.js",
"umd:main": "lib-umd/sequency.js",
Expand Down
19 changes: 19 additions & 0 deletions src/Sequence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,22 @@ export function generateSequence<T>(a: any, b?: any): Sequence<T> {
? createSequence<T>(new GeneratorSeedIterator(seed, b))
: emptySequence<T>();
}

export function range(startInclusive: number, endExclusive: number, step: number = 1): Sequence<number> {
if (startInclusive >= endExclusive) {
throw new Error(`startInclusive [${startInclusive}] must be lower then endExclusive [${endExclusive}]`);
}
if (startInclusive === endExclusive - 1) {
return emptySequence();
}
let current = startInclusive;
return generateSequence(() => {
try {
return current < endExclusive
? current
: undefined;
} finally {
current += step;
}
});
}
22 changes: 22 additions & 0 deletions test/range.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {range} from "../src/Sequence";

describe("range", () => {
it("should create range of numbers with step = 1", () => {
const numbers = range(0, 5).toArray();
expect(numbers).toEqual([0, 1, 2, 3, 4]);
});

it("should create range of numbers with step = .5", () => {
const numbers = range(0, 4, .5).toArray();
expect(numbers).toEqual([0, .5, 1, 1.5, 2, 2.5, 3, 3.5]);
});

it("should create empty sequence", () => {
const numbers = range(0, 1).toArray();
expect(numbers).toEqual([]);
});

it("should throw on invalid boundaries", () => {
expect(() => range(1, 0)).toThrow();
});
});

0 comments on commit 915ce56

Please sign in to comment.