avatarHelen Olivier (AuDHD)

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3392

Abstract

er">17</span>]</pre></div><blockquote id="c020"><p>Compact Map()</p></blockquote><p id="b1c2"><code>compactMap</code> function is similar to <code>map</code>, but it also includes an additional step, it filters out any <code>nil</code> values that result from applying the transformation closure. This is particularly useful when you have an array of optional values and you want to transform them while simultaneously filtering out the <code>nil</code> elements.</p><div id="9cf9"><pre><span class="hljs-keyword">extension</span> <span class="hljs-title class_">Array</span> { <span class="hljs-keyword">func</span> <span class="hljs-title function_">compactMap</span><<span class="hljs-type">T</span>>(<span class="hljs-keyword">_</span> <span class="hljs-params">transforms</span>: (<span class="hljs-type">Element</span>) -> <span class="hljs-type">T</span>?) -> [<span class="hljs-type">T</span>] { <span class="hljs-keyword">var</span> result <span class="hljs-operator">=</span> <span class="hljs-type">T</span> <span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span> { <span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> element <span class="hljs-operator">=</span> element { result.append(transform(element)) } } <span class="hljs-keyword">return</span> result } }</pre></div><p id="fe9c">Here the only change from <code>Map</code> and <code>Compact Map</code> is avoiding the nil value with a condition.</p><p id="3b42">In the above code</p><ul><li>It takes a closure <code>transform</code> as an argument, which specifies how each element should be transformed.</li><li>Inside <code>compactMap</code>, a new array <code>result</code> is created to store the transformed non-nil elements.</li><li>It then iterates over each element of the original array (<code>self</code>), applies the transformation closure to each element, and checks if the result is non-nil.</li><li>If the result is non-nil, it appends the transformed element to the <code>result</code> array.</li><li>Finally, it returns the <code>result</code> array containing all the non-nil transformed elements.</li></ul><blockquote id="9909"><p>How to consume it?</p></blockquote><div id="41f1"><pre><span class="hljs-keyword">let</span> mapArray <span class="hljs-operator">=</span> [<span class="hljs-number">23</span>, <span class="hljs-number">43</span>, <span class="hljs-number">56</span>, <span class="hljs-literal">nil</span>, <span class="hljs-number">75</span>, <span class="hljs-number">9</span>, <span class="hljs-number">14</span>] <span class="hljs-keyword">let</span> result<span class="hljs-operator">=</span> mapArray.compactMap { <span class="hljs-variable">$0</span> } <span class="hljs-built_in">debugPrint</span>(result) output: [<span class="hljs-number">23</span>, <span class="hljs-number">43</span>, <span class="hljs-number">56</span>, <span class="hljs-number">75</span>, <span class="hljs-number">9</span>, <span class="hljs-number">14</span>]</pre></div><blockquote id="854c"><p>Flat Map()</p></blockquote><p id="b656">FlatMap is typically the same as <code>map</code> does, only difference is that it always return flatten array joining all elements. Typically used to collect specific data in n

Options

