is there anyway to increment exponentially in swift?
问题 I am trying to write a for loop, where I have to increment exponentially. I am using stride function but it won't work. Here's c++ code, I am trying to write a swift version. for (int m = 1; m <= high - low; m = 2*m){} can you help me, to write this code in swift version? 回答1: A while-loop is probably the simplest solution, but here is an alternative: for m in sequence(first: 1, next: { 2 * $0 }).prefix(while: { $0 <= high - low }) { print(m) } sequence() (lazily) generates the sequence 1, 2,