Skip to content

Instantly share code, notes, and snippets.

@joshwashywash
Last active February 27, 2022 00:30
Show Gist options
  • Save joshwashywash/0b31b58ff2331808c1320e7d6271e6d1 to your computer and use it in GitHub Desktop.
Save joshwashywash/0b31b58ff2331808c1320e7d6271e6d1 to your computer and use it in GitHub Desktop.
zips two arrays together

Zip function for TypeScript

all elements of as must be of type A. all elements of bs must be of type B. you can do this using recursion and you wouldn't have to know about the lengths of the arrays but that's for another gist.

const zip = <A, B>(as: A[], bs: B[]) => {
  const length = Math.min(as.length, bs.length);
  return Array.from({ length }, (_, i) => [as[i], bs[i]]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment