...

/

Solution Review: Adding a Check

Solution Review: Adding a Check

Let’s look at the solution to the coding challenge of “Adding a Check” in our application.

We'll cover the following...

Solution

Let’s add a check in the following code.

Press + to interact
// Note that you could also use the fp-ts identity function in getOrElse
const checkValidHour = (hour: number): E.Either<string, number> => {
return hour > 0 && hour <= 24 ? E.right(hour) : E.left('invalid hour');
};
const mapToGreeting = (hour: number): string => {
return hour < 12 ? 'good morning' : 'good afternoon';
};
const checkDecentHour = (hour: number): E.Either<string, number> => {
return hour >= 8 ? E.right(hour) : E.left('way too early');
};
const application = (hour: number) => {
return pipe(
checkValidHour(hour),
E.chain(checkDecentHour),
h => E.map(mapToGreeting)(h),
E.getOrElse(err => err),
);
};
console.log(application(5));
console.log(application(9));

Explanation

Here’s a ...