Skip to content

Instantly share code, notes, and snippets.

@ronnelreposo
Created August 1, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnelreposo/8d18a2c4e04d626a593d532740a99f25 to your computer and use it in GitHub Desktop.
Save ronnelreposo/8d18a2c4e04d626a593d532740a99f25 to your computer and use it in GitHub Desktop.
Functional Programming Example
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