Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Iterator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Implicit iterator== Some object-oriented languages such as [[C Sharp (programming language)|C#]], [[C++]] (later versions), [[Object Pascal|Delphi]] (later versions), [[Go (programming language)|Go]], [[Java (programming language)|Java]] (later versions), [[Lua (programming language)|Lua]], [[Perl]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]] provide an [[Intrinsic function|intrinsic]] way of iterating through the elements of a collection without an explicit iterator. An iterator object may exist, but is not represented in the source code.<ref name=ExternalInternal /><ref name="HFDPIterators">{{cite book | last1 = Freeman | first1 = Eric | last2 = Freeman | first2 = Elisabeth | last3 = Kathy | first3 = Sierra | last4 = Bert | first4 = Bates | editor-last = Hendrickson | editor-first = Mike | editor2-last = Loukides | editor2-first = Mike | year = 2004 | title = Head First Design Patterns | volume = 1 | pages = 338 | publisher = O'REILLY | format = paperback | isbn = 978-0-596-00712-6 | access-date = 2012-08-09 | url = http://shop.oreilly.com/product/9780596007126.do }}</ref> An implicit iterator is often manifest in language syntax as <code>[[foreach]]</code>. In Python, a collection object can be iterated directly: <syntaxhighlight lang="python"> for value in iterable: print(value) </syntaxhighlight> In Ruby, iteration requires accessing an iterator property: <syntaxhighlight lang="ruby"> iterable.each do |value| puts value end </syntaxhighlight> This iteration style is sometimes called "internal iteration" because its code fully executes within the context of the iterable object (that controls all aspects of iteration), and the programmer only provides the operation to execute at each step (using an [[anonymous function]]). Languages that support [[list comprehension]]s or similar constructs may also make use of implicit iterators during the construction of the result list, as in Python: <syntaxhighlight lang="python"> names = [person.name for person in roster if person.male] </syntaxhighlight> Sometimes the implicit hidden nature is only partial. The [[C++]] language has a few function templates for implicit iteration, such as <code>for_each()</code>. These functions still require explicit iterator objects as their initial input, but the subsequent iteration does not expose an iterator object to the user.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)