avatarSalam Khan

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>

My Writing Journey: From Nowhere to Now Here

Thank you Mediumers (and Grammarly)

“I went for years not finishing anything. Because, of course, when you finish something you can be judged.” — Erica Jong

Image by Angela Yuriko Smith from Pixabay

I am not scared of judgment anymore.

I started my career as a Technical Writer. I wrote those long boring user manuals nobody ever read. I also wrote software requirement specification, technical specification, and other similar documents. All went well for the first three to four months until a UK-returned-dude, who became the manager of my manager, joined our office in Pakistan, and started noticing mistakes in my write-ups. Instead of giving my manager or me concrete feedback so that I could improve, he started disliking me. I didn’t cope well with it, as I was just a nobody — a fresher, and I quit. Since then, I hated writing as I was not good at it, and because English is my 4th language.

My next few jobs were not writing-related, and things were going smooth until I decided to be a blogger for a medium-sized blog. My daily tasks included testing various software tools, mobile apps, and Operating Systems, and then write articles about that. Soon, the editor of the blog started complaining about my approach and suggested it wasn’t good enough. I had to move on.

For the next two years, my jobs didn’t require me to write much so that anybody could criticize it. But I kept reading.

Since I started writing, people kept telling me my writing sucked. Even though I gave up on writing, I never stopped reading. That helped me immensely.

I gave up on writing as a hobby or full-time profession after that. I wrote a few pieces here and there but could never get going. Credit of me restarting to write and finding what I love to write about goes to Medium and Grammarly. In November 2019, I got Medium’s yearly membership and started reading more and more. Every day during my commute to work and back, I read. Soon enough, I knew I love reading various things on Medium. I was still not confident about writing.

And then came the Grammarly ad, on youtube. So much I hated it, I bought the yearly subscription in anger, this February. It’s not perfect as it’s just a tool, but I use it everyday as the first line of defence against my grammar errors.

As I wrote in my introduction article, my father was a poet. I always knew I could write poetry. Unlike my other write-ups, poetry comes naturally to me, without much struggle. I started giving it a go on Medium, this March and since then, there’s no stopping.

  • Thank you Joel Mwakasege, You were the first editor to add me as a writer and publish my posts for a medium publication — Be Yourself — I will never forget that.
  • Dr Mehmet Yildiz I am so thankful to you for adding me as an Editor and writer for ILLUMINATION. It gave me exposure to some of the great writers, who I proudly call friends now.
  • Thanks to S. Stefan Karabacak for adding me as a writer for a Few Words where I love publishing my poetry.
  • Last but not least, the spirits in me always kept flapping their wings but could never take off until Simran Kankas added me as a writer for Spiritual Tree, for which I’m eternally grateful to Simran.

“I can shake off everything as I write; my sorrows disappear, my courage is reborn.” — Anne Frank

I am aware it’s a long and hard journey to be a good writer: as long as I keep writing. And when I’ve writing-travel-companions like George J. Ziogas, Shin Jie Yong, Tom Byers, Harley King, Gillian Annie, Mary Holden, Paul Myers MBA, Joe Luca, Holly Catherine, Cat, Catherine Delia, David, Dr Ron Pol, Pierre Trudel, Lanu Pitan, R Tsambounieri Talarantas, 🌲Andrea D'Angelo🌲, Somsubhra Banerjee, Tim Maudlin, Amy Marley, Gurpreet Dhariwal, Michael Ritoch, Chris Hedges, Arthur G. Hernandez, Lori Brown, Livia Dabs, Desiree Driesenaar, Chrissie Morris Brady Ph.D., Sherry S, Alison Hannah, René Junge, Ksenia Sein, Besom & Bletherskite, Indra Raj Pathak, Aurora E, it’s an enjoyable one.

Thank you my friends, and everyone else for reading, clapping, responding to my articles, and supporting me in my writing journey.

I love you all!

P.S. If you’re a writer and would like to write for an up-and-coming publication, talk to me, I’ll add you as a writer for ILLUMINATION and introduce to some fantastic humans with diverse background, and exceptional skills.

Follow it nonetheless for great content.

About the Author Salam is a polyglot poet by passion and an Agile Coach and Delivery Consultant by profession. He writes about Love, Spirituality, Heartache, Agile, Management, and Life.

Writing
Self Improvement
Medium
Communication
Personal Development
Recommended from ReadMedium