Skip to content

Feature/issue 78 random booleans #85

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ Quick Reference
| [`count`](#Count) | Count sequentially forever | `infinite.count([start], [step])` |
| [`cycle`](#Cycle) | Cycle through a collection | `infinite.cycle(iterable)` |
| [`repeat`](#Repeat-1) | Repeat an item forever | `infinite.repeat(item)` |
| [`booleans`](#Booleans) | Generate random booleans | `infinite.booleans([repetitions])` |

#### Math Iteration
| Iterator | Description | Sync Code Snippet | Async Code Snippet |
Expand Down Expand Up @@ -998,6 +999,35 @@ for (const item of infinite.repeat('bla')) {
// bla, bla, bla, bla, bla, ...
```

### Booleans
Generate random boolean values.

```
function* booleans(repetitions?: number): Iterable<boolean>
```

If `repetitions` is provided, generates exactly that many booleans. If not provided, generates booleans infinitely.

```typescript
import { infinite } from 'itertools-ts';

for (const bool of infinite.booleans(5)) {
console.log(bool);
}
// true, false, false, true, false (random values)

for (const bool of infinite.booleans()) {
console.log(bool);
}
// false, false, true, false, true, ... (infinite random values)

// Async version
for await (const bool of infinite.booleansAsync(5)) {
console.log(bool);
}
// true, false, true, false, true (random values)
```

## Math Iteration

### Running Average
Expand Down
12 changes: 12 additions & 0 deletions src/async-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,18 @@ export class AsyncStream<T> implements AsyncIterable<T> {
return new AsyncStream(toAsyncIterable(infinite.repeat(item)));
}

/**
* Creates iterable instance with fluent interface from infinite random boolean values.
*
* @param repetitions (optional) If provided, generates exactly this many booleans.
* If not provided, generates booleans infinitely.
*
* @see infinite.booleansAsync
*/
static booleans(repetitions?: number): AsyncStream<boolean> {
return new AsyncStream(infinite.booleansAsync(repetitions));
}

/**
* Iterate stream collection with another iterable collections simultaneously.
*
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
valuesAsync,
} from "./single";

import { count, cycle, cycleAsync, repeat as infiniteRepeat } from "./infinite";
import { count, cycle, cycleAsync, repeat as infiniteRepeat, booleans, booleansAsync } from "./infinite";

import {
runningAverage,
Expand Down Expand Up @@ -231,6 +231,8 @@ export const infinite = {
cycle,
cycleAsync,
repeat: infiniteRepeat,
booleans,
booleansAsync,
};

export const math = {
Expand Down
18 changes: 18 additions & 0 deletions src/infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,21 @@ export function* repeat<T>(item: T): Iterable<T> {
yield item;
}
}

export function* booleans(repetitions?: number): Iterable<boolean> {
let i = 0;

while (repetitions === undefined || i < repetitions) {
yield Math.random() > 0.5;
i++;
}
}

export async function* booleansAsync(repetitions?: number): AsyncIterable<boolean> {
let i = 0;

while (repetitions === undefined || i < repetitions) {
yield Math.random() > 0.5;
i++;
}
}
12 changes: 12 additions & 0 deletions src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ export class Stream<T> implements Iterable<T> {
return new Stream(infinite.repeat(item));
}

/**
* Creates iterable instance with fluent interface from infinite random boolean values.
*
* @param repetitions (optional) If provided, generates exactly this many booleans.
* If not provided, generates booleans infinitely.
*
* @see infinite.booleans
*/
static booleans(repetitions?: number): Stream<boolean> {
return new Stream(infinite.booleans(repetitions));
}

/**
* Iterate stream collection with another iterable collections simultaneously.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/async-stream/infinite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ describe.each([
}
);

describe.each([
...dataProviderForBooleans(),
])(
"AsyncStream Infinite Booleans Test",
(repetitions: number | undefined, limit: number) => {
it("", async () => {
// When
const stream = AsyncStream.booleans(repetitions);
const result = await stream.limit(limit).toArray();

// Then
expect(result).toHaveLength(limit);
expect(result.every(x => typeof x === 'boolean')).toBe(true);
});
}
);

function dataProviderForOfCount(): Array<[Array<number>, number, Array<number>]> {
return [
[
Expand Down Expand Up @@ -194,3 +211,23 @@ function dataProviderForOfRepeat(): Array<[unknown, number, Array<unknown>]> {
],
];
}

function dataProviderForBooleans(): Array<[number | undefined, number, Array<boolean>]> {
return [
[
undefined,
5,
[true, false, true, false, true],
],
[
10,
10,
[true, false, true, false, true, false, true, false, true, false],
],
[
3,
3,
[true, false, true],
],
];
}
60 changes: 60 additions & 0 deletions tests/infinite/booleans.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expectToBeCloseToArray } from "../fixture";
import { infinite } from '../../src';

describe.each([
...dataProviderForBooleans(),
])(
"Infinite Booleans Test",
(repetitions: number | undefined, limit: number, expected: Array<boolean>) => {
it("", () => {
// When
const generator = infinite.booleans(repetitions);
const result = Array.from({ length: limit }, () => generator[Symbol.iterator]().next().value);

// Then
expect(result).toHaveLength(limit);
expect(result.every(x => typeof x === 'boolean')).toBe(true);
});
}
);

describe.each([
...dataProviderForBooleansAsync(),
])(
"Infinite Booleans Async Test",
(repetitions: number | undefined, limit: number, expected: Array<boolean>) => {
it("", async () => {
// When
const generator = infinite.booleansAsync(repetitions);
const result = await Promise.all(Array.from({ length: limit }, () => generator[Symbol.asyncIterator]().next().then(r => r.value)));

// Then
expect(result).toHaveLength(limit);
expect(result.every(x => typeof x === 'boolean')).toBe(true);
});
}
);

function dataProviderForBooleans(): Array<[number | undefined, number, Array<boolean>]> {
return [
[
undefined,
5,
[true, false, true, false, true],
],
[
10,
10,
[true, false, true, false, true, false, true, false, true, false],
],
[
3,
3,
[true, false, true],
],
];
}

function dataProviderForBooleansAsync(): Array<[number | undefined, number, Array<boolean>]> {
return dataProviderForBooleans();
}
37 changes: 37 additions & 0 deletions tests/stream/infinite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ describe.each([
}
);

describe.each([
...dataProviderForBooleans(),
])(
"Stream Infinite Booleans Test",
(repetitions: number | undefined, limit: number) => {
it("", () => {
// When
const stream = Stream.booleans(repetitions);
const result = stream.limit(limit).toArray();

// Then
expect(result).toHaveLength(limit);
expect(result.every(x => typeof x === 'boolean')).toBe(true);
});
}
);

function dataProviderForOfCount(): Array<[Array<number>, number, Array<number>]> {
return [
[
Expand Down Expand Up @@ -194,3 +211,23 @@ function dataProviderForOfRepeat(): Array<[unknown, number, Array<unknown>]> {
],
];
}

function dataProviderForBooleans(): Array<[number | undefined, number, Array<boolean>]> {
return [
[
undefined,
5,
[true, false, true, false, true],
],
[
10,
10,
[true, false, true, false, true, false, true, false, true, false],
],
[
3,
3,
[true, false, true],
],
];
}