Skip to content

Commit

Permalink
Itertools/takewhile (#42)
Browse files Browse the repository at this point in the history
* feat(takewhile): implement operation

* chore: apply changeset
  • Loading branch information
Baek2back authored Aug 6, 2024
1 parent 4b86d72 commit e822f0c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-tables-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@iter-x/python": patch
---

implement takewhile operation
14 changes: 14 additions & 0 deletions packages/python/src/itertools/takewhile.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from "vitest";
import { takewhile } from "./takewhile";

describe("takewhile", () => {
it("takewhile on empty list", () => {
const result = [...takewhile((x) => x < 5, [])];
expect(result).toEqual([]);
});

it("takewhile on list", () => {
const result = [...takewhile((x) => x < 5, [1, 4, 6, 3, 8])];
expect(result).toEqual([1, 4]);
});
});
13 changes: 13 additions & 0 deletions packages/python/src/itertools/takewhile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @example
* takewhile((x) => x < 5, [1,4,6,3,8]) -> 1 4
*/
export function* takewhile<T>(
predicate: (item: T) => boolean,
iterable: Iterable<T>,
): IterableIterator<T> {
for (const x of iterable) {
if (!predicate(x)) break;
yield x;
}
}

0 comments on commit e822f0c

Please sign in to comment.