Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Created March 12, 2018 17:38
Show Gist options
  • Save ajcrites/2b6b62091079b6e84c95e105e545684a to your computer and use it in GitHub Desktop.
Save ajcrites/2b6b62091079b6e84c95e105e545684a to your computer and use it in GitHub Desktop.
const map = <T, R = T>(items: T[], cb: (item: T) => R): R[] => {
const mapped: R[] = [];
for (let item of items) {
mapped.push(cb(item));
}
return mapped;
};
const mapx = (items, cb) => {
const mapped = [];
for (let item of items) {
mapped.push(cb(item));
}
return mapped;
};
console.log(map(['1', '2', '3'], str => parseInt(str))[0]);
interface Person {
age: number;
name: string;
}
interface Human {
years: number;
}
map<Human, Person>([{ years: 12 }], ({ years: age }) => ({ age, name: 'unknown' }));
map([{ years: 12 }], ({ years: age }) => ({ age }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment