*Consolidation Entry* Closures Part 2

Here we go - back to two entries in a row! As I mentioned, Ramadan is about to begin, which means I'm around a calendar year from on where the 'relaunch' of my coding journey began. In fact, let's just take a little trip back to this time last year....

In fact that was 4th June! At that point, I went back to basics and started again using Treehouse to go through all of the content. Before that, towards the end of December in 2017, I made a go at a relaunch there, but it was pointless, as were the several entries in between then and June. So really, it's been 11 months. In that time, I really have achieved a lot but should NOT be too hard on myself with what I CAN'T do. Let's just focus on what I am able to do! Not going to do a break down now - will save that for a month when it is a full year! So for now, I just want to cap off closures...

Start Time - 20:37

Just to go over the very last bit from before -



This was the creation of a closure. So specifically, we have the name of a constant, with the type and return type declared (although return type is technically empty) - then the parameter 'name' being used and put within the closure - the block of code in the curly brackets.

Right, name is a local variable.



This is even more of a shorthand way. No local variable being used or the keyword 'in'. Instead we have the $0 than the local variable with string interpolation.





Do I agree with this? Yes I think! The first part means code within code e.g. the adding of curly brackets with a block of code to a constant or variable. That is the closure part. The parameters part is making more sense and the having a type including the closure parameters...making more sense!



OK it's getting there but this is still confusing....


I'm going to leave that for now. It's just a bit too much techno jargon. Going to try one more video for now...

https://medium.com/ios-os-x-development/basic-closures-in-swift-for-newbies-7319344b3d2b


A different website first! The point he makes is that there are no extra powers with closures. The code though is short, readable and easy to debug.

So with a function it very simple -

func sayLannisterName(firstName: String) -> String {
    
    return ("Hi \(firstName) Lannister!")
}


sayLannisterName(firstName: "Tyrion")


OK so that is a good, direct comparison but I am at a VERY basic level still!

var sayStarkName = {(firstName: String) in return "Hi \(firstName) Stark"}

sayStarkName("Ned")

https://www.youtube.com/watch?v=SUZJfaQ3-hE

Now let's check this out!

No use whatsoever

https://www.youtube.com/watch?v=fVF_tNcIhfc

func filterGreaterThanValue(value: Int, numbers: [Int]) -> [Int] {
    
    return []
}

let filteredList = filterGreaterThanValue(value: 3, numbers: [1, 2, 4, 5])



Simple example here - no actual block of code yet!

func filterGreaterThanValue(value: Int, numbers: [Int]) -> [Int] {
    
    var numArray: [Int] = []
    
    for number in numbers {
        if number > value {
            numArray.append(number)
        }
    }
    
    return numArray

}

Ok I figured all of the out no problem - all pre closures though!

A key point is that every time I would want to modify the value, or the rule then it is fiddly. 

OK, so the idea below is that a CLOSURE has been used as part of a function...

func filterWithPredicateClosure(closure: (Int) -> Bool, numbers: [Int]) -> [Int] {
    return []
}


Here is the full thing now - 

func filterWithPredicateClosure(closure: (Int) -> Bool, numbers: [Int]) -> [Int] {
    var filteredNumbers: [Int] = []
    for num in numbers {
        if closure(num) {
            filteredNumbers.append(num)
            
        }
    }
    return filteredNumbers
}

let filteredListWithClosure = filterWithPredicateClosure(closure: { (num) -> Bool in return num < 5 }, numbers: [1, 4, 6, 7, 8])

To break this down, the closure parameter is used as part of the function. The closure created as the block of (Int) -> Bool, which is then followed in the creation of the closure above. OK making slightly more sense!

OK that is cool!

func greaterThanThree(num: Int) -> Bool {
    return num > 3
}

let newList = filterWithPredicateClosure(closure: greaterThanThree, numbers: [1, 4, 7, 8])


So I've created a function for finding if a number is greater than 3 or not. This function is put into the closure block because it also follows the syntax of (Int) -> Bool. 

Ok that was quite useful! Thank you Brian Voong from Let's Build That App! That did help a bit.

Finish Time - 21:42 (1 hour 5 minutes total)

Well that was worth going over, even though my knowledge of closures is very limited. I'm going to continue with Angela's course next time and see what I can learn more about - it's going to be technical no doubt and I will have to be honest to pause and check things where necessary. Good to have 2 consecutive days and get some proper coding in!

Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)