Created
March 12, 2018 17:38
-
-
Save ajcrites/2b6b62091079b6e84c95e105e545684a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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