Transforming Each Item in a Loop

A mapping loop applies one transformation per input item. The result typically keeps positional correspondence with the source.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Compute one result per item
  2. Choose what a failure does
  3. Track one output position for every input
  4. Choose what the map does when an input fails

Compute one result per item

Converting temperatures in degrees Celsius by \(F=9C/5+32\) maps 0, 10, and 20 to 32, 50, and 68 in Fahrenheit. Unit-aware values make the input and output scales clear in the conversion's name.

Choose what a failure does

If a source field is missing, you can preserve a missing result at that position or report an error. Dropping it changes the correspondence and acts as filtering. Test a missing value in the middle rather than only at the end.[1]

Track one output position for every input

A conversion mapping speeds from meters per second to kilometers per hour multiplies each reading by 3.6. Readings 0, 1, and 2 correspond to 0, 3.6, and 7.2. The output positions stay attached to the input positions under a normal map. If one reading is absent in the middle, preserving an absent result at that same position lets consumers align with timestamps. Dropping it would also perform a filter. Values with units describes how to preserve the intended conversion scale.

Choose what the map does when an input fails

For a per-row parser, decide whether the first invalid item aborts the whole operation or whether each row contributes a success or error result. A list of per-row results can still preserve one position per input. An entire export may require a complete accepted collection before becoming visible. See staged parsing and published replacement.

Give tests distinct inputs, an empty list, and one bad value between two good ones. Zero and equal repeated values can obscure a missing transformation or accidental reordering.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