Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialjohn lau
3,471 Pointserror: type '(T) -> U' has no subscript members return transformation[array]
no really sure what they're asking
func map<T,U>(array: [T], transformation: (T) -> U) -> [U] {
return transformation[array]
}
6 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi John,
I'm not sure why you've changed your function signature since that was correct in your original question. The function signature you just posted is different from what the question asks for and so even if your code worked perfectly, it wouldn't conform to the requirements of the question.
Let's just assume you've fixed your function signature. As I said before, the body of your function has to apply transformation
to every element in array
. Your function totally ignores transformation
and multiplies the value in array by itself. That's fine (though unnecessary) if transformation
just happens to be a squaring function but since this is challenge is explicitly a test of generics, you've got to be able to handle any function that takes a single input and produces a single output. The reason you have transformation
as an argument to your function is so that you can use it in your function body.
In the Multiple Type Parameters video (the video immediately before this challenge), Pasan explains and demonstrates the exact thing you are being asked to do to every element in your array (his transform
function). You should make sure you understand what this function is doing, and why because you are going to see functions of this form all the time in Swift. If after rewatching the video you still don't fully understand this form of generics, then describe what part of the concept is not making sense. Right now it's impossible to give you any really useful pointers because it's not clear what you understand and what you don't.
You should carefully read this Stack Overflow article on how to ask a good question. It will really enable you to articulate the specifics of where you are struggling so that we can provide you with useful help.
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi John,
Your function signature is correct, so you have to think about what kind of behaviour the function should have that would turn your inputs into your output.
You know that you are going to be taking an array that contains elements of type T. You also know that you you are taking in a function that itself takes T and returns U. Finally, you know that your output is going to need to be an array filled with elements of type U.
Put another way, you want to write a function that will take each element of your input array, then you want to apply transformation
to that element, stick the result of that back into an array, and then when you've gone through all the elements in your input array and thus finished building up your output array, return that array.
So what programming construct would you use to repeatedly perform a identical action on each element in array? How about a for
loop?
Hopefully that will give you a nudge in the right direction. If you're still stuck, let me know.
Cheers
Alex
john lau
3,471 Pointsthks Alex I know all that, my question is why I can't pass the challenge with this answer.
Best
Alex Koumparos
Python Development Techdegree Student 36,887 PointsBecause you haven't actually implemented the function. All you've done is write a function signature and a return statement (and once you've implemented the substance of the function, you'll see why your return statement isn't right).
Like I said before, you need to fill in the body of the function with something that will actually work through each element in your input array, applying transformation
to it, building up an output array, then returning the output array.
john lau
3,471 PointsHi,
func trans<T,U>(array:T, numb:(T) -> [U]) -> [U] {
var numb:[Int] = []
for multiplier in array {
let result = multiplier*multiplier
numb.append(result)
}
return numb as! [U]
}
cheers
john lau
3,471 Pointswhen I'm trying to pass the array into the function
func map<T,U>(array: [T], transformation: (T) -> U) -> [U] {
let value = transformation(array) return value }
it gives me that
swift_lint.swift:5:13: error: '([T]) -> U' is not convertible to '(T) -> U' let value = transformation(array)
Alex Koumparos
Python Development Techdegree Student 36,887 PointsRight.
Because transformation
takes T and you are giving it [T].
You need to apply transformation
to every element T in the array of [T].
What programming construct do you use to iterate through all the elements of an array? Hint: I said it in my original answer to your question.
Inside that programming construct you will have an element from the array of type [T] and that element will be of type T. And function transformation
can take a T.