Iterators, generators and array-like structures
8/22/2019 1:03:19 PMDavid Crowley
10 AnswersNew Answer
A deeper understanding constructs may help
Kindly read more on loops, iteration and recursive constructs...
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)
Control & data structures are the foundation of algorithms
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.
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
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?"
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.
Da2 any references to suggest for getting started?
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.