liveBook · Manning

Map

As explained in F# in Action: A Revised Edition of Get Programming with F#

A map operation applies a function to a value inside a computational effect and rewraps the result in that effect.

map transforms a value without changing the surrounding context that holds it. Give it a function from 'T to 'U, and it lifts that function so it can work on 'T inside an effect and return 'U inside the same effect. The effect decides what happens when no usable value is available; the mapping function stays ordinary, focused code. section 15.4.3

For lists, List.map applies a mapping function to every item and produces a new list of transformed items. Its signature, mapping : ('T -> 'U) -> 'T list -> 'U list, makes the two inputs explicit: a converter and the list to convert. It does not modify the original list. section 7.2.3 section 7.2.4

Convert list values

Use a small mapping function when each input item becomes one output item: let output = [ 1; 2; 3; 4 ] |> List.map (fun x -> x.ToString()) The result is a new list of strings. The pipeline keeps the data flow visible: start with values, then describe their conversion. section 7.2.3

The same pattern avoids writing separate, hand-rolled logic for common effects. Option.map runs a function when a value exists and preserves None otherwise. Result.map transforms an Ok value while preserving an Error. Task.map waits for its value, and List.map transforms every list item. In each case, map handles the effect-specific checking and rewrapping. section 15.4.3

Lift one function across effects

A normal function can be reused unchanged: let timesTwo (x:int) = $"{x} * 2 is {x * 2}" let timesTwoOptional = Option.map timesTwo let timesTwoResult = Result.map timesTwo let timesTwoList = List.map timesTwo let timesTwoTask = Task.map timesTwo Each mapped function retains its input effect while changing the value inside it. section 15.4.3

Choose map when one input value corresponds to one transformed output value inside the same effect:

  • Use List.map to convert every item in a list from one shape to another. section 7.2.4
  • Use Option.map, Result.map, or Task.map when the surrounding optionality, validation state, or completion state must be preserved. section 15.4.3
  • Use collect, also called flatMap or SelectMany elsewhere, when each item produces a list and the result must be flattened. section 7.2.4 section 15.4.3

Do not confuse the map operation with the F# Map type. Map is an immutable dictionary-like key/value collection: adding a key returns a new map, and adding an existing key replaces its old value. The map operation, by contrast, is a transformation function such as List.map or Result.map. section 7.3.5 section 15.4.3

Table 15.3 How map appropriately behaves for each different effect (view table figure)

Name

Happy path

Unhappy path

Option

Data exists; apply your code to it

No data (None); return it back

Result

Data is valid; apply your code to it

Data is invalid (Error); return it back

Task

Data is available; apply your code to it

Data not yet available; keep waiting

List

List is nonempty; apply your code to all values

Empty list; return it back

Compare map’s happy and unhappy paths across Option, Result, Task, and List to see how the same mapping function preserves each effect’s behavior. section 15.4.3

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains map through its generic signature, list use, and effect-specific handling, while deferring implementations to open-source code.
Examples
Uses integer-to-string list conversion, a timesTwo formatter, customers’ orders, and chained division results.
What it assumes
Assumes familiarity with F# function arrows and generic type parameters while walking through the List.map signature.
What you can do after
Apply it via List.map, Option.map, Result.map, or Task.map to reuse ordinary transformations across effects.
How it connects
Connects map with List, Option, Result, Task, bind, collect, flatMap, and SelectMany.

Also explained in

Compare how 8 books explain map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.