flatMap applies a function that already returns a wrapped result, then removes the extra wrapper. It is often called bind; chain is a convenient name for the same idea. Without it, mapping monad-returning functions creates nested containers such as Wrapper(Wrapper(address)). section 5.3 section 5.4
Use flatMap when each step can produce a Maybe, Either, or another monad. Ordinary map keeps the result in its container; flatMap lets the next operation receive one usable monadic layer while preserving the container’s handling of missing values or errors. section 5.3 section 5.4
Validate and find a student
A student-display pipeline starts with a nullable SSN, cleans it with map, then chains validation and lookup because both return monads. If validation or lookup fails, dependent work is skipped; successful data is formatted and appended to the page. section 5.4
Listing 5.9. showStudent using monads for automatic error handling
const showStudent = (ssn) => #1
Maybe.fromNullable(ssn)
.map (cleanInput)
.chain(checkLengthSsn)
.chain(findStudent)
.map (R.props(['ssn', 'firstname', 'lastname']))
.map (csv)
.map (append('#student-info'));
The pipeline contrasts map steps that transform available data with chain steps that run validation and lookup functions returning monadic results. section 5.4




