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 trialGavin Hobbs
5,205 PointsIs Something Wrong With the Exercise?
Either I'm missing something or this exercise is faulty, here's my code...
func map<T, U>(array: [T], transformation: (T) -> U) -> [U] {
return transformation(array)
}
func map<T, U>(array: [T], transformation: (T) -> U) -> [U] {
return transformation(array)
}
1 Answer
andren
28,558 PointsNo, the challenge works, but the description might be a bit misleading.
It asks you to apply the function passed in on the array, but if you look at the signature of the function you will see that it is designed to take an element T
not an array of element T
. It also returns U
and not an array of U
which is what the map
function is meant to return.
Because of this you can't just pass the array itself into the function. You have pull the items out of the array and call the function on each of the elements, add them to an array of type U
, and then return that array.
I'd suggest taking another stab at the challenge with the above info in mind. If you are still struggling to get past the challenge then I can post the solution for you along with some comments explaining the code.
Michael Hulet
47,913 PointsMichael Hulet
47,913 PointsHey andren, thanks for this awesome explanation! Please don't post, copy/pastable answers to challenges, however. If you need to show code to illustrate concepts, that's awesome and it'll be a great way to demonstrate what you're saying, but please make sure that it can't be copied and pasted into the challenge and pass unmodified. Thanks for helping out in the community!
Gavin Hobbs
5,205 PointsGavin Hobbs
5,205 PointsHi Michael Hulet, I would kindly disagree with your view on pasting the code.
My first reason for my opposing opinion would be that Pasan always reveals how he solved a problem in the videos. He doesn't just give you the necessary concepts and leave you hanging to solve it by yourself. He does encourage you to solve it by yourself but he always provides the solution.
My second reason is that, often, the best way to learn something is to try and try and try... and if you still can't get it right, look at the answer and reverse engineer it to find the method for finding the solution. Then you move onto the next problem and attempt to apply the method you learned on the last problem. If you continue this, eventually you will understand the concepts and how to apply them... and more than likely you'll remember it better than if you had just figured it out on your first try.
Additionally, the students here at Treehouse are here to learn... not to copy and paste in the code in the challenges so they rack up points next to their avatar. I don't think the students, who pay for this educational resource are interested in just taking something as it is, and trying to get through the course. I mean if you complete a course and come away from it not understanding anything, then what's the point? That would just be a waste of time... and a waste of money.
I really like what andren did here (thanks andren!) the concepts were provided but the solution wasn't posted; however, it was offered to me if I couldn't figure it out from the concepts... the same thing Pasan has been doing.
Maybe I'm wrong about all this and these are stupid reasons.
Thanks for your reflection on this subject.
Gavin Hobbs
5,205 PointsGavin Hobbs
5,205 PointsThanks so much, andren! I figured it out!