Skip to content

Instantly share code, notes, and snippets.

@newmanbrad
Last active December 27, 2016 19:15
Show Gist options
  • Save newmanbrad/d2dd7de26338474d9dcc242c2b8f195c to your computer and use it in GitHub Desktop.
Save newmanbrad/d2dd7de26338474d9dcc242c2b8f195c to your computer and use it in GitHub Desktop.
functional Typescript Examples #1
let courses = [
{ name: 'functional fundamentals', enrolled: 2, grade: 100, students: 30 },
{ name: 'category theory', enrolled: 2, grade: 80, students: 25 },
{ name: 'basket weaving', enrolled: 1, grade: 89, students: 43 }
];
function avg(nums: number[]): number {
const total = nums.reduce((a,b) => a + b, 0);
return (nums.length == 0) ? 0 : total / nums.length;
}
function pluck<T, K extends keyof T>(objs: T[], key: K): T[K][] {
return objs.map(obj => obj[key]);
}
let overOneEnrolled = courses.filter(course => course.enrolled > 1);
let averageItem = avg(pluck(overOneEnrolled, 'grade'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment