...

/

Solution Review: Working with Either and Pipe

Solution Review: Working with Either and Pipe

Let’s look at the solution to the coding challenge of "Working with Either and Pipe" in our application.

We'll cover the following...

Solution

Let’s see how we use the Either and pipe() functions in the below code.

Press + to interact
const checkValidHour = (hour: number): E.Either<string, number> => {
return hour > 0 && hour <= 24 ? E.right(hour) : E.left('invalid hour');
};
const mapToGreeting = (hour: number) => {
return hour < 12 ? 'good morning' : 'good afternoon';
};
const application = (hour: number) => {
return pipe(
checkValidHour(hour),
h => E.map(mapToGreeting)(h),
E.getOrElse(() => 'unknown greeting'),
);
};
console.log(application(8));
console.log(application(13));
console.log(application(25));
...