1

Here is the shot of the playground where I am trying to print all the even numbers until you reach an odd one in 3 ways and I don't understand why there are different number of times it looped

enter image description here

From my understanding it should loop only 7 times because there are 6 even numbers in the starting and the 7th time there is an odd number which must terminate the loop but we have gotten 8 times in one case ! Why ?

Also quite literally the second and third way are same but still there is a difference in the number of times the loop was executed.

This, I don't understand.

===== EDIT =======
I was asked for text form of the code so here it is

import UIKit

// First way
var arr = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix { num in
    return num.isMultiple(of: 2)
}
print(arr)

// Second way
var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})
print(arr2)

//Third way (almost second way but directly prinitng this time)
print([34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
    .prefix(while: {$0.isMultiple(of: 2)}))
2
  • Can you also add the code in text form, so that we can easily copy and paste it? Commented Mar 9, 2020 at 18:23
  • alright ! will do it Commented Mar 9, 2020 at 18:25

2 Answers 2

1

The message of "(x times)" isn't really saying how many times a line is executed in a loop.

Think of it like this, sometimes the playground want to output multiple messages on the same line, because two things are being evaluated on that line. This happen the most often in loops, where in each iteration a new thing is evaluated, and Xcode wants to display that on the same line, but can't, so can only display "(2 times)".

You can also see this happen in other places, like closures that are on the same line:

let x = Optional.some(1).map { $0 + 1 }.map { String($0) } // (3 times)

Here, the 3 things that are evaluated are:

  • x
  • $0 + 1
  • String($0)

Another example:

func f() { print("x");print("y") } // (2 times)
f() 

Normally, each print statement will have some text displayed on the right, won't they? But since they are on the same line, Xcode can only show "(2 times)".

The same thing happens in this case:

var arr2 = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30].prefix(while: {$0.isMultiple(of: 2)})

There are 8 things to evaluate. The closure result is evaluated 7 times, plus once for the value of arr2.

If you put the variable declaration and the closure on separate lines, then Xcode can show the value of the variable on a separate line, which is why the number is one less in the two other cases.

To illustrate this graphically:

enter image description here

You are forcing Xcode to combine the two boxed messages into one single line. Xcode can't usefully fit all that text onto one single line, so the best it can do is "(8 times)".

Sign up to request clarification or add additional context in comments.

1 Comment

That was great! But I don't think people can easily discover what you said especially with the current question's context ? I should've asked something like what does ( x times) mean in x code ?
1

I believe these are just artifacts of how playgrounds work. With a little re-formatting I get "7 times" in all three variations:

let data = [34, 92, 84, 78, 90, 42, 5, 89, 64, 32, 30]
let arr = data.prefix {
    $0.isMultiple(of: 2)
}
print(arr)

let arr2 = data.prefix(while: {
    $0.isMultiple(of: 2)
})
print(arr2)

print(data.prefix(while: {
    $0.isMultiple(of: 2)
}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.