ested array, dictionary or set</p><div id="6431"><pre><span class="hljs-keyword">extension</span> <span class="hljs-title class_">Array</span> { <span class="hljs-keyword">func</span> <span class="hljs-title function_">flatMap</span><<span class="hljs-type">T</span>>(<span class="hljs-keyword">_</span> <span class="hljs-params">transform</span>: (<span class="hljs-type">Element</span>) -> [<span class="hljs-type">T</span>]) -> [<span class="hljs-type">T</span>] { <span class="hljs-keyword">var</span> result <span class="hljs-operator">=</span> <span class="hljs-type">T</span> <span class="hljs-keyword">for</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">self</span> { result.append(contentsOf: transform(element)) } <span class="hljs-keyword">return</span> result } }</pre></div><p id="75fa">In the above code</p><ul><li>It takes a closure <code>transform</code> as an argument, which specifies how each element should be transformed into a sequence.</li><li>Inside <code>flatMap</code>, a new array <code>result</code> is created to store the flattened elements.</li><li>It then iterates over each element of the original array (<code>self</code>), applies the transformation closure to each element, and concatenates the resulting sequences into the <code>result</code> array.</li><li><code>append(contentsOf: )</code> will add the elements of a sequence to the end of the array.</li><li>Finally, it returns the <code>result</code> array containing all the flattened elements.</li></ul><blockquote id="2285"><p>How to consume it?</p></blockquote><div id="117f"><pre><span class="hljs-keyword">let</span> arrayOfArrays <span class="hljs-operator">=</span> [[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], [<span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], [<span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]] <span class="hljs-keyword">let</span> transformedArray <span class="hljs-operator">=</span> arrayOfArrays.flatMap { <span class="hljs-variable">$0</span> } <span class="hljs-built_in">print</span>(transformedArray) output: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]</pre></div><h1 id="3bda">Conclusion</h1><p id="057c">There are plenty of other use cases for higher-order functions. This is a gist of what we have discovered today:</p><ul><li>If you need to simply transform a value to another value, then use <code>map</code>.</li><li>If you need to remove nil values, then use <code>compactMap</code>.</li><li>If you need to flatten your result one level down, then use <code>flatMap</code>.</li></ul><p id="0a10">Thank you for your time and attention! 👏👏👏</p><p id="33e6">Do clap👏 if you like this and comment your suggestions!!! <i>Happy coding</i>!!!</p><blockquote id="1e0c"><p>Source Code: <a href="https://github.com/Vikassingamsetty/HigherOrderFunctions.git">GitHub</a></p></blockquote></article></body>

Author: JillWellington on Pixabay

How These 3 Questions Changed My Life

I was sitting in the office of my therapist. We were talking about my problem with negative thinking. No matter how I tried, my first thoughts about any subject were negative ones, based on trauma and fear.

He told me that he was giving me homework. A simple task. Just three questions. Three questions I ask myself and answer every day. And the promise behind those three questions was immense. He said that they would rewire my brain to think positively.

OK, I probably can do this, I thought hesitantly. I am not good with regular activities. But this seems quite painless and simple enough. So, we proceeded to the questions:

  • What made you happy today?
  • What have you achieved today?
  • What have you learned today?

He asked me to write the answers down every evening. It could be just tiny, little things. He then promised that answering these questions will change the neural pathways in my brain to more positive thinking after only 21 days. Apparently, it takes 21 days to form a habit — including the habit of thinking about the world more positively.

I decided to give it a try. Every evening I said down with a journal and started searching my mind.

The first question was quite easy for me. I may be a negative thinker, but I am a joyful negative thinker. I know it’s a bit of an oxymoron, but people are that complicated. So I was able to spot many little joys throughout the day. The flower I saw blooming on the side of the street. A happy dog that I could pet. Somebody who smiled at me. Sunshine on my face.

The second question was harder. I don’t have much self-esteem, and it took me some time to be able to appreciate the little things I did. Even tiny achievements like washing the dishes or cooking a healthy meal. And to be honest, I am still learning to appreciate my small successes to this day. But the important thing is that I can find and acknowledge them when I search for them.

The third question was the hardest. What have I learned? Some days, I study, and I learn plenty of new things. But some days are just the usual daily routine. Nothing much educational about it. Or maybe it should be normal to be eager to learn new things routinely? Maybe it should. Maybe it’s a good habit to develop.

My therapists explained that by answering these questions, I would not only focus more on the positive things throughout the day but also start to create the situations that lead to them.

But I must admit that without my very curious boyfriend who is always willing to learn anything that comes his way, I would be a little lost here. A lot of my “What I learned” answers sounded a bit forced — it felt like writing something down just for the sake of writing it down. I did it anyway.

Just because something is such a tiny thing that it flies under my radar doesn’t mean that I should not try to acknowledge it. Maybe that’s a point as well — to learn that nothing is too small.

I wrote all the answers down every evening. Not only for the required 21 days — I continued and journaled about the positive aspects of my days for over a year. And by that time, spotting the beautiful things, the good things, the things that made me smile, just became my second nature. A mentality that was not determined by the negativity of my childhood, but one I have built for myself with my own actions.

I can now go about my day and see, really see, those little happy things. They really are everywhere if you know how to look. And I learned to appreciate even the small achievements and be glad for them. My answers to those three questions will never again be “nothing.” Because now I see the world with new eyes. And I am much happier for it.

If you enjoyed this article, you might also like this one about how writing letters of support to other people helps me with my depression:

Or this one about how dreams feel when they come true:

Want more? Subscribe to Of Heart and Soul, my newsletter that delivers right into your mailbox a spoonful of self-improvement and mental health, a dash of writing tips, a pinch of positivity and a healthy dose of a sense of wonder.

Neuroplasticity
Positive Thinking
Change Your Life
Life Hacking
Positive Stories
Recommended from ReadMedium