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 CSS Background Blend Modes Challenge

Sergio Alen
Sergio Alen
26,726 Points

Code works in xcode playground but not in the challenge

Not sure what I'm doing wrong, I keep getting the error "Make sure your second argument has the external name transformation and type (T) -> U", it works fine in Xcode but it doesnt get accepted in the challenge.

Here's my code:

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

func toSquare(_ arrayToSquare: [Int]) -> [Int] {
    var returnArray = [Int]()
    for i in arrayToSquare {
        returnArray.append(i * i)
    }
    return returnArray
}

var challengeArray = [1,2,3,4,5]
map(array: challengeArray, transformation: toSquare)

2 Answers

Chris Stromberg
PLUS
Chris Stromberg
Courses Plus Student 13,389 Points

You need to check your code here:

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

The transformation argument type does not match what the challenge asked for.

transformation: ([T]) -> [U] 

Is not the same type as :

transformation: (T) -> U

Also the challenge is asking you to create a func with two generic parameters:

In the editor define a function named map with two generic type parameters, T and U. The function takes two arguments - array of type [T], and transformation of type (T) -> U. The function returns an array of type U.

The goal of the function is to apply the transformation operation on the array passed in as argument to return a new array containing the values transformed.

Within your func you need to loop thru the generic array [T] and apply a transformation to each element. The type (T) -> U would be applied to each of your elements.

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

    var transformedArray = [U]()

    for i in array {
        // Here we take an array and append the result of  (T) -> U for each element 
        // in the array provided.
        transformedArray.append(transformation(i))
    }

     // Then we return the new array of generic [U]
    return transformedArray
}
Sergio Alen
Sergio Alen
26,726 Points

Legend. Thanks Chris

Deysha Rivera
Deysha Rivera
5,088 Points

Thank you for your explanation. I'm not sure how I was supposed to know from the description that I needed to use a for in loop to answer this. I don't think the clues were super clear on how to write the body of the function.