Quick Language – Does Chaining Higher-order Functions Mean Weaker Performance?

For more precision, let's take a look at the following example:

Consider that we have the following table of Ints:

leave table = [1, 2, 3, 4, 5]

and we were asked to generate a new table from board corresponding to the following two conditions:

  • The element in board should be> 3.
  • The new generated element must be a string instead of int, in the form: "this is (element * 2)".

Following the approach of linking higher order functions, we could achieve it like this:

let newArray = array.filter {$ 0> 3} .map {"This is  ($ 0 * 2)"}

At this point, he should go through all the elements to filtered and then reiterate once again through the filtered elements for the map.

However, when using the standard for-loop:

var transformed = [String]()
for i in 0 .. 3 {
transform.append ("This is  (array[i] * 2) ")

he should go through board elements for one time.

As far as I know, using higher order functions leads to suppressing mutability (we have stated newArray as let instead of var).

In such a case, what better for the performance?