Last active
December 27, 2016 19:15
-
-
Save newmanbrad/d2dd7de26338474d9dcc242c2b8f195c to your computer and use it in GitHub Desktop.
functional Typescript Examples #1
This file contains 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
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