@gabelluardo/itertools@0.2.0Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
itertools for Deno 🦕
itertools
This module provides generators for iterating subsets of an input. It is heavily
inspired by the combinatorial iterators provided by the
itertools package from the
Python
standard library.
- All generators are importable on their own.
- These implementations do not build up intermediate results in memory.
- All functions iterate subsets lexicographically according to their input indices. If the input is sorted the output will be too.
- Likewise, whether the input elements are unique or not does not matter.
- The inputs provided are not modified, however, consumable iterables will be consumed.
Usage
Simple Combinations
Returns r-length
subsequences of elements from the input iterable
. Order of
selection does not matter and elements are chosen without replacement.
import { assertEquals } from "@std/assert"; import { combinations } from "jsr:@gabelluardo/itertools"; const sequences = [...combinations([1, 2, 3, 4], 2)]; assertEquals(sequences, [ [1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], ]);
Simple Permutations
Returns successive r-length
permutations of elements from the iterable
.
import { assertEquals } from "@std/assert"; import { permutations } from "jsr:@gabelluardo/itertools"; const sequences = [...permutations([1, 2, 3, 4], 2)]; assertEquals(sequences, [ [1, 2], [1, 3], [1, 4], [2, 1], [2, 3], [2, 4], [3, 1], [3, 2], [3, 4], [4, 1], [4, 2], [4, 3], ]);
Combinations with Replacement
Returns r-length
subsequences of elements from the input iterable
allowing
individual elements to be repeated more than once.
import { assertEquals } from "@std/assert"; import { combinationsWithReplacement } from "jsr:@gabelluardo/itertools"; const sequences = [...combinationsWithReplacement([1, 2, 3, 4], 2)]; assertEquals(sequences, [ [1, 1], [1, 2], [1, 3], [1, 4], [2, 2], [2, 3], [2, 4], [3, 3], [3, 4], [4, 4], ]);
Permutations with Replacement
Returns successive r-length
permutations of elements from the iterable
allowing individual elements to be repeated more than once.
import { assertEquals } from "@std/assert"; import { permutationsWithReplacement } from "jsr:@gabelluardo/itertools"; const sequences = [...permutationsWithReplacement([1, 2, 3, 4], 2)]; assertEquals(sequences, [ [1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3, 2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4], ]);
Add Package
deno add jsr:@gabelluardo/itertools
Import symbol
import * as itertools from "@gabelluardo/itertools";
Import directly with a jsr specifier
import * as itertools from "jsr:@gabelluardo/itertools";
Add Package
pnpm i jsr:@gabelluardo/itertools
pnpm dlx jsr add @gabelluardo/itertools
Import symbol
import * as itertools from "@gabelluardo/itertools";
Add Package
yarn add jsr:@gabelluardo/itertools
yarn dlx jsr add @gabelluardo/itertools
Import symbol
import * as itertools from "@gabelluardo/itertools";
Add Package
npx jsr add @gabelluardo/itertools
Import symbol
import * as itertools from "@gabelluardo/itertools";
Add Package
bunx jsr add @gabelluardo/itertools
Import symbol
import * as itertools from "@gabelluardo/itertools";