Intro to Higher Order Functions in Swift 3

The Wild Edition: Lio The Lion

Roar. Noticed.


Today is another day. Another great day. Another great day for some Swift action. But not just any other day. Today we are going into the jungle. Prepare yourself for some Swift jungle wilderness.

I watch some random animals fight on YouTube recently and hence this article takes place in the jungle.

Keep it light and chill. Enjoy the journey.

Topics that we will cover are higher order functions with map, filter, reduce, and putting all three together.

Welcome to The Wild Edition: Lio The Lion.


Highly recommend that you have a solid understanding of functions and closures before diving in. If you do get stuck anywhere in this article, hopefully these articles can prove worthy for you. So definitely check any of these articles out if you are not confident with them yet.

Master Functions in Swift 3
Introduction to Closures In Swift 3


Hungry lion wants food. Food is depleted. Only 1 of 3 is left of his species. The earth, messed up. Hungry lion. Lio. Lio is his name. Lio is a hungry lion. He will eat anything. Anyone.

Laying down in the green-less jungle, looking more like a desert. Dry and leafless. Lio hibernates with his right eye closed. Left eye open… waiting. Waiting to kill. So comes the human. So comes Bon.

Oh Bon. What are you doing here?

Lio has targeted his prey. A flesh. He opens his right eye. Watches Bon. Stalks Bon. Stretches his back. Lowers his stance. Moves left from a far distance to fall behind Bon’s line of sight. Lio moves. Moves forward. Still low and moving. Still eyeing his prey with the steadiness of a sniper.

Lio is behind Bon. Bon hears the beating heart of hungry king behind him. Bon turns around.

High Five.


Lions are close to extinction. Some places, they are extinct. Now it’s time to treat each and every of our lion friends better. Keywords — each and every.

Map — each and every

Say the lions’ well being is calculated by HP with 0 being worst and 100 being best.

var HPs = [50, 60, 70]

Now we increase each and every lion’s HP by 10. { $0 + 10 }

The above is most succinct.

Here is most to least verbose: { (HP: Int) -> Int in return HP + 10} ) { (HP: Int) in return HP + 10} ) { HP in return HP + 10} ) { HP in HP + 10} ) { $0 + 10} ) { $0 + 10 }


Now that we have treated lions better, the lions reproduce and we are up 5 lions now.

The lions are of different ages. The young ones seem to be really interested in learning to make iOS games. So let’s do a filter to find the young ones.

Filter — meet condition

Say young is below 5.

var ages = [2, 4, 6 , 8, 10]

Now let’s filter.

ages.filter { $0 < 5 }

The above is most succinct.

Here is most to least verbose:

ages.filter( { (age: Int) -> Int in return age < 5 } )
ages.filter( { (age: Int) in return age < 5 } )
ages.filter( { age in return age < 5 } )
ages.filter( { age in age < 5 } )
ages.filter( { $0 < 5 } )
ages.filter { $0 < 5 }


The lions have reproduced. The number of lions has gone up to 5 thanks to the collaboration of the beautiful readers like you. Male and female. Beautiful. The lions now wants to know how much the 5 of them weighs together because they are curious.

Reduce — all to one

The curiosity came from the cubs’ physics simulation lab.

Here are their weights:

var weights = [100, 200, 300, 400, 500]

Lions are super healthy now. Some of them have been putting on quite some muscles or belly muscle.

weights.reduce(0, +)

The above is most succinct.

Here is most to least verbose:

weights.reduce(0, { (result: (Int, Int)) -> Int in return result.0 + result.1 } )
weights.reduce(0, { result -> Int in return result.0 + result.1 } )
weights.reduce(0, { result in return result.0 + result.1 } )
weights.reduce(0, { result in result.0 + result.1 } )
weights.reduce(0, { $0 + $1} )
weights.reduce(0, +)


After all these random act of kindness from the earthlings, each of the 5 lions’ velocity has increase by a tremendous 20 mi/hr.

After their speed increase, we want filter down to over 50 mi/hr.

Then add those together. {$0 + 20}.filter {$0 > 50} .reduce(0, +)


With The Wild Edition: Lio The Lion, we saw how we can use higher order functions with map, filter, reduce, and putting the three all together.


I hope you have enjoyed and learned something valuable from my article. If you have then let me know by hitting that ❤ button and follow me on Medium. And, share this article so that your circle can make some knowledge gains too.

For those interested, here is my LinkedIn.

Lastly if you have any comment, question, or recommendation, feel free to drop them below. Tell me what you want to learn next. I’m writing for you guys!



Lilit Badeyan for her excellent editing skills.

Medium Recommends and LinkedIn Messages

Thank you all for your incredible support.

Medium Responses from Last Article

Annabelle, Ayaz Akbar, hasan, Ousmane Younoussa Abdou