Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/itertools/mod.ts>flatmap

🦕 A TypeScript port of Python's itertools and more-itertools for Deno
Go to Latest
function flatmap
import { flatmap } from "https://deno.land/x/itertools@v1.1.1/mod.ts";

Returns 0 or more values for every value in the given iterable. Technically, it's just calling map(), followed by flatten(), but it's a very useful operation if you want to map over a structure, but not have a 1:1 input-output mapping. Instead, if you want to potentially return 0 or more values per input element, use flatmap():

For example, to return all numbers n in the input iterable n times:

>>> const repeatN = n => repeat(n, n);
>>> [...flatmap([0, 1, 2, 3, 4], repeatN)]
[1, 2, 2, 3, 3, 3, 4, 4, 4, 4]  // note: no 0

Parameters

iterable: Iterable<T>
mapper: (v: T) => Iterable<S>

Returns

Iterable<S>