avatarMotaz Majed "My To-Do List is Laughing at Me!!!"

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>

Why Using Positive Quotes Is a Game-Changer

My Journey with Positive Quotes

Photo by Alex Azabache on Unsplash

Have you ever wondered why some people seem to radiate positivity, even in the face of adversity, while others struggle to find that inner light?

I certainly did, and it led me on a journey, a journey filled with words of wisdom, inspiration, and the transformative power of positive quotes. Join me in unraveling the mysteries of how positive quotes can be a game-changer in our lives.

The Spark of Curiosity

One day, while sipping my morning coffee and scrolling through my social media feed, I stumbled upon a beautifully crafted quote:

“Your thoughts shape your reality.”

It was attributed to Mahatma Gandhi, a man whose life was a testament to the power of positivity.

Those words resonated with me, but as a self-proclaimed skeptic, I couldn’t help but question their validity. Can mere words have such a profound impact on one’s life?

Embracing the Experiment

Photo by Chase Baker on Unsplash

As a lover of experiments, I decided to embark on a journey.

I resolved to immerse myself in the world of positive quotes for an entire month, living and breathing the wisdom they offered.

My mission was clear: to find out if these succinct, often poetic, expressions of wisdom could genuinely bring about a transformation in my life.

Curious for more? Subscribing ensures you’ll never miss out.💙🔔📧

The Journey Begins

I started each day by selecting a positive quote, something that resonated with my current state of mind.

The process of choosing a quote itself was therapeutic, as it forced me to introspect and acknowledge my emotions.

These quotes acted as daily affirmations, setting the tone for my day.

Diving into Positivity

Photo by Alice Pasqual on Unsplash

Positive quotes became my companions, guiding me through both the mundane and challenging aspects of life.

I encountered quotes from historical figures like Helen Keller, who said,

“Life is a daring adventure or nothing at all,”

and contemporary thinkers like Maya Angelou, who reminded me,

“We may encounter many defeats, but we must not be defeated.”

Transforming Mindset

Slowly but surely, I noticed changes within myself. The once-doubtful skeptic began to embrace a more optimistic outlook.

Positive quotes were rewiring my thought patterns, redirecting them from negativity to hope.

The science behind this transformation lay in the power of neuroplasticity, the brain’s ability to adapt and change.

By consistently exposing myself to positivity, I was essentially rewiring my neural pathways.

Strengthening Relationships

Photo by Everton Vila on Unsplash

But the effects weren’t limited to my inner world.

The newfound positivity in my approach started to manifest in my interactions with others.

Quotes like

“In a world where you can be anything, be kind”

inspired me to be more compassionate, nurturing healthier relationships.

Building Resilience

Life, of course, is filled with challenges, and I faced my fair share during this experiment.

However, positive quotes acted as my armor, fortifying my resilience.

When faced with adversity, I remembered Winston Churchill’s words:

“Success is not final, failure is not fatal: It is the courage to continue that counts.”

A Paradigm Shift

As the days turned into weeks, and the weeks into a month, a profound shift occurred.

I began to see the world through a different lens.

What was once mundane became extraordinary, and every challenge an opportunity for growth.

The lens of positivity didn’t negate the existence of problems; rather, it revealed the potential for solutions.

Join the Experiment

So, here I am, not as a skeptic but as a believer in the transformative power of positive quotes.

My journey with these simple yet profound expressions of wisdom has turned me into an advocate, and I invite you to join me.

Explore the world of positive quotes, let their wisdom wash over you, and see how your life can be a game-changer.

The only thing you have to lose is negativity, and the world you stand to gain is brimming with possibility.

Want to be part of my readers’ circle? Subscribe for updates.💙💎

Conclusion

In the words of Albert Einstein,

“Imagination is more important than knowledge.”

Knowledge, the understanding that positive quotes can be a game-changer in our lives, is merely the first step.

The real magic happens when we harness our imagination and put these words into action. I leave you with this:

“The only limit to our realization of tomorrow will be our doubts of today.” — Franklin D. Roosevelt.

It’s time to shed those doubts and embrace the game-changing potential of positive quotes in your journey of life.

This content thrives on the backing of amazing readers like you. If you enjoyed it, please share it and subscribe to get all upcoming stories sent right to your inbox.

If you’re feeling extra generous and want to support my writing, consider becoming a Medium member! For just $5 a month

And hey, don’t forget you can also buy me a coffee! Your support truly means a lot.

Next To Read ..

Thanks for being amazing!

If you enjoyed this article, you can help me share this knowledge with others by:👏claps, 💬comment, and be sure to 👤+ follow.

Life
Life Lessons
Self Improvement
Illumination
Gamechangers
Recommended from ReadMedium