*Consolidation Entry* Closures Part 1
*A HUGE gap since the last coding session. Various reasons why - work, personal...and to be honest, the idea of getting back on it hasn't appealed as much because of the time trickling along. Anyway, I have to take ownership for that lack of commitment, accept it and move on! The longest gap between entries for around a year is nothing to be proud of and I will endeavour not to let it happen again. In fact, Ramadan is about to commence so the PERFECT opportunity to get stuck back in and make up for lost time!
So as promised, rather than cracking on with Angela's course, it makes more sense at this point to go over closures. It is something that still boggles me and before going on with any new content, I want to understand them better!
*This does NOT mean that I'm having Angela's course - I will come back to it! I'm going to go over Bob Lee's points on closures - see what I can glean and then go back to Angela's. Let's go!
Start Time - 11:37
So the key questions - why do we use closures? My understanding is that they are like functions but the code of them is a bit simpler.

So that's the simple definition and a little bit about it.
So as promised, rather than cracking on with Angela's course, it makes more sense at this point to go over closures. It is something that still boggles me and before going on with any new content, I want to understand them better!
*This does NOT mean that I'm having Angela's course - I will come back to it! I'm going to go over Bob Lee's points on closures - see what I can glean and then go back to Angela's. Let's go!
Start Time - 11:37
So the key questions - why do we use closures? My understanding is that they are like functions but the code of them is a bit simpler.

So that's the simple definition and a little bit about it.
func addTwoNumbers(number1: Int, number2: Int) -> Int {
return number1 + number2
}
addTwoNumbers(number1: 4, number2: 2)
So a quick example of creating an adding function and using it.
Right let's have a look at syntax for closures. Here is Bob's first example -
var addClosures: (Int, Int) -> Int = {
(number1: Int, number2: Int) in return number1 + number2
}
addClosures(3, 2)
So in terms of syntax this looks rather verbose to me. Let's break it down to make proper sense of it.
The var keyword is used instead of func
After the name of the closure, the two input types (parameters) are put in brackets (Int, Int)
The return type with the arrow is then put
Then we have = and the curly brackets opening
That's where the parameter names and types are put in
Then we have in return as key words
Then the actual functionality of number1 + number2
Then close curly bracket
That is a LOT! I believe Bob will then simplify this...
var addClosures2 = {
(number1: Int, number2: Int) in return number1 + number2
}
Already that is simpler - no declaration part for the type etc! So Swift can infer that.
var addClosures3 = {
(number1: Int, number2: Int) in number1 + number2
}
Simpler still - Swift infers that you are returning. So no need for the return keyword.
And here we go with a more detailed declaration but very, very simple closure block!
var addClosures4: (Int, Int) -> Int = { $0 + $1 }
So you need to define a type....or make the closure block more detailed. Or both!
Right here is a string function -
func callString() -> String {
return "I am a string"
}
Let's try making this as a closure without Bob first!
var callStringClosure: () -> String = { () in return "I am a string" }
Yes! I thought it hadn't worked but did.
Right so again, return word not needed.
Also, no parameters - so that can change...
var callStringClosure3: () -> String = {"I am a string"}
Wow this next one really is simpler. No parameter - Swift infers the rest!
var callStringClosure4 = {"I am a string"}
So at this stage, I've learned that closures can be simplified from functions, then simplified themselves. The functionality is essentially the same as a lot of inference from Swift can be included. Let's do one more bit with Bob for now!
So news from Bob. Function is a GLOBAL CLOSURE!
So really functions are TYPES OF CLOSURES! Hmmm interesting!!
A bit of rambling about problems then into the key part - can you pass functions into functions?
Right this is higher level stuff - this is for INDIRECTLY including closure
func returnClosure() -> ((Int, Int) -> Int) {
return addClosures
}
returnClosure()
We have here a function that returns a closure! But no actual functionality is returned, just the template of the block itself.
let returnedClosure = returnClosure()
returnedClosure(4, 3)
OK, so I have assigned a constant to the function and then put in the numbers.
THE WHOLE point of this is that the return type is a closure. Rather than the code.
func returnClosureDirectly() -> ((Int, Int) -> Int) {
return { (number1: Int, number2: Int) in number1 + number2 }
}
This means creating the closure within the return block. This is directly returning it. Of course that is all very verbose. Now to try with the dollar signs...
func returnClosureDirectly2() -> ((Int, Int) -> Int) {
return { $0 + $1 }
}
NAILED IT!
The last bit is confusing - this is probably where, the first time round back in November/December, I started to give up on this course. So to sum up the waffle, the direct/indirect element to it is either putting in a pre-created function/closure (indirect) or to actually code it for the first time (direct).
Well, that's it from Bob for now - his stuff just becomes too hard to follow after that initial introduction part. I'm looking online for a few supplementary bits before finishing this entry for today...
https://www.weheartswift.com/closures/
So a but if general info -
So we touched on global before. Nested is a function within a function. The last one - the expression is where I know very little about!
More info -
The general syntax for declaring closures is:
{ (
}
parameters
) -> return type
instatements
}
If the closure does not return any value you can omit the arrow (
->
) and the return type. This also applies to the case where the type of the closure can be infered.
{ (
}
parameters
) instatements
}
So with this, the closure is within the curly brackets. We also start with parameters, then the statements. If there is no return value, then no return type or arrow needed. Inference is key!
Some of this may be outdated - such as the sort function. Let's just try one more website before leaving closures for now...
https://learnappmaking.com/closures-swift-how-to/
OK, this one is from 2018 so more relevant hopefully!
Closures are blocks of code that you can pass around in your code, as if you assign a function to a variable.
Another definition! This one states passing around and assigning a function to a variable. Right..
The example is the "Happy Birthday" one. A key thing is the block of code is in the curly brackets and uses the assignment operator (=) -
So far, so simple...
So we have here now the type in brackets for the parameter, the return is in empty parentheses and then we have the variable (name) with keyword in included. THEN we have in the print line, a reference to the the variable - name.
There is a closure type here - (String) -> ( )
Three essential things -
- The closure type
(String) -> ()
- The closure expression
{ name in ... }
- The closure call
birthday(...)
Finish Time - 12:58 (1 hour 21 minutes)
Ok this is all still very complex. But I'm getting it! I'm going to do ONE more closure entry, contenting with this site before resuming with Angela's course. Closures seem so important, I want to crack them!
Comments
Post a Comment