Generators Flashcards

1
Q

How are generators useful?

A

They provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a generator do?

A

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory (this saves you from exceeding a memory limit, or requiring a considerable amount of processing time to generate).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between a generator and a normal function?

A

Instead of returning once, a generator can yield as many times as it needs to in order to provide the values to be iterated over.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does a generator function return?

A

An object that can be iterated over.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you iterate over a generator object?

A

PHP will call the generator function each time it needs a value, then saves the state of the generator when the generator yields a value so that it can be resumed when the next value is required.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can a generator return a value?

A

No. Doing so will result in a compile error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is an empty return statement valid syntax within a generator?

A

Yes. It will terminate the generator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between yield and return?

A

Return stops execution of a function and returns. Yield provides a value to the code looping over the generator and pauses execution of the generator function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

If you use yield in an expression context (for example, on the right hand side of an assignment), you must…

A

…surround the yield statement with parentheses. For example, this is valid:

$data = (yield $value);

But this is not:

$data = yield $value;

This syntax may be used in conjunction with the Generator::send() method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Do yield statements work with associative arrays?

A

Yes. In addition to yielding simple values, you can also yield a key at the same time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Yielding a key/value pair in an expression context requires…

A

…the yield statement to be parenthesised.

$data = (yield $key => $value);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What happens when yield is called without an argument?

A

It yields a NULL value with an automatic key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can generators yield values by reference?

A

Yes. It’s done in the same way as returning references from functions: by prepending an ampersand to the function name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is returned when a generator function is called for the first time?

A

An object of the internal Generator class. This object implements the Iterator interface in much the same way as a forward-only iterator object would.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How are generators superior to implementing an Iterator class?

A

Much less boilerplate code has to be written, and the code is generally much more readable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are generators inferior to implementing an Iterator class?

A

Generators are forward-only iterators, and cannot be rewound once iteration has started. This also means that the same generator can’t be iterated over multiple times; the generator will need to either be rebuilt by calling the generator function again, or cloned via the clone keyword.