+ 1

Iterators, generators and array-like structures

Can someone confirm my general understanding of these data structures, from my limited use of Python and JavaScript: [updated description] iterators are classes that are more fundamental than array, etc. and define methods for accessing and moving through data; generator (objects) are a subclass of iterators instanciated by generator functions (using yield); other iterable data structures like arrays and lists inherit and adapt iterator methods. Instances of iterators and generators can be created without creating arrays, lists, etc. [1st attempt] iterators are known locations in memory, generators describe how the data is produced and assigning an iterator to an array or dictionary or similar container allows us to use the properties and methods of an array/dictionary/etc. on the data. Is that [updated description] accurate? Am I missing something fundamental?

22nd Aug 2019, 1:03 PM
David Crowley
David Crowley - avatar
10 odpowiedzi
+ 3
A deeper understanding constructs may help
23rd Aug 2019, 12:55 PM
Da2
Da2 - avatar
+ 2
Kindly read more on loops, iteration and recursive constructs...
23rd Aug 2019, 3:44 PM
Da2
Da2 - avatar
+ 2
You need to get your terminology right. Please don't use the word "algorithm" in this context. An algorithm is just the idea for the actual implementation of a program or part of program. What (I think) you mean is either a function or a method (a function bound to an object or class). And "data" can be anything. In python both terms (iterator/generator) are more or less used interchangeably because generators are way more common than iterators that are not generators. There are two ways to create a generator. You can use a generator function (with "yield") or a generator expression ("for variable in iterable"). An example for a generator expression would be this: new_generator = (element for element in range(10)) You can reach the same goal with a generator function: def gen(): i = 0 while i < 10: yield i i+=1 new_generator = gen() to be continued later (gtg)
23rd Aug 2019, 4:14 PM
Thoq!
Thoq! - avatar
+ 1
Control & data structures are the foundation of algorithms
23rd Aug 2019, 2:15 PM
Da2
Da2 - avatar
+ 1
Here is a nice explanation for Python: https://stackoverflow.com/questions/2776829/difference-between-pythons-generators-and-iterators and wikipedia has a more general explanation: https://en.wikipedia.org/wiki/Iterator P.S.: I don't understand Da2's answers either.
23rd Aug 2019, 2:34 PM
Thoq!
Thoq! - avatar
+ 1
Da2 , I understand your answer (algorithms define operations on data) but without seeking significant information outside this thread, I would never have been able to see any link between it and my initial question or the follow-up question, specifically the NATURE of iterators and generators with respect to data structures like arrays and linked lists. Fortunately, I have read more, and I think I do see the link! (and the massive misconceptions in the initial question). What do you think of this? Iterators are defined methods to access and traverse (move from element to element) data stored in containers (arrays, dictionaries, etc.) Each container has defined interator methods. [edit: so the link with Da2's answer is that an iterator is a set of algorithms for accessing and moving through the data structure] ... Still working out what generators are, but I gather that they are also methods on the data structures that use the iterators. Ref.: https://en.m.wikipedia.org/wiki/Iterator
23rd Aug 2019, 2:40 PM
David Crowley
David Crowley - avatar
+ 1
Hmm... Thoq! That stackoverflow thread adds some depth. Iterators are not just algorithms (instructions for accessing and traversing data) that are inherited by arrays, dictionaries, etc. The terms are also used to refer to objects/instances/data created by using the algorithms independently of their use within arrays, lists, etc. Generators are functions that use "yield" (Python) to produce an iterable generator object, and are a subclass of Iterators. Especially with generators, we rarely distinguish the two with more explicit language - Generator function versus Generator object - which adds to the confusion "algorithm or data?"
23rd Aug 2019, 3:23 PM
David Crowley
David Crowley - avatar
+ 1
Da2 very Yoda-like of you, lol. You provide so little, but you do give a nudge in a handy direction... that will require significant effort on my part but will undoubtedly give me a much deeper ultimate understanding.
23rd Aug 2019, 3:56 PM
David Crowley
David Crowley - avatar
0
Da2 any references to suggest for getting started?
23rd Aug 2019, 1:07 PM
David Crowley
David Crowley - avatar
0
Da2 can you clarify your answer? I've looked up constructs quickly and only found linear/conditional/loop as related to algorithms. I doubt that this is what you intended. I also saw someone in a different forum replying to the question "what is a construct in C++?" suggest that CONSTRUCTORS is likely what you meant.
23rd Aug 2019, 1:24 PM
David Crowley
David Crowley - avatar