Skip to content

Commit

Permalink
feat(basics): add iter::toString (#4489) & perf(logging): reduce pe…
Browse files Browse the repository at this point in the history
…ak memory usage in CDF export (#4490)
  • Loading branch information
eventualbuddha authored Jan 9, 2024
1 parent b0e182d commit a6164eb
Show file tree
Hide file tree
Showing 14 changed files with 516 additions and 442 deletions.
4 changes: 1 addition & 3 deletions docs/exercises/01-iteration/06-json.exercise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,5 @@ function extractNamesFromContactsSolutionInner(): AsyncGenerator<string> {
// in a real application you would want to use the AsyncGenerator directly. Otherwise
// you would be building up a huge string in memory.
async function extractNamesFromContactsSolution(): Promise<string> {
return (await iter(extractNamesFromContactsSolutionInner()).toArray()).join(
''
);
return await iter(extractNamesFromContactsSolutionInner()).toString();
}
4 changes: 2 additions & 2 deletions libs/auth/src/cast_vote_record_hashes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ const expectedCastVoteRecordRootHash =
test('readableFileFromData', async () => {
const readableFile = readableFileFromData('1', 'a');
expect(readableFile.fileName).toEqual('1');
expect((await iter(readableFile.open()).toArray()).join('')).toEqual('a');
expect(await iter(readableFile.open()).toString()).toEqual('a');
expect(await readableFile.computeSha256Hash()).toEqual(sha256('a'));
});

test('readableFileFromDisk', async () => {
fs.writeFileSync(path.join(tempDirectoryPath, '1'), 'a');
const readableFile = readableFileFromDisk(path.join(tempDirectoryPath, '1'));
expect(readableFile.fileName).toEqual('1');
expect((await iter(readableFile.open()).toArray()).join('')).toEqual('a');
expect(await iter(readableFile.open()).toString()).toEqual('a');
expect(await readableFile.computeSha256Hash()).toEqual(sha256('a'));
});

Expand Down
21 changes: 16 additions & 5 deletions libs/basics/src/iterators/async_iterator_plus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,6 @@ test('toMap property', async () => {
);
});

test('toSet', async () => {
expect(await iter([]).async().toSet()).toEqual(new Set());
expect(await iter([1, 2, 3]).async().toSet()).toEqual(new Set([1, 2, 3]));
});

test('toMap from iterable', async () => {
expect(
await iter(integers({ from: 1, through: 10 }))
Expand All @@ -339,6 +334,22 @@ test('toMap from iterable', async () => {
);
});

test('toSet', async () => {
expect(await iter([]).async().toSet()).toEqual(new Set());
expect(await iter([1, 2, 3]).async().toSet()).toEqual(new Set([1, 2, 3]));
});

test('toString', async () => {
expect(await iter([]).async().toString()).toEqual('');
expect(await iter([1, 2, 3]).async().toString()).toEqual('123');
expect(await iter([1, 2, 3]).async().toString(' ')).toEqual('1 2 3');
expect(
await iter([{ toString: (): string => 'hello' }, 'world'])
.async()
.toString(', ')
).toEqual('hello, world');
});

test('first', async () => {
expect(await iter([]).async().first()).toEqual(undefined);
expect(await iter([1]).async().first()).toEqual(1);
Expand Down
4 changes: 4 additions & 0 deletions libs/basics/src/iterators/async_iterator_plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,10 @@ export class AsyncIteratorPlusImpl<T> implements AsyncIteratorPlus<T> {
return set;
}

async toString(separator = ''): Promise<string> {
return (await this.toArray()).join(separator);
}

windows(groupSize: 0): never;
windows(groupSize: 1): AsyncIteratorPlus<[T]>;
windows(groupSize: 2): AsyncIteratorPlus<[T, T]>;
Expand Down
19 changes: 14 additions & 5 deletions libs/basics/src/iterators/iterator_plus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,6 @@ test('toMap property', () => {
);
});

test('toSet', () => {
expect(iter([]).toSet()).toEqual(new Set());
expect(iter([1, 2, 3]).toSet()).toEqual(new Set([1, 2, 3]));
});

test('toMap from iterable', () => {
expect(iter(integers({ from: 1, through: 10 })).toMap((a) => a % 2)).toEqual(
new Map([
Expand All @@ -254,6 +249,20 @@ test('toMap from iterable', () => {
);
});

test('toSet', () => {
expect(iter([]).toSet()).toEqual(new Set());
expect(iter([1, 2, 3]).toSet()).toEqual(new Set([1, 2, 3]));
});

test('toString', () => {
expect(iter([]).toString()).toEqual('');
expect(iter([1, 2, 3]).toString()).toEqual('123');
expect(iter([1, 2, 3]).toString(' ')).toEqual('1 2 3');
expect(
iter([{ toString: (): string => 'hello' }, 'world']).toString(', ')
).toEqual('hello, world');
});

test('first', () => {
expect(iter([]).first()).toEqual(undefined);
expect(iter([1]).first()).toEqual(1);
Expand Down
4 changes: 4 additions & 0 deletions libs/basics/src/iterators/iterator_plus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ export class IteratorPlusImpl<T> implements IteratorPlus<T>, AsyncIterable<T> {
return new Set(this.iterable);
}

toString(separator = ''): string {
return this.toArray().join(separator);
}

windows(groupSize: 0): never;
windows(groupSize: 1): IteratorPlus<[T]>;
windows(groupSize: 2): IteratorPlus<[T, T]>;
Expand Down
2 changes: 1 addition & 1 deletion libs/basics/src/iterators/lines.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ test('lines (async)', async () => {
).toEqual(['abc', 'de']);

const input = createReadStream(__filename);
expect((await lines(input).toArray()).join('\n')).toEqual(
expect(await lines(input).toString('\n')).toEqual(
await readFile(__filename, 'utf8')
);

Expand Down
12 changes: 12 additions & 0 deletions libs/basics/src/iterators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,12 @@ export interface IteratorPlus<T> extends Iterable<T> {
*/
toSet(): Set<T>;

/**
* Returns a string representation of `this` by joining elements with
* `separator`. Consumes the entire contained iterable.
*/
toString(separator?: string): string;

/**
* Throws an error because `groupSize` must be greater than 0.
*/
Expand Down Expand Up @@ -1121,6 +1127,12 @@ export interface AsyncIteratorPlus<T> extends AsyncIterable<T> {
*/
toSet(): Promise<Set<T>>;

/**
* Returns a string representation of `this` by joining elements with
* `separator`. Consumes the entire contained iterable.
*/
toString(separator?: string): Promise<string>;

/**
* Throws an error because `groupSize` must be greater than 0.
*/
Expand Down
Loading

0 comments on commit a6164eb

Please sign in to comment.