How does this function retain a value - confused - swift

I'm learning closures... albeit slowly and I have come across the following example which provides an example of how closures 'close over' the variables/constants within their scope. I'll post the code first and ask my question after:
func countingClosure() -> () -> Int {
var counter = 0
let incrementCounter: () -> Int = {
counter += 1
return counter
}
return incrementCounter
}
let counter1 = countingClosure()
let counter2 = countingClosure()
counter1() // 1
counter2() // 1
counter1() // 2
counter1() // 3
counter2() // 2
I get the function block and can see how it returns the value it does. What is throwing me is how counter1 and counter2 retain their values, or, maybe, how the function retains the value (which I didn't think they could?). Surely every time counter1 or counter2 is called, counter resets to 0 as its declared inside the function?
I know it's right as the code compiles, I just can't understand how the Int value returned by counterClosure function is retained?

If you rename some of your names, perhaps it becomes a bit clearer:
func makeANewAutoIncrementingClosure() -> () -> Int {
// Initialize a variable, lives on stack initially but...
var counter = 0
let incrementingCounterClosure: () -> Int = {
// counter is captured and its storage is
// therefore moved from the stack to
// somewhere that hangs around as long as this function
// does
counter += 1
return counter
}
// Now we return this new closure, which is like
// [ function ]--attached to its closed over vars--> {counter: 0}
return incrementingCounterClosure
}
makeANewAutoIncrementingClosure // () -> () -> Int
// is a function that returns a closure that starts out closing over 0
let incr1 = makeANewAutoIncrementingClosure() // () -> Int
// a function that increments its captured int and returns it
let incr2 = makeANewAutoIncrementingClosure()
incr1() // 1 this does not call makeANewAutoIncrementingClosure, it calls the incr1 function (i.e. the incrementingCounterClosure). No assignments to 0 involved
incr2() // 1
incr1() // 2
incr1() // 3
incr2() // 2

Related

What does this function actually do?

i am currently trying to do some self learning in swift just for my own interest. in the course i bought it says that we should create a function similar to this one in order to solve my problem. but I'm blankly staring trying to figure out what this function actually does?
func unknown() -> () -> Int {
var x = 0
let z: () -> Int = {
x += 1
return x
}
return z
}
It is a function that returns another function which will return an integer that will be increased everytime you call it:
let afunc = unknown()
let value1 = afunc() // 1
let value2 = afunc() // 2
let value3 = afunc() // 3
The interesting part of this is the return type. () -> Int is a function that returns an Int, which means that unknown returns a function rather than something simple, like a number.
z is then a variable of that same type and is assigned a function definition to be returned.
If you assign the result of unknown to a variable, you can then invoke the returned function.
This implementation of a high order function is an interesting way of defining generators. An infinite sequence-like class would've achieve the same thing, but with more verbosity:
class MySequence {
private var x = 0
func unknown() -> Int {
x += 1
return x
}
}
var seq = MySequence()
let unknown = seq.unknown
print(unknown()) // 1
print(unknown()) // 2
print(unknown()) // 3
// ... and so on
The main difference between the class and the anonymous closure is the storage for x: the closure captures in due to using the variables within its body, while the class declares explicit storage for the property.
Some fancy stuff can result by using high order functions, like a generator for the Fibonacci numbers:
func fibonnaciSequence() -> () -> Int? {
var a = 0, b = 1
return { let c = a; a += b; b = c; return c }
}
let fibo = fibonnaciSequence()
while let f = fibo() {
// this will print forever
// actually not forever, it will stop at some point due to += overflowing
print(f)
}

Is there a way to retrieve directly the value returned from a closure in Swift, with type the return type of the closure and not: () -> Type

This is a question that aims merely at elegance, but is there a way to make something to the following code work in Swift? I know the code does not work, what I want is that the result of the code within the closure is stored in a constant. And the underlying theoretical issue is whether or not it is possible to retrieve the returned value from the closure with type Int and not with type () -> Int.
Thanks a lot for any help or comment!
let tableWithBooleans: [Bool] = Array(repeating: false, count: 10)
tableWithBooleans[0] = true
tableWithBooleans[5] = true
let numberOfTrue: Int = {
var result: Int = 0
for i in 0...9 {
if tableWithBooleans[i] {
result += 1
}
}
return result
}
// I want the code to compile and numberOfTrue to be a constant equal to 2
Use a high-order function instead
let numberOfTrue = tableWithBooleans.reduce(0) { $1 ? $0 + 1 : $0 }
Now if you still want to use your closure code then you should add a () after the closing } since you are calling the code inside {} as a function
let numberOfTrue: Int = {
var result: Int = 0
for i in 0...9 {
if tableWithBooleans[i] {
result += 1
}
}
return result
}()

Express for loops in swift with dynamic range

