Skip to content

Instantly share code, notes, and snippets.

@ntnbst
Last active August 9, 2023 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ntnbst/2aebdc4fc8366465f8087286e369f58a to your computer and use it in GitHub Desktop.
Save ntnbst/2aebdc4fc8366465f8087286e369f58a to your computer and use it in GitHub Desktop.
function myForEach<T>(arr: T[], forEachFunc: (v: T) => void) {
arr.reduce((acc, val) => {
forEachFunc(val)
return undefined;
}, undefined)
}
function myFilter<T> (arr: T[], filterFunc: (v: T) => boolean): T[] {
return arr.reduce((acc: T[], val) => {
if (filterFunc(val)) {
return [...acc, val]
} else {
return [...acc]
}
}, [])
}
function myMap<T, K>(arr: T[], mapFunc: (v: T) => K) {
return arr.reduce((acc: K[], curr) => [...acc, mapFunc(curr)], [])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment