liveBook · Manning

Abstract data type

As explained in The Joy of JavaScript

A data abstraction defined by its behavior and operations rather than by its concrete representation.

An abstract data type (ADT) defines a value through its behavior and supported operations rather than exposing its concrete representation. In functional JavaScript, an ADT can provide a context—such as success or validation state—while operations transform the contained value without requiring callers to manage that context directly. section 5.1 section 5.6

The practical payoff is composability. Extracted operations such as map can transform an ADT fluently, while flatMap sequences operations that may preserve or change the surrounding context. For example, safe division chains validation for non-NaN and nonzero values before performing the division. section 5.6

A context-preserving computation

Success.of(2)::map(x => x * 2)::map(x => x ** 2) produces Success(16). The individual functions operate on the contained number, while the Success context remains part of the result. section 5.6

const { flatMap } = Monad;

const safeDivideBy = denominator => numerator => 
   Success.of(numerator) 
      :: flatMap(notNaN) 
      :: flatMap(notZero)
      :: map(divideBy(denominator));

const halve = safeDivideBy(2);

halve(16); // Success(8)

Shows an ADT-oriented safe division pipeline that combines flatMap validation with map for the final transformation. section 5.6

How The Joy of JavaScript covers it

How deep it goes
Builds from wrappers and containers toward functor and monad operations, while deferring applicative monads.
Examples
Demonstrates Success values, chained map calls, safe division, and an Array-derived identity container.
What it assumes
Assumes familiarity with functions, higher-order functions, currying, composition, and JavaScript method binding.
What you can do after
Builds and composes context-preserving transformations with map, flatMap, currying, and fluent JavaScript syntax.
How it connects
Connects ADTs with containers, higher-order functions, currying, composition, functors, monads, and method extraction.

Also explained in

Compare how 4 books explain abstract data type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.