From d5c5967e33ad0a6a9371bf24d71de2e4a3a1144f Mon Sep 17 00:00:00 2001 From: Daniel Bradley Date: Thu, 7 May 2020 14:46:35 +0100 Subject: [PATCH] feat(iterables): add pairwise Add new pairwise function and tests. --- src/iterables.ts | 18 ++++++++++++++++++ test/iterable.test.ts | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/iterables.ts b/src/iterables.ts index 34efaa2..ff3eeb0 100644 --- a/src/iterables.ts +++ b/src/iterables.ts @@ -659,6 +659,24 @@ export function* reverse(source: Iterable): Iterable { } } +/** + * Returns an interable of each element in the input sequence and its predecessor, + * with the exception of the first element which is only returned as the predecessor of the second element. + * @param source The input collection + */ +export function* pairwise(source: Iterable): Iterable<[T, T]> { + let prev: T | undefined = undefined + let started = false + for (const item of source) { + if (!started) { + started = true + } else { + yield [prev!, item] + } + prev = item + } +} + /** * Returns the sum of the values in the collection. * @param source The input collection. diff --git a/test/iterable.test.ts b/test/iterable.test.ts index f54f873..af51476 100644 --- a/test/iterable.test.ts +++ b/test/iterable.test.ts @@ -716,6 +716,43 @@ describe('take', () => { }) }) +describe('pairwise', () => { + test('empty', () => { + expect( + pipe(Iterables.init({ count: 0 })) + .then(Iterables.pairwise) + .then(Iterables.toArray).result + ).toEqual([]) + }) + test('single item', () => { + expect( + pipe( + (function* () { + yield 1 + })() + ) + .then(Iterables.pairwise) + .then(Iterables.toArray).result + ).toEqual([]) + }) + test('multiple items', () => { + expect( + pipe( + (function* () { + yield 1 + yield 2 + yield 3 + })() + ) + .then(Iterables.pairwise) + .then(Iterables.toArray).result + ).toEqual([ + [1, 2], + [2, 3], + ]) + }) +}) + describe('length', () => { it('can return zero length', () => { expect(pipe(Iterables.init({ count: 0 })).then(Iterables.length).result).toEqual(0)