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 trialTrevor Duersch
9,964 PointsIs flatMap deprecated?
Is flatMap depcreated? I am currently in the video on closures and reviewing the "Flat Map" video - https://teamtreehouse.com/library/flat-map . I got a warning saying that flatMap was deprecated and I should use compactMap. Once I changed the code from "flatMap" to "compactMap", the functionality was still there and the warning went away.
2 Answers
Caleb Kleveter
Treehouse Moderator 37,862 PointsCorrect. In essence, flatMap
was just renamed to compactMap
because it makes more sense. From this method, you only get the non-nil results of the closure passed in. nil
is left out of the resulting array.
Omar De Yamlor
10,980 Points@Caleb : not really the same result, it doesn't flatten the array of array to an array. In the video example, the type of tags is :
-
[[String]]
when usingcompactMap
-
[String]
when usingflatMap
Caleb Kleveter
Treehouse Moderator 37,862 PointsflatMap
is deprecated for operations that returned an optional. Previously you could use .flatMap
and if the closure returned an optional, any nil
values would not appear in the resulting array. This was simply renamed to compactMap
.
In the case that you are talking about, that is correct, you use flatMap
to convert [[Element]]
to [Element]
.