Skip to content

Commit

Permalink
refactor: replace take to spread syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Baek2back committed Jul 31, 2024
1 parent 229b831 commit 6bcc375
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 85 deletions.
9 changes: 4 additions & 5 deletions packages/python/src/built-in/zip.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools/take";
import { zip } from "./zip";

describe("zip", () => {
it("zips an iterable correctly", () => {
const result = take(Number.POSITIVE_INFINITY, zip([1, 2], [3, 4]));
const result = [...zip([1, 2], [3, 4])];
expect(result).toEqual([
[1, 3],
[2, 4],
]);
});

it("return an empty iterables when pass empty iterables", () => {
const result = take(Number.POSITIVE_INFINITY, zip([], []));
const result = [...zip([], [])];
expect(result).toEqual([]);
});

it("throw Error pass different length iterables when strict option is true", () => {
expect(() => {
take(Number.POSITIVE_INFINITY, zip([1, 2], [1, 2, 3], { strict: true }));
[...zip([1, 2], [1, 2, 3], { strict: true })];
}).toThrow("zip() argument 2 is longer than argument 1");

expect(() => {
take(Number.POSITIVE_INFINITY, zip([1, 2, 3], [1, 2], { strict: true }));
[...zip([1, 2, 3], [1, 2], { strict: true })];
}).toThrow("zip() argument 1 is longer than argument 2");
});
});
7 changes: 2 additions & 5 deletions packages/python/src/itertools/accumulate.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";
import { accumulate } from "./accumulate";

describe("accumulate", () => {
it("should accumulate values from iterator", () => {
const iterable = accumulate([1, 2, 3], (x, y) => x + y);

expect(take(Number.POSITIVE_INFINITY, iterable)).toEqual([1, 3, 6]);
expect([...iterable]).toEqual([1, 3, 6]);
});

it("should accumulate values from iterator with initial value", () => {
const iterable = accumulate([1, 2, 3, 4, 5], (a, b) => a + b, 100);

expect(take(Number.POSITIVE_INFINITY, iterable)).toEqual([
100, 101, 103, 106, 110, 115,
]);
expect([...iterable]).toEqual([100, 101, 103, 106, 110, 115]);
});
});
13 changes: 3 additions & 10 deletions packages/python/src/itertools/batched.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools/take";
import { batched } from "./batched";

