Treehouse Intermediate Course - Part 4 (Generics 2)

The run continues! Though just a short-ish entry today as I only have around half an hour. More time tomorrow, then nothing for Friday through to Sunday! But the main thing is I have momentum! Let's crack on with generics!

Start Time - 10:08

Code challenge -



Wow this looks hard! Let's try breaking it down...

OK, so we have a [T] type and the transformation of (T) -> U - these need to be in the parameters. The return is type U. The return is then a square function.

Let's try some things out! Opening a playground!

func map<T, U>(array: [T], transformation: (T) -> U) -> U {
    

}

That seems to make sense so far! For the next bit though....just gonna check back the previous video as the transformation part has flummoxed me!

Ah ok, I think I'm on to something. I need to do a separate function for the square, then put that into the transformation bit....

func map<T, U>(array: [T], transformation: (T) -> U) -> U {
    return transformation(array)
}

func square(_ number: Int?) -> Int {
    guard let value = number else { fatalError() }
    
    return value * value
}

A good try but not quite! Let's get some help....

This is proving hard finding any sort of help! Will keep going....

func map<T, U>(array: [T], transformation: (T) -> U) -> [U] {
    var mapArray: [U] = []
    
    for item in array {
        mapArray.append(transformation(item))
    }
    
    return mapArray
}

Wow that was hard! I went down the wrong route by creating a separate function - felt like I was misled a little in Pasan's video! Then the whole transformation bit, needing to have the transformation within the for loop bit....very tricky stuff! Anyway, moving on with one last bit for today!

Protocol Based Type Constraints

func findKey<Key, Value: Equatable>(for value: Value, in dictionary: Dictionary<Key,Value>) -> Key? {
    for (iterKey, iterValue) in dictionary {
        if iterValue == value {
            return iterKey
        }
    }
    return nil
}

let airportCodes = ["DXB": "Dubai International", "LHR": "London Heathrow Aiport"]

findKey(for: "Dubai International", in: airportCodes)

All of this is very technical and will need some going over but I am keeping up!

Ok some of my own practice.....

let f1Driver: [F1Team: String] = [.ferrari: "Leclerc"]

findKey(for: "Leclerc", in: f1Driver)

That's really cool! The great thing with generics is you can use ANY type and it will figure it out. I like that. 

The equatable is a protocol that has been put in. It is the contract put in....still need to practice protocols more!

Here is Pasan's example - tricky stuff!

That is a good place to stop! I know I haven't covered as much as I'd like to, but I've got to go. If I'm not back tooo late this evening, I might have another look. If not, tomorrow is cool as there's plenty of time then! The key thing here (pun not intended) is that I will get my head around this stuff, before moving on to the next Generics parts. There are actually several more sections of generics, which is good as I'm still at the early stages of conceptualising them!

Finish Time - 10:59 (51 minutes)

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)