<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>iOS Developer</title>
        <link>https://paragraph.com/@ios-developer</link>
        <description>undefined</description>
        <lastBuildDate>Sun, 26 Apr 2026 00:46:24 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <image>
            <title>iOS Developer</title>
            <url>https://storage.googleapis.com/papyrus_images/fe182e3c2aa59b8f52cb5e53239ae18da99e0939a8708d608578f1a796e2adec.jpg</url>
            <link>https://paragraph.com/@ios-developer</link>
        </image>
        <copyright>All rights reserved</copyright>
        <item>
            <title><![CDATA[Higher-Order Functions in Swift]]></title>
            <link>https://paragraph.com/@ios-developer/higher-order-functions-in-swift</link>
            <guid>Z5nCXtAezeRtUHpxEAgh</guid>
            <pubDate>Mon, 23 May 2022 15:00:59 GMT</pubDate>
            <description><![CDATA[In previous articles, we’ve seen how to improve the architecture of our code through design patterns and the use of SOLID principles. Now we’ll see how to improve our Swift code with higher-order functions. Surely, you’ve used them more than once, but what are they and how can you improve your Swift code with higher-order functions?Swift Higher-Order FunctionsHigher-order functions are functions that take other functions or closures as arguments and that return a function or a closure. These ...]]></description>
            <content:encoded><![CDATA[<p>In previous articles, we’ve seen how to improve the architecture of our code through <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://medium.com/be-developer/design-patterns-in-software-b5fc4baa9595">design patterns</a> and the use of <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://medium.com/better-programming/solid-principles-application-to-swift-development-1de8d7c57fdf">SOLID principles</a>. Now we’ll see how to improve our Swift code with higher-order functions. Surely, you’ve used them more than once, but what are they and how can you improve your Swift code with higher-order functions?</p><h2 id="h-swift-higher-order-functions" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Swift Higher-Order Functions</h2><p><em>Higher-order functions</em> are functions that take other functions or closures as arguments and that return a function or a closure. These functions are used with arrays, sets, and dictionaries and act on the elements they contain (this is done by methods that are applied to the elements of the collection using the point syntax).</p><p>Some of the best-known functions are <code>map</code>, <code>compactMap</code>, <code>flatMap</code>, <code>reduce</code>, <code>filter</code>, <code>contains</code>, <code>sorted</code>, <code>forEach</code>, and <code>removeAll</code>.</p><h2 id="h-map" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘map’</h2><p>The <code>map</code> function works by performing an operation on all the elements of a collection and returning a new collection with the results of that operation.</p><p>For example, let’s suppose we have an array with several words with all their letters in lowercase, and we want to obtain a new array with each of these words but with all uppercase letters. We could do this with a <code>for</code>…<code>in</code> loop:</p><p>Let’s see how to do it with the <code>map</code> function. As shown in the <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://developer.apple.com/documentation/swift/array/3017522-map">Apple documentation</a>, it’s declared as:</p><p><code>transform</code> accepts an element from a collection or a sequence as a parameter and returns a transformed value of the same type or a different one.</p><p>In the example, we’re seeing we apply it as follows:</p><p>What it does is loop through the entire array of elements, apply the <code>uppercased()</code> method on each of them, and return a new array with these values.</p><p>Anyway, we can reduce this expression by using the shorthand argument <code>$0</code>, which refers to any elements of the array:</p><h2 id="h-compactmap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘compactMap’</h2><p>Now suppose that inside the array of the previous example there are <code>nil</code> values. If we use the function, we must take into account whether the value to be acted on is <code>nil</code> or not:</p><p>But what if what we really want is to get the new array but without the <code>nil</code> values? To achieve this we have the <code>compactMap</code> function.</p><p>Therefore, in the example, we’re seeing:</p><p>In other words, <code>compactMap</code> loops through all the elements of the array and applies the method to non-<code>nil</code> values, returning them in an array, in this case of type <code>String</code> (that is, the value of <code>String</code> isn’t optional).</p><h2 id="h-flatmap" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘flatMap’</h2><p>The <code>flatMap</code> function allows us to transform a set of arrays into a single set that contains all of the elements. As Apple declares in its <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://developer.apple.com/documentation/swift/sequence/2905332-flatmap">documentation</a>:</p><p>For example, let’s first look at how we’d do it without <code>flatMap</code>:</p><p>But with <code>flatMap</code>, we can simplify the code as follows:</p><h2 id="h-reduce" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘reduce’</h2><p><code>reduce</code> is a function that, when applied to a collection, returns the result of combining the elements of that collection:</p><p>For example, if we have an array with the numbers 1-10 and we want to obtain their sum, we can do it in the following way without using the reduce function:</p><p>With <code>reduce</code>, we simply do the following:</p><p>In the first iteration, the value of <em>x</em> is 0 (as we’ve indicated in the function), and the value of <em>y</em> is 1. So the result of* x + y* will be 1. In the second iteration, <em>x</em> is 1, and <em>y</em> is 2. So the result will be 3. And so on.</p><p>In a more simplified way, we can write:</p><h2 id="h-filter" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘filter’</h2><p>As its name suggests, the <code>filter</code> function filters the content of a collection and returns a new collection that contains the elements that meet a certain condition:</p><p>For example, suppose we have the word collection from the first examples and we want to get a new collection of words containing the letter <code>o</code>. Without the filter function, we could do it in the following way:</p><p>In this case, we haven’t used the <code>contains</code> function since, as we’ll see later, it’s also a higher-order function.</p><p>Now we’re going to simplify this code using the <code>filter</code> function:</p><p>But it doesn’t have to apply a single condition; we can apply several conditions. For example, in the previous case, we can make it return the words that contain the vowel <code>o</code> and whose length is <code>5</code> characters or more:</p><h2 id="h-contains" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘contains’</h2><p>In the previous example, we’ve used the <code>contains</code> function to determine if a word contained the vowel <code>o</code>. Well, <code>contains</code> is a higher-order function that allows you to check if there are elements that meet a certain condition and return <code>true</code> or <code>false</code> depending on whether or not they meet it.</p><p>As Apple indicates in its <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://developer.apple.com/documentation/swift/array/2945493-contains">documentation</a>, <code>contains</code> returns a boolean value that indicates whether the sequence contains the given element.</p><h2 id="h-sorted" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘sorted’</h2><p>On numerous occasions, we find a collection of elements we want to order in some way to show them. For example, in the word-array examples seen so far, those words aren’t arranged alphabetically.</p><p>But what if we wanted to order them alphabetically? We could use some algorithm, like:</p><p>We could reduce this code using the <code>swapAt</code> method, which allows us to exchange the positions of two elements in a sequence:</p><p>To further reduce this code, we can use the <code>sorted</code>* *function. This function returns the elements of a sequence ordered in ascending order (as long as the elements of the collection adopt the <code>Comparable</code> protocol):</p><p>On the other hand, if we want to use our own condition to order the collection, we use the function <code>sorted(by:)</code>, which, as Apple indicates in its <a target="_blank" rel="noopener noreferrer nofollow ugc" class="dont-break-out" href="https://developer.apple.com/documentation/swift/array/2296815-sorted">documentation</a>, returns the elements of the sequence, ordered using the predicate given as the comparison between elements.</p><p>For example, if we want the words to be ordered in reverse-alphabetical order, we can do the following:</p><p>In this case, by indicating the symbol <code>&gt;</code>, we order the collection in descending order.</p><h2 id="h-foreach" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘forEach’</h2><p>Neither <code>continue</code> nor <code>break</code> can be used inside <code>forEach</code>, just <code>return</code>:</p><h2 id="h-removeall" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">‘removeAll’</h2><p>The higher-order function <code>removeAll(where:)</code> allows us to remove elements from a sequence that meet certain conditions:</p><p>For example, if we want to remove all the even numbers from a sequence, we can do the following using <code>removeAll</code>:</p><p>The power of <code>removeAll(where:)</code> is seen more clearly in the example that Apple shows in its documentation, where it uses it to remove the vowels of a phrase:</p><h2 id="h-function-concatenation" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Function Concatenation</h2><p>The first-order functions can be applied consecutively, concatenated. For example, we can take an array containing arrays of numbers and calculate their sum:</p><p>First we apply the <code>flatMap</code> function to obtain an array with all the numbers. Then we apply the <code>reduce</code> function to add them.</p><h2 id="h-conclusion" class="text-3xl font-header !mt-8 !mb-4 first:!mt-0 first:!mb-0">Conclusion</h2><p>We’ve just seen some of the most commonly used higher-order functions and their power by using some examples. These functions allow us, on the one hand, to reduce the amount of code and, on the other, to make our code clearer and more concise.</p>]]></content:encoded>
            <author>ios-developer@newsletter.paragraph.com (iOS Developer)</author>
            <enclosure url="https://storage.googleapis.com/papyrus_images/48867307d051994393ba95a2db17db084e0eaa319fa6aa635f6257525c1c75b5.jpg" length="0" type="image/jpg"/>
        </item>
    </channel>
</rss>