Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Iterator
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Iterator
(section)
Add topic