DELAPARRA
Index
Color
NL
Category: Design patterns

Iterator

Explained by Updated

What is Iterator?

Iterator: Provide a way to access elements of a collection sequentially without exposing the underlying data structure. The Iterator pattern gives a standard way to go through the items of a collection one by one, without knowing how the collection stores them. The same foreach works for an array, a database result or a file read line by line.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Iterator

How is Iterator used in practice?

PHP supports it with the Iterator and IteratorAggregate interfaces, and generators with yield make writing iterators easy. JavaScript has iterators and for...of.

What should you keep in mind with Iterator?

The real power is lazy processing. A generator can read a two-gigabyte CSV one line at a time, keeping memory use tiny. Laravel's lazy() and cursor() do the same for database results. Whenever you process large data sets, an iterator that hands out one item at a time is usually better than loading everything into an array first.

At a glance

CategoryDesign patterns
Explained by
Updated

More Design patterns terms

All terms starting with I

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.