Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does passing an unnamed function as transform to array add one to iteration count in playground in furthest abstractions

Tags:

swift

I'm in the process of getting comfortable passing unnamed functions as arguments and I am using this to practice with, based off of the examples in the Swift Programming Guide.

So we have an array of Ints:

var numbers: Int[] = [1, 2, 3, 4, 5, 6, 7]

And I apply a transform like so: (7)

func transformNumber(number: Int) -> Int {
    let result = number * 3
    return result
}

numbers = numbers.map(transformNumber)

Which is equal to: (7)

numbers = numbers.map({(number: Int) -> Int in
    let result = number * 3
    return result;
    })

Which is equal to: (8)

numbers = numbers.map({number in number * 3})

Which is equal to: (8)

numbers = numbers.map({$0 * 3})

Which is equal to: (8)

numbers = numbers.map() {$0 * 3}

As you can see in the following graphic, the iteration count in the playground sidebar shows that in the furthest abstraction of a function declaration, it has an 8 count.

enter image description here

Question

Why is it showing as 8 iterations for the last two examples?

like image 279
Logan Avatar asked Jun 03 '14 03:06

Logan


1 Answers

It's not showing 8 iterations, really. It's showing that 8 things executed on that line. There were 7 executions as part of the map function, and an 8th to do the assignment back into the numbers variable.

It looks like this could probably provide more helpful diagnostics. I would highly encourage you to provide feedback via https://bugreport.apple.com.

like image 176
Dave DeLong Avatar answered Sep 22 '22 13:09

Dave DeLong