toMap<K,V,>(source: AsyncIterable<[K, V]>): Promise<Map<K, V>>
Creates a map from an async iterable of key-value pairs. Each pair is represented as an array of two elements.
import { toMap } from "./collections.ts"; async function* gen(): AsyncIterableIterator<[string, number]> { yield ["foo", 1]; yield ["bar", 2]; yield ["baz", 3]; yield ["qux", 4]; } const map = await toMap<string, number>(gen());
The map variable will be a map like Map { "foo" => 1, "bar" => 2, "baz" => 3, "qux" => 4 }.
Duplicate keys are removed except for the last occurrence of each key. E.g.:
import { fromIterable, toMap } from "./collections.ts"; const iterable = fromIterable<[string, number]>([ ["foo", 1], ["bar", 2], ["baz", 3], ["qux", 4], ["foo", 5], ["bar", 6], ]); const map = await toMap<string, number>(iterable);
The map variable will be a map like Map { "foo" => 5, "bar" => 6, "baz" => 3, "qux" => 4 }.
Note that the iterable source is assumed to be finite; otherwise, it will
never return. The following example will never return:
import { toMap } from "./collections.ts"; import { count } from "./infinite.ts"; import { map } from "./map.ts"; await toMap<number, number>( map((v: number) => [v, v] as [number, number], count(0)) );