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 isFemale = match('gender', is('female')); | |
| const isYoungAdult = match('age', isOrOver(18)); | |
| const canPlayNightOwl = match('age', isUnder(60)); | |
| const ageIsSpecialNumber = match('age', isMultipleOf(4)); | |
| const getSpecialFemaleDevs = chain( | |
| isFemale, | |
| isYoungAdult, | |
| canPlayNightOwl, | |
| ageIsSpecialNumber |
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 is = (target) => (value) => value === target; | |
| const isOver = (target) => (value) => value > target; | |
| const isOrOver = (target) => (value) => value >= target; | |
| // match devs over 24 years old | |
| const olderThan24 = match('age', isOver(24)); | |
| const over24s = olderThan24(data); | |
| // match devs at, or over 35 years old | |
| const aged35OrOlder = match('age', isOrOver(35)); |
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 isOver24 = (value) => value > 24; | |
| const getOver24s = match('age', isOver24); | |
| const over24s = getOver24s(data); |
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 match = (field, delegate) => { | |
| return (data = []) => { | |
| return data.filter(entry => delegate(entry[field])); | |
| } | |
| }; | |
| const isMale = (value) => value === 'male'; | |
| const isFemale = (value) => value === 'female'; |
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 getFemales = match('gender', isFemale); | |
| const femaleDevs = getFemales(data); |
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 sumAges = compute(sumOf, 'age'); | |
| const sumOfAge = sumAges(data); | |
| const avgAge = compute(averageOf, 'age'); | |
| const averageAge = avgAge(data); | |
| console.log(`... while total age of African developers was ${sumOfAge}, the average age was ${averageAge}`); |
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 sumOf = (data = [], field) => { | |
| return data.reduce((sum, entry) => { | |
| return sum += entry[field] || 0; | |
| }, 0); | |
| }; | |
| const averageOf = (data = [], field) => { | |
| return data.reduce((sum = 0, entry, index) => { | |
| const sum += (entry[field] || 0); |
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 compute = (delegate, field) => { | |
| return (data) => delegate(data, field); | |
| }; |
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 avgAge = compute(averageOf, 'age'); | |
| const avgOfAges = avgAge(data); |
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 sumAges = compute(sumOf, 'age'); | |
| const sumOfAge = sumAges(data); |