Reductions

Find the incremental values of a sequence “reduce” operation.

Reductions.md

Overview

Call one of the reductions methods when you want the result of a reduce operation along with all of its intermediate values:

let exclusiveRunningTotal = (1...5).reductions(0, +)
print(exclusiveRunningTotal)
// prints [0, 1, 3, 6, 10, 15]

let inclusiveRunningTotal = (1...5).reductions(+)
print(inclusiveRunningTotal)
// prints [1, 3, 6, 10, 15]

Topics

Supporting Types

Deprecated Methods

See also

  • Combinations and Permutations

    Find the combinations and permutations of any collection’s elements, or the product of two different collections.

    Read More
  • Slicing and Splitting

    Iterate over tuple pairs of adjacent elements, overlapping windows of a specified size, or lazily-calculated splits.

    Read More
  • Chunking

    Break collections into consecutive chunks by length, count, or based on closure-based logic.

    Read More
  • Joining

    Join the parts of a collection of collections, providing a connecting element or collection, or a closure that produces the connector.

    Read More
  • Extending

    Chain two collections end-to-end, or repeat a collection forever or a specific number of times.

    Read More
  • Trimming

    Remove unwanted elements from the start, the end, or both ends of a collection.

    Read More
  • Keying and Grouping

    Convert a sequence to a dictionary, providing keys to individual elements or to use as grouping values.

    Read More
  • Random Sampling

    Choose a specified number of random elements from a sequence or collection.

    Read More
  • Finding the Minimum and Maximum

    Find the minimum and maximum elements simultaneously, or a specific number of elements at the minimum and maximum.

    Read More
  • Selecting Elements

    Select elements at a particular interval, the first mapped value, or iterate of elements with their indices.

    Read More
  • Filtering

    Remove duplicated elements or strip the nil values from a sequence or collection.

    Read More
  • Partitioning and Rotating

    Partition a collection according to a unary predicate, rotate a collection around a particular index, or find the index where a collection is already partitioned.

    Read More