Search⌘ K
AI Features

Solution Review: Using Reader Monad in Function

Understand the practical use of the Reader monad in TypeScript by reviewing a solution that enhances a reservation application. Learn how to compose functions effectively by passing transformed data through monadic workflows, improving code modularity and clarity.

We'll cover the following...

Solution

TypeScript 3.3.4
const writeToBucket = (message: string): RT.ReaderTask<Dependencies, string> => (dependencies: Dependencies) => {
return () => Promise.resolve(`Wrote '${message}' to ${dependencies.bucketName}`);
};
const writeToTable = (message: string): RT.ReaderTask<Dependencies, string> => (dependencies: Dependencies) => {
return () => Promise.resolve(`Wrote '${message}' to ${dependencies.tableName}`);
};
const application = (message: string) => {
return pipe(
upperCaseItAgain(message),
m => writeToTable(m),
RT.chainFirst(writeToBucket),
)(exampleDependencies)();
};
var msg=upperCaseItAgain("How are you")
console.log(writeToBucket(msg)({bucketName: 'ourBucket',tableName: 'ourTable'})());
console.log(writeToTable(msg)({bucketName: 'ourBucket',tableName: 'ourTable'})());

Explanation

...