Skip to content

Instantly share code, notes, and snippets.

@nateq314
nateq314 / flatten.ts
Last active March 21, 2019 12:16
non-mutating flatten()
/**
* Return the flattened version of an arbitrarily deep nested array as a new
* array, keeping the original unchanged. It uses a simple recursive algorithm.
*
* Positives: clean, intuitive, doesn't mutate the argument
* Negatives: 1) probably uses more memory than alternative implementations.
* 2) recursive, so in theory more limited by system resources than
* an alternative non-recursive algorithm would be. Unlikely to
* be a problem in most use cases but if you had an array nested
* say 1 million levels deep, probably wouldn't want to use this

Keybase proof

I hereby claim:

  • I am nateq314 on github.
  • I am nateq (https://keybase.io/nateq) on keybase.
  • I have a public key ASCA3MG53QsWUcHzRsHGagZMkcdlhkKNC25-qenpKZ8jRgo

To claim this, I am signing this object: