O'Reilly logo

Mastering JavaScript Functional Programming by Federico Kereki

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dealing with missing values with Maybe

A common problem in programming is dealing with missing values. There are many possible causes for this situation: a web service Ajax call may have returned an empty result, or a dataset could be empty, or an optional attribute might be missing from an object, and so on. Dealing with this kind of situation, in normal imperative fashion, requires adding if statements or ternary operators everywhere, to catch the possible missing value, avoiding a certain runtime error. We can do a bit better by implementing a Maybe functor, to represent a value that may be (or may not be) present! We will use two classes, Just (as in just some value) and Nothing, one for each functor:

class Nothing extends Functor {

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required