4. What does the compiler translate a for comprehension with yield into?
- A while loop
- Java bytecode directly with no method calls
- Calls to map, flatMap and withFilter
- Recursive functions only
Show answer
Answer: Calls to map, flatMap and withFilter
Generators and guards desugar into flatMap, map and withFilter, so any type with those methods works.
Lesson: Conditionals, Loops and for Comprehensions
6. What does a by-name parameter `action: => A` do?
- Delays evaluating the argument until it is used, possibly several times
- Evaluates the argument once before the call
- Makes the parameter optional
- Passes the argument by reference like C++
Show answer
Answer: Delays evaluating the argument until it is used, possibly several times
By-name arguments are evaluated each time they are referenced inside the method.
Lesson: Functions as Values