Flatmap
FlatMap is an operation that maps each element to a sequence or computational context and combines the resulting sequences or contexts into a flattened result.
Functional Programming in Scala, Second Edition
flatMap is a monadic combinator that applies a computation-producing function and combines its result with the surrounding monadic context.
- How it is used
- Chains computations when an earlier result determines the next List, Option, state, parallel, parser, or stream computation.
- How deep it goes
- Builds implementations for List, Option, Rand, Par, and Parser, then generalizes flatMap through Monad laws.
- Examples
- Uses list duplication, optional variance, bounded random numbers, employee lookups, generators, parsers, and temperature-conversion streams.
- What you can do after
- Build it from scratch for List, Option, and Rand, then apply it to dependent computations across several abstractions.
Read how Functional Programming in Scala, Second Edition explains flatmap →
Spring in Action, Sixth Edition
flatMap is a Project Reactor operator that maps each source element to a publisher and merges the resulting publishers into one sequence.
- How it is used
- Composes publisher-returning transformations in reactive streams, web handlers, client requests, and repository operations.
- How deep it goes
- Explains flattening, contrasts map(), and demonstrates scheduling and publisher composition without detailing operator internals.
- Examples
- Uses player-name transformations, a taco-saving controller, and conditional ingredient-response decoding.
- What you can do after
- Apply it via Project Reactor to compose publisher-returning transformations and schedule inner work.
The Joy of JavaScript
Functional composition operations that transform values and flatten nested computations or containers.
- How it is used
- Uses flatMap to flatten mapped array data and chain computations that return containers.
- How deep it goes
- Explains map-then-flat, implements a simple container protocol, and illustrates monad laws with arrays.
- Examples
- Shows blockchain balance calculation, array-based monad laws, and validation of nested blockchain objects.
- What you can do after
- Build it from scratch for a simple container, or apply it to flatten array pipelines and compose validation results.
Functional Programming in JavaScript
- How it is used
- Builds safe pipelines when validation or lookup operations return Maybe or Either values.
- How deep it goes
- Defines bind as flatMap, relates it to join, and demonstrates chained monadic error handling.
- Examples
- Uses nested Wrapper addresses and a student SSN validation, lookup, formatting, and HTML-update pipeline.
- What you can do after
- Evaluate/understand when to replace map with chain to avoid nested monads and propagate failures safely.
Read how Functional Programming in JavaScript explains flatmap →
Secrets of the JavaScript Ninja, Third Edition
flatMap is an array method that maps each element to a value and flattens the resulting arrays by one level.
- How it is used
- Uses flatMap to turn arrays whose items contain zero or multiple values into one aggregate array.
- How deep it goes
- Explains callback results and one-level flattening, but does not detail the optimized operation’s internals.
- Examples
- Demonstrates collecting weapons from objects that hold two weapons or none.
- What you can do after
- Apply it via JavaScript Array.flatMap to collect zero or more values per input into one array.
Read how Secrets of the JavaScript Ninja, Third Edition explains flatmap →
Fabulous Adventures in Data Structures and Algorithms
FlatMap is an operation that maps each element to a sequence and concatenates the resulting sequences into one sequence.
- How it is used
- Flattens mapped sequences and composes branching computations such as backtracking searches.
- How deep it goes
- Defines flatMap, derives its C# implementation, connects it to monadic operations, and demonstrates recursive search.
- Examples
- Uses nested C# iteration, zero-or-one-element filtering, graph-coloring search, and Sudoku solving.
- What you can do after
- Build it from scratch as nested iteration and apply it to concatenate branching solution sequences.
Read how Fabulous Adventures in Data Structures and Algorithms explains flatmap →
Functional Programming in C#, Second Edition
SelectMany is a LINQ query operator that projects each element to a sequence and flattens the resulting sequences.
- How it is used
- Uses SelectMany to flatten results in LINQ queries over sequences, optional values, and asynchronous observable streams.
- How deep it goes
- Explains the basic and projected IEnumerable overloads, compiler translation, and an Option implementation.
- Examples
- Uses character–integer pairs, optional integer addition, and asynchronous currency-rate streams as examples.
- What you can do after
- Apply SelectMany to combine sequences, optional values, or asynchronous observable results with LINQ or method syntax.
Read how Functional Programming in C#, Second Edition explains flatmap →
Also covered in
- Idiomatic Rust
- The Art of Code
- Data-Oriented Programming: Reduce software complexity
- Kotlin in Action, 2E
- F# in Action: A Revised Edition of Get Programming with F#
- Kafka Streams in Action, Second Edition
What to know first
- exceptionAn exception is a runtime signal or value representing an abnormal condition that interrupts ordinary program flow and can be handled or propagated.
- lambdaA lambda is an expression that defines an unnamed function, specifying its parameters and body and producing a callable value.
- functional programmingFunctional programming is a programming paradigm that expresses computation through the evaluation and composition of functions, typically favoring immutable data and minimizing mutable state and side effects.
- functorA functor is an abstraction that maps values or objects while preserving the structure and relationships of their surrounding context.
Related concepts
- bindequivalent to · Bind is an operation that chains a value in a computational context to a function returning another value in that context, flattening the resulting nested context.
- monadused by · A monad is a programming abstraction that provides operations for sequencing computations while encapsulating and preserving their computational context.
- filteringimplements · Filtering is the selection or exclusion of data or items according to specified criteria.
- query patternused by · The query pattern is a set of method conventions that enables types to be queried using LINQ syntax.
- data streamproduces · A data stream is an ordered sequence of data items that may be ongoing and is made available incrementally for consumption or processing.
- sequenceproduces · A sequence is an ordered collection of elements whose positions determine the order in which they are arranged or traversed.
Questions about flatmap
What is flatmap?
FlatMap is an operation that maps each element to a sequence or computational context and combines the resulting sequences or contexts into a flattened result.
Which Manning books explain flatmap?
Functional Programming in Scala, Second Edition, Spring in Action, Sixth Edition, The Joy of JavaScript, Functional Programming in JavaScript, Secrets of the JavaScript Ninja, Third Edition, Fabulous Adventures in Data Structures and Algorithms and Functional Programming in C#, Second Edition.
What should I know before learning flatmap?
Manning books that teach flatmap usually introduce exception, lambda, functional programming and functor first.
What is flatmap also called?
Flatmap is also called SelectMany.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