describe("batched", () => {
it("returns remainder in final batch", () => {
const result = take(
Number.POSITIVE_INFINITY,
batched([1, 1, 1, 1, 1, 1, 2, 2], 3),
);
const result = [...batched([1, 1, 1, 1, 1, 1, 2, 2], 3)];
expect(result).toEqual([
[1, 1, 1],
[1, 1, 1],
Expand All @@ -17,16 +13,13 @@ describe("batched", () => {

it("throw error when batch length does not matched n with strict option", () => {
expect(() => {
take(
Number.POSITIVE_INFINITY,
batched([1, 1, 1, 1, 1, 1, 2, 2], 3, { strict: true }),
);
[...batched([1, 1, 1, 1, 1, 1, 2, 2], 3, { strict: true })];
}).toThrow("batched(): incomplete batch");
});

it("throw error when n is less than 1", () => {
expect(() => {
take(Number.POSITIVE_INFINITY, batched([1, 1, 1, 1, 1, 1, 2, 2], 0));
[...batched([1, 1, 1, 1, 1, 1, 2, 2], 0)];
}).toThrow("n must be at least one");
});
});
3 changes: 1 addition & 2 deletions packages/python/src/itertools/chain.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";
import { chain } from "./chain";

describe("concat", () => {
it("concat two iterables", () => {
const result = take(Number.POSITIVE_INFINITY, chain("ABC", "DEF"));
const result = [...chain("ABC", "DEF")];

expect(result).toEqual(["A", "B", "C", "D", "E", "F"]);
});
Expand Down
21 changes: 10 additions & 11 deletions packages/python/src/itertools/compress.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";
import { compress } from "./compress";

describe("compress", () => {
it("compress on empty list", () => {
expect(take(Number.POSITIVE_INFINITY, compress([], []))).toEqual([]);
expect([...compress([], [])]).toEqual([]);
});

it("compress removes selected items", () => {
expect(
take(Number.POSITIVE_INFINITY, compress("ABCDEF", [1, 0, 1, 0, 1, 1])),
).toEqual(["A", "C", "E", "F"]);
expect([...compress("ABCDEF", [1, 0, 1, 0, 1, 1])]).toEqual([
"A",
"C",
"E",
"F",
]);

expect(
take(
Number.POSITIVE_INFINITY,
compress("ABCDEF", [true, false, true, false, true, true]),
),
).toEqual(["A", "C", "E", "F"]);
expect([
...compress("ABCDEF", [true, false, true, false, true, true]),
]).toEqual(["A", "C", "E", "F"]);
});
});
30 changes: 5 additions & 25 deletions packages/python/src/itertools/dropwhile.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";

import { dropwhile } from "./dropwhile";

describe("dropwhile", () => {
it("dropwhile on empty list", () => {
expect(
take(
Number.POSITIVE_INFINITY,
dropwhile((x) => x < 2, []),
),
).toEqual([]);
expect([...dropwhile((x) => x < 2, [])]).toEqual([]);
});

it("dropwhile on list", () => {
expect(
take(
Number.POSITIVE_INFINITY,
dropwhile((x) => x % 2 === 0, [1]),
),
).toEqual([1]);
expect(
take(
Number.POSITIVE_INFINITY,
dropwhile((x) => x % 2 === 1, [1]),
),
).toEqual([]);
expect(
take(
Number.POSITIVE_INFINITY,
dropwhile((x) => x < 5, [1, 4, 6, 3, 8]),
),
).toEqual([6, 3, 8]);
expect([...dropwhile((x) => x % 2 === 0, [1])]).toEqual([1]);
expect([...dropwhile((x) => x % 2 === 1, [1])]).toEqual([]);
expect([...dropwhile((x) => x < 5, [1, 4, 6, 3, 8])]).toEqual([6, 3, 8]);
});
});
21 changes: 8 additions & 13 deletions packages/python/src/itertools/filterfalse.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";

import { filterfalse } from "./filterfalse";

describe("filterfalse", () => {
it("should filter elements predicate return true", () => {
expect(
take(
Number.POSITIVE_INFINITY,
filterfalse([1, 4, 6, 3, 8], (x) => x < 5),
),
).toEqual([6, 8]);
expect([...filterfalse([1, 4, 6, 3, 8], (x) => x < 5)]).toEqual([6, 8]);
});

it("should filter truthy value if predicate did not specify", () => {
expect(
take(
Number.POSITIVE_INFINITY,
filterfalse([1, undefined, 2, null, 3, "", 4, 0, 5]),
),
).toEqual([undefined, null, "", 0]);
expect([...filterfalse([1, undefined, 2, null, 3, "", 4, 0, 5])]).toEqual([
undefined,
null,
"",
0,
]);
});
});
8 changes: 3 additions & 5 deletions packages/python/src/itertools/starmap.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";
import { starmap } from "./starmap";

describe("starmap", () => {
it("starmap with pre-zipped tuple", () => {
const result = take(
Number.POSITIVE_INFINITY,
starmap(Math.pow, [
const result = [
...starmap(Math.pow, [
[2, 5],
[3, 2],
[10, 3],
]),
);
];

expect(result).toEqual([32, 9, 1000]);
});
Expand Down
13 changes: 4 additions & 9 deletions packages/python/src/more-itertools/flatten.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import { describe, expect, it } from "vitest";
import { take } from "../more-itertools";
import { flatten } from "./flatten";

describe("flatten", () => {
it("return flatten iterables in iterables of iterables", () => {
const result = take(
Number.POSITIVE_INFINITY,
flatten([
const result = [
...flatten([
[1, 2],
[3, 4],
]),
);
];

expect(result).toEqual([1, 2, 3, 4]);
});

it("only one level flatten", () => {
const result = take(
Number.POSITIVE_INFINITY,
flatten([[[1, 2]], [[3, 4]]]),
);
const result = [...flatten([[[1, 2]], [[3, 4]]])];

expect(result).toEqual([
[1, 2],
Expand Down

0 comments on commit 6bcc375

Please sign in to comment.