...or how can I use the index inside the for loop condition
Hey people
Since we're left with no c style for loops in swift 3 I can't seem to find a way to express a bit more complex for loops so maybe you can help me out.
If I were to write this
for(int i=5; num/i > 0; i*=5)
in swift 3 how would I do that?
The closes I came by was:
for i in stride(from: 5, through: num, by: 5) where num/i > 0
but this will of course iterate 5 chunks at a time instead if i being: 5, 25, 125 etc.
Any ideas?
Thanks
Using a helper function (originally defined at Converting a C-style for loop that uses division for the step to Swift 3)
public func sequence<T>(first: T, while condition: #escaping (T)-> Bool, next: #escaping (T) -> T) -> UnfoldSequence<T, T> {
let nextState = { (state: inout T) -> T? in
// Return `nil` if condition is no longer satisfied:
guard condition(state) else { return nil }
// Update current value _after_ returning from this call:
defer { state = next(state) }
// Return current value:
return state
}
return sequence(state: first, next: nextState)
}
you can write the loop as
let num = 1000
for i in sequence(first: 5, while: { num/$0 > 0 }, next: { $0 * 5 }) {
print(i)
}
A simpler solution would be a while-loop:
var i = 5
while num/i > 0 {
print(i)
i *= 5
}
but the advantage of the first solution is that the scope of the loop variable is limited to the loop body, and that the loop variable is a constant.
Swift 3.1 will provide a prefix(while:) method for sequences,
and then the helper function is no longer necessary:
let num = 1000
for i in sequence(first: 5, next: { $0 * 5 }).prefix(while: { num/$0 > 0 }) {
print(i)
}
All of above solutions are "equivalent" to the given C loop.
However, they all can crash if num is close to Int.max
and $0 * 5 overflows. If that is an issue then you have to check
if $0 * 5 fits in the integer range before doing the multiplication.
Actually that makes the loop simpler – at least if we assume that
num >= 5 so that the loop is executed at least once:
for i in sequence(first: 5, next: { $0 <= num/5 ? $0 * 5 : nil }) {
print(i)
}
For completeness: an alternative to the while loop approach is using an AnyIterator:
let num = 1000
var i = 5
for i in AnyIterator<Int>({
return i <= num ? { defer { i *= 5 }; return i }() : nil
}) {
// note that we choose to shadow the external i variable name,
// such that any access to i within this loop will only refer
// to the loop local immutable variable i.
print(i)
// e.g. i += 1 not legal, i refers to a constant here!
} /* 5
25
125
625 */
This method suffers from the same drawback as the while loop in that the loop "external" i variable persists outside and after the scope of the loop block. This external i variable is not, however, the i variable that is accessible within the loop body, as we let the loop body variable i shadow the external one, limiting access to i within the body to the immutable, temporary (loop scope local) one.

what do parenthesis do after lazy var definition?

I am analyzing analyzing some third party code and there is a "lazy" var statement that looks like this, and I would like to understand what the parenthesis are doing after the "computed property" curly braces:
lazy var defaults:NSUserDefaults = {
return .standardUserDefaults()
}()
The "return .standardUserDefaults()" is returning the NSUserDefaults instance object, so why add a () after the right curly brace?
thanks
It means that its a block that is executed the first time defaults is accessed. Without the () it means the defaults is a block type of variable of type () -> NSUserDefaults. When you add () it means it's just NSUserDefaults which is returned by the block executed at the time of access.
I came up with two examples. The first example is your typical computed property. It runs every time the variable is called.
var num = 0
var myName: String {
print(num)
return "xxx"
}
print(myName)
// 0
// xxx
num += 1
print(myName)
// 1
// xxx
The second example is a self-executing closure. As you can see, it only runs the print(num) the first time it is called.
var num = 0
var myName: String = {
print(num)
return "xxx"
}()
print(myName)
// 0
// xxx
num += 1
print(myName)
// xxx
To further illustrate, I've returned the num and see if it changes in a SEC. It doesn't. That means the block only runs the first time it is called, and assigns itself the return value thereafter. For all intents and purposes, after the first call, MyNum is now 0 and no longer a block.
var num = 0
var myNum: Int = {
print(num)
return num
}()
print(myNum)
// 0
// 0
num += 1
print(myNum)
// 0

Using a Closure as a While Loop's Condition

I want to use a closure as a condition for a while loop. This is what I have:
var x: Int = 0
var closure = {() -> Bool in return x > 10}
while closure {
x += 1
println(x) // never prints
}
It never prints anything. If I change it to closure(), it doesn't work either.
Any help would be appreciated.
There are two problems here.
Firstly, as written, your code won't even compile. You need to change while closure to while closure().
Second, the bigger problem, your closure logic is wrong. x > 10 never returns true because x is never greater than 10. Flip the sign over and it'll work.
Swift 2
var x = 0
var closure = { () -> Bool in return x < 10 }
while closure() {
++x
print(x)
}
Swift 1.2
var x = 0
var closure = { () -> Bool in return x < 10 }
while closure() {
++x
println(x)
}
Two issues:
Your logic is backwards. You want to print while x is less than 10, so:
when you call a closure directly you do so as you would a function, i.e. with parenthesis.
Updated code, tested with Swift 2.0:
var x: Int = 0
var closure = {() -> Bool in return x < 10}
while closure() {
x += 1
print(x)
}
You need to call the closure with (), and your condition is the wrong way around so that it's false at the beginning (it should be x < 10, not x > 10). Change to:
var x = 0
var closure: () -> Bool = { x < 10 }
while closure() {
++x
print(x)
}
Take advantage of abbreviation opportunities to make your code more concise and elegant.
var x = 0
let closure = { x < 10 }
while closure() {
x++
}
x // 10