Last active
August 3, 2024 02:31
-
-
Save getify/21148d8f49143980765ded4abb139012 to your computer and use it in GitHub Desktop.
illustrating some "dynamic composition" usage
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
function flow(...fns) { | |
return arg => fns.reduce((res,fn) => fn(res),arg); | |
} | |
function partial(fn,initialArgs) { | |
return (nextArgs = []) => fn(...initialArgs,...nextArgs); | |
} | |
function pick(prop) { | |
return obj => obj[prop]; | |
} | |
function prepend(prefix) { | |
return val => `${prefix}${val}`; | |
} | |
function append(suffix) { | |
return val => `${val}${suffix}`; | |
} | |
// *************************** | |
function formatName1(name) { | |
return `${name.last}, ${name.first}`.toUpperCase(); | |
} | |
function formatName2(name) { | |
const fname = `${name.first}`.toLowerCase(); | |
return `${fname[0].toUpperCase()}${fname.slice(1)}`; | |
} |
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
// note: these are partially-applied composition functions | |
const getCustomerName = partial(flow,[ | |
pick("customerName"), | |
formatName2 | |
]); | |
const getEmployeeName = partial(flow,[ | |
pick("employeeName"), | |
formatName1 | |
]); | |
const emailGreeting = record => ( | |
(record.isEmployee ? getEmployeeName : getCustomerName) | |
([ | |
append("--") | |
]) | |
(record) | |
); | |
const chatGreeting = record => ( | |
(record.isEmployee ? getEmployeeName : getCustomerName) | |
([ | |
prepend("Hello "), | |
append("!") | |
]) | |
(record) | |
); |
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 kyle = { customerName: { first: "Kyle", last: "Simpson", }, }; | |
const sally = { isEmployee: true, employeeName: { first: "Sally", last: "MCDANIELS", }, }; | |
emailGreeting(kyle); // Kyle-- | |
chatGreeting(kyle); // Hello Kyle! | |
emailGreeting(sally); // MCDANIELS, SALLY-- | |
chatGreeting(sally); // Hello MCDANIELS, SALLY! |
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
// note: these are just arrays | |
const getCustomerName = [ | |
pick("customerName"), | |
formatName2 | |
]; | |
const getEmployeeName = [ | |
pick("employeeName"), | |
formatName1 | |
]; | |
const emailGreeting = record => ( | |
record | |
|> flow( | |
...(record.isEmployee ? getEmployeeName : getCustomerName) | |
)(^) | |
|> append("--")(^) | |
); | |
const chatGreeting = record => ( | |
record | |
|> flow( | |
...(record.isEmployee ? getEmployeeName : getCustomerName) | |
)(^) | |
|> prepend("Hello ")(^) | |
|> append("!")(^) | |
); |
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
// note: these are just arrays | |
const getCustomerName = [ | |
pick("customerName"), | |
formatName2 | |
]; | |
const getEmployeeName = [ | |
pick("employeeName"), | |
formatName1 | |
]; | |
// note: using a proposed `...` extension to `|>` | |
const emailGreeting = record => ( | |
record | |
|> ...(record.isEmployee ? getEmployeeName : getCustomerName) | |
|> append("--")(^) | |
); | |
const chatGreeting = record => ( | |
record | |
|> ...(record.isEmployee ? getEmployeeName : getCustomerName) | |
|> ...[ prepend("Hello "), append("!") ] | |
); |
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
// note: these are just arrays | |
const getCustomerName = [ | |
pick("customerName"), | |
formatName2 | |
]; | |
const getEmployeeName = [ | |
pick("employeeName"), | |
formatName1 | |
]; | |
// note: using a proposed `|=>` which is `|>` + `=>` | |
const emailGreeting = record |=> ( | |
...(^.isEmployee ? getEmployeeName : getCustomerName) | |
|> append("--")(^) | |
); | |
const chatGreeting = record |=> ( | |
...(^.isEmployee ? getEmployeeName : getCustomerName) | |
|> ...[ prepend("Hello "), append("!") ] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment