Map, Filter and Reduce in Swift
Tags: swift, ios, mobile, beginners
Published: 2018-11-06 16:48:02

---
title: Map, Filter and Reduce in Swift
published: true
description: Going further with collections by using swift functional capabilities
tags: swift, ios, mobile, beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/7nqxj0p1xqs1g2qnsmmm.png
---


Swift, like many modern languages, has implemented some beautiful, functional capabilities to its core. If you are not experienced with the functional paradigm, it's common to implement the classic for-loop for resolving the frequently seen problems around collection types, but that's not the Swiftiest way. So today, I'm going to introduce High Order Functions, which will let you simplify your code when a collection type joins in.

A Higher-Order Function is a function that receives other functions as arguments or a function that returns another function as its result. We'll be using Map, Reduce, and Filter in this post. Let's get started!

##Map

> In many programming languages, map is the name of a higher-order function that applies a given function to each element of a functor, e.g. a list, returning a list of results in the same order.
> Source: Wikipedia

Instead of writing a for loop and applying your business logic to each element, the map function takes care of that for you. It's only necessary to code the function that will be used to each element, saving us time and lines.

For example:

{% gist https://gist.github.com/eleazar0425/bebf8d8cd072bef9aefb2508444aa5b9 %}

Also you are able to transform your collection to another collection with a different type. 

{% gist https://gist.github.com/eleazar0425/557edf4d4a28023d864bb07ed10cd9e3 %} 

##Filter

> In functional programming, filter is a higher-order function that processes a data structure (usually a list) in some order to produce a new data structure containing exactly those elements of the original data structure for which a given predicate returns the boolean value true.
>Source: Wikipedia

The filter function iterates over a collection and then returns a new array containing those elements that have matched our predicate condition.

{% gist https://gist.github.com/eleazar0425/9a1868866afc5ffe4c88e3f2b33257f1 %}

##Reduce

> In functional programming, fold (also termed reduce, accumulate, aggregate, compress, or inject) refers to a family of higher-order functions that analyze a recursive data structure and through use of a given combining operation, recombine the results of recursively processing its constituent parts, building up a return value.
>Source: Wikipedia

Okay, that sounds too technical. Let me explain it in other words. What reduce function does is to combine every item of a collection in a unique value.

{% gist https://gist.github.com/eleazar0425/b89950dcbaac9a10bc9c6373cd0216fa %}

Let's see all of these together in action. Do you remember [my previous post](https://dev.to/eleazar0425/swift-pro-tip-of-the-day-extension-with-generic-where-clause-1e2e) where I was introducing a new way to solve a common problem? Now let's do that again but using what we just learned.

So, now instead of using the classic for-loop like this:

{% gist https://gist.github.com/eleazar0425/104b684ade20dda2180fc31989c29d87 %}

We could just reduce this to a one single line:

{% gist https://gist.github.com/eleazar0425/95afa1ece632fef066cb16af1b6c9bb7 %}

It's now more elegant than before.