Back to questions
JuniorTheoryCommon
What does the program print?
let numbers = [1, 2, 3]
let result = numbers.filter { $0 < 3 }.map { $0 * 10 }
print(result)[10, 20]
- Expecting
[30]: thinkingfilterremoves the elements for which the closure returnstrue. It keeps them, so1and2stay and are multiplied. - Expecting
[10, 20, 30]: thinking each call in the chain works onnumbers.mapgets the result offilter,[1, 2], so only two numbers are multiplied.
- What does the program print if you swap
filterandmap? - How would you get the same result with one
for-inloop?