Skip to main content
Module

x/aitertools/mod.ts>toSet

Well-tested utility functions dealing with async iterables
Go to Latest
function toSet
import { toSet } from "https://deno.land/x/aitertools@0.5.0/mod.ts";

Creates a set from an async iterable.

import { toSet } from "./collections.ts";

async function* gen() { yield "foo"; yield "bar"; yield "baz"; }
const set = await toSet(gen());

The set variable will be a set like new Set(["foo", "bar", "baz"]).

Duplicate elements are removed except for the first occurrence of each element. E.g.:

import { toSet } from "./collections.ts";

async function* gen() { yield "foo"; yield "bar"; yield "foo"; }
const set = await toSet(gen());

The set variable will be a set like new Set(["foo", "bar"]).

Note that the iterable source is assumed to be finite; otherwise, it will never return. The following example will never return:

import { toSet } from "./collections.ts";
import { count } from "./infinite.ts";

await toSet(count(0));

Type Parameters

T

The type of the elements in the source and the returned set.

Parameters

source: AsyncIterable<T>

An async iterable to create a set from. It must be finite.

Returns

Promise<Set<T>>

A set that contains the elements from the source iterable. Duplicate elements are removed except for the first one.