Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Generics in Swift Generic Functions, Parameters and Constraints Protocol Based Type Constraints

NIKOLA RUSEV
NIKOLA RUSEV
5,293 Points

second chalenge??

i try to solve second chalenge but i cant. and i find the code that pass in comunity but i still have some confusions.

  1. every func that will return a square of some int or double will be return also int or double and requarement in chalenge was (T)->U which means T and U should be defferent type

so this func have no sense. func map<T, U>(array: [T], transformation: (T) -> U) -> [U] { var newArray = U for values in array{ newArray.append(transformation(values)) }

return newArray  

}

is there something that i missed??

The two generic types are T and U, they can be any type and the same if you want but I believe they were trying to make you use different ones to prove a point that you can have as many type parameters as you wish. What they were trying to show you is the flexibility of generics, so rather having to change the map function to cater for every type you can pass in any closure/function that conforms to transformation parameter and it will produce the required result. Here is an example of a function returning square numbers that are of type Double and another one summing the values. Its a bit of a confusing question but hopefully I've helped lift some of the mystery behind it.

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

    for item in array {
        transformedArray.append(transformation(item))
    }

    return transformedArray
}

func square(number: Int) -> Double   {
    return Double(number * number)
}

func sum(number: Int) -> Int    {
    return number + number
}

let numbers = [1,2,3,4,5]

map(array: numbers, transformation: square)
map(array: numbers, transformation: sum)

Its a weird one to wrap your head around, I could be wrong but I believe this challenge is actually mirroring the map closure that Apple provides which I have included below as a reference.

let stringNumbers = numbers.map { String($0) }
print(stringNumbers)

Hopefully I haven't confused you :)

2 Answers

Xavier D
PLUS
Xavier D
Courses Plus Student 5,840 Points

used a while instead of for

func map<T,U>(array:[T],transformation:(T)->U)->[U]
{
    var repeatwhile=0;var returnArray:[U]=[]
    repeat{returnArray.append((transformation(array[repeatwhile])))
    repeatwhile+=1}while repeatwhile<array.count
    return returnArray
}

func squaring(_ int:Int)->Int{let squared=int*int;return squared}

map(array: [1,2,3,4,5], transformation: squaring)