Higher-Order Function in Swift
come let’s become lazier.
Don’t ignore the passages like your crush ❤️ignoring you.
What is the higher-order function?
It’s basically to make developer lazier than we are now or in other words, we can say Higher-order functions are function that already built-in
For sorting an array without any higher-order function, we use at least for loops with 6 or more lines that but with a higher-order function can do it in a single line with using just sort(by:>)
Note: I used fully shorthand method to explain here and I don't want you to make lazier, so I don’t upload any code in GitHub for you to copy and paste
Map :
What is the map function?
The map will loop over the collection and apply same operation in an array
let's start with comparing examples (like comparing traditional way with higher-order function way)
After applying the higher-order function
you can feel the difference from image size itself
- Here
$0
Swift’s closures shorthand notation map
will loop over the collection and apply the same operation($0 * 10)
in an array and return an array
(NOTE: inside the {$0 }
we can use any type of operation or even other higher-order function in it )
Map with a dictionary
After applying the higher-order function (SPD emergency )
.values
for selecting values of the dictionary.keys
for selecting key of the dictionary
Flat Map
The
FlatMap
is basically just like.map()
, except it filters out anynil
values.
After applying the higher-order function. Image size speaks bro
I have one unknown use of FlatMap it is used to split the strings in an array
(Note: Don’t ask this traditional way code, I'm that much lazier)
- it will split the strings and ignores the NIL
CompactMap
Returns an array result of calling the given transformation with each element in the sequence.
Here is the use of CompactMap
It is used to take out Int values from an array of string and more
After applying the higher-order function
similarly, we can also do this for string
Filter
What is the filter function?
filter
to loop over a collection and return anArray
containing only those elements that match an include condition.
After applying the higher-order function
- Now filter will be checking like
{1 % 2 == 0}
here we will get False and the filter will not add the value to evenArray if true will be adding it
Let’s get all the numbers less than 5:( other usages)
Filtering Array of string using Filter
- The filter will be check like
{"Abc".contains("Ab")
now this will return true (if returns false filter will ignore the value )so the filter will be adding the value into filteredArray
contains : Returns a Boolean value indicating whether the sequence contains the given element.
Filter on the dictionary
After applying the higher-ordered function
HERE $0 is stands for key
$1 is stands for value
Filtering with an array of objects
step 1: creating an array of objects
(Note: Don’t get confused both are same code )
Step2: creating an array of abject using a struct, let's use a filter on it
$0.rate
to access the rate object.$0.booksName
will access thebooksName
array
Reduce
What is Reduce function?
The reduce function allows you to combine all the elements in an array and return an single value
After getting more laziness (Applying the higher-order function)
- The first argument
(1)
always represents the partial result and not an element in the collection. - Then it’s like
1 * 2
2*3
and so on
Another way of using reduce is like
reduce(1,{$0 *$1})
with shorthand argument
- Here 1 again represents the partial result
- $0 -> it’s for starting value (it’s like n the current value)
- $1-> it’s for so on values (it’s like n the next value of current value )
For addition, we need to change the symbol (+)
and The first argument
You know what we doing here
Some untold uses
max & min:
To find the maximum element in the sequence.
Much better always use .max
to avoid more stress
just .max()
is enough to find maximum element in the array
as? Int ?? 0
is to unwrap the optional
same like for minimum
After applying min
function
Split :
split a string into pieces based on a given condition
In this, we separate all the " ”
, but at some time we don’t need to split all the " ”
so we can use maxSplits
split using shorthand argument
abs: (Be positive)
To change the negative values into positive values
If you pass positive value means will return a positive value
The reality, crush ignores even if you found the solution for COVID19..!
“ Every programmer is an author. “ — Sercan Leylek
Find it a good read?
Recommend this post (by clicking 👏 a button) so other people can see it too…