Functional Programming Example
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
| type Customer = { | |
| firstname: string | |
| middlename: string | |
| lastname: string | |
| age: number | |
| } | |
| const formatSalesClerkCustomerName = (customer: Customer): string => { | |
| const formattedName = `${customer.lastname}, ${customer.firstname}`; | |
| return (customer.middlename && customer.middlename[0]) ? | |
| `${formattedName} ${customer.middlename[0]}.` : formattedName; | |
| }; | |
| const isCutomerLegalAge = (customer: Customer): boolean => { | |
| return customer.age > 18; | |
| }; | |
| const customerStatus = (customer: Customer): string => { | |
| return 'Status: ' + isCutomerLegalAge(customer)? 'Allowed' : 'Not Allowed'; | |
| }; | |
| const formatCustomerName = ( | |
| customer: Customer, | |
| nameFormatter: (customer: Customer) => string): string => { | |
| return 'Name: ' + nameFormatter(customer); | |
| }; | |
| const reportingFormatter = (customer: Customer): string => { | |
| throw new Error('not implemented yet'); | |
| }; | |
| let bran: Customer = { | |
| firstname: 'bran', | |
| middlename: 'tully', | |
| lastname: 'stark', | |
| age: 8 | |
| } | |
| // Usage | |
| formatCustomerName(bran, formatSalesClerkCustomerName); | |
| formatCustomerName(bran, reportingFormatter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment