Advanced Usage of Functional Methods

In the last post at reducing-code-for-readability-and-speed which took you through using functional methods instead of loops. The issue was that in some of these functions you might require the index and therefore pushed one back to using loops instead.

Let’s look at

Consider the code below, it iterates through the array and returns the array with every alternative value doubled.

now, if we simply wanted to double each value, then we could have used the map function as

However in this case, it would mean that we need an index to determine if the item is the next one or not.

What about other functions?

Let’s look at the same array and say we want to sum every other element, it would be quite easy if we wanted to sum all the elements by simply using

But back to the problem we are trying to resolve, sum every alternative number, that would put us to using

Is there a better way?

Swift has a lot of functions and methods around collections, sequences, etc

this is similar to the for loop earlier but this time we also get the index for free if we use enumerated() which returns a Sequence with two elements, the offset and the element. The offset is what we are referring to as the index as in this case.

so we can now use our mapping function from the first paragraph, where we were attempting to double every other element in the array.

now, let us look at our other example where we were attempting to sum every other element

So there we have it…

This is something that I was blissfully unaware of, did not play much with Sequences and Collections. However I had used it only with the for loops not with the functions, but the introduction to this opens many doors (so as to say). Have fun with this…

Views All Time
Views All Time
Views Today
Views Today
Posted in Article, Basics, Tip, Tutorial.