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, whatever) 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:

const fakeSearchForSomething = key => {    if (key % 2 === 0) { return {key, some: "whatever", other: "more data"}; ...

Get Mastering Javascript Functional Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.