Create Your Own Reduce Function in Swift

There’s an Iterator…and…

Steven Curtis
4 min readMay 1, 2020
Photo by Noah Buscher on Unsplash

Reduce makes an Array a single function that is it combines all of the given elements in an array and outputs a single value (more on that in a minute) — according to the function we give it.

How is that implemented? What do we do?

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

Terminology

Array: An ordered series of objects which are the same type

Dictionary: The association between keys and values (where all the keys are of the same type, and all values are of the same type)

Reduce: A higher-order function that returns the result of combining the elements of a sequence using a closure

The Motivation

Reduce is one of the coolest higher — order functions available to us in Swift.

What if we could look about how reduce works under the hood, and perhaps mangle the functionality to be…whatever we would want it to be.

The reduce function

The original

--

--

Responses (2)