Adding operations

Let's consider a simple problem. Suppose you have the following pair of functions, working with Maybe functors: the first function tries to search for something (say, a client or a product) given its key, and the second attempts to extract some attribute from it (I'm being purposefully vague because the problem does not have anything to do with whatever objects or things we may be working with). Both functions produce Maybe results to avoid possible errors. We are using a mocked search function, just to help us see the problem: for even keys, it returns fake data, and for odd keys, it throws an exception. The code for this search is very simple:

const fakeSearchForSomething = key => {  if (key % 2 === 0) { return {key, some: ...

Get Mastering JavaScript Functional Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.