21
votes
Accepted
C++ Iterator, Why is there no Iterator base class all iterators inherit from
You've already gotten answers pointing to why it's not necessary for all iterators to inherit from a single Iterator base class. I'd got quite a bit further though. One of the goals of C++ is ...
16
votes
pre-increment vs. post-increment
You may be right regarding pointers.
However:
C is not C++. C++ guarantees some very precise semantics especially around when copies are made, as this affects RAII: in C++, copies are an observable ...
10
votes
C++ Iterator, Why is there no Iterator base class all iterators inherit from
The difference is between What something is, and How something behaves.
A lot of languages try to conflate the two together, but they are quite distinct things.
If How is What, and What is How...
...
7
votes
C++ Iterator, Why is there no Iterator base class all iterators inherit from
Because C++ doesn't need to have (abstract) base classes to do polymorphism. It has structural subtyping as well as nominative subtyping.
Confusingly in the particular case of Iterators, previous ...
6
votes
C++ Iterator, Why is there no Iterator base class all iterators inherit from
One reason is that iterators don't have to be instances of a class. Pointers are perfectly good iterators in many cases, for example, and since those are primitives they can't inherit from anything.
6
votes
Should I move tasks which is just for a specific element only out of for loop?
There is a huge difference in both code snippets: if this.arr.length is 0 then option 1 works as designed, whereas option 2 fails in an attempt to perform operations on not existing elements.
...
6
votes
Is it true that "A Java Iterator is an Abstract Data Type"?
A Tale of Abstractions
There's a lot of confusion about this on the Internet, but the term is quite technical, and according to the Cook paper (referenced in the answer liked to by Greg Burghardt), it ...
5
votes
Accepted
Hash Table with iterators as the keys, is this poor design and can I do this better?
Algorithm
So, AFAICT your algorithm boils down to:
For each new_item, find the closest old_item. (This creates a set of old_items.)
For each old_item in the set, update it with the closest new_item.
...
5
votes
using-declaration or typedef for iterator tags?
In C++, using and typedef are mostly equivalent and both declare a type alias. But they have reverse order of arguments:
typedef original new_name;
using new_name = original;
The typedef can get very ...
5
votes
JS - two array filters vs. one forEach?
This might be a case of pre-optimization. Each call to filter will loop over the array, so your first example iterates twice. Your second example iterates once. On the surface your second approach ...
5
votes
Accepted
Should I always use iterators when working with strings?
Go for what'll be least surprising to yourself and your colleagues in the future. I've heard this termed the principle of least surprise, and it's a pretty simple idea. If there's a simple way to ...
4
votes
Accepted
Indexable iterators
Indexing should be free of side effects. That is, I expect x[a] == x[a] for all objects x and all valid indices a. This rules out solutions 1a and 2a. Reasoning about iterator indexing in 1a sounds ...
4
votes
Accepted
How to Implement a `function` with `return` Without Using the `function` keyword
If you are writing an interpreter for an machine code instruction set architecture, you provide for and maintain (1) the CPU's registers, which includes the Program Counter (aka Instruction Pointer), ...
4
votes
Iterator/Range design, can a lightweight input iterator implemented with C++/java's iterator model?
what are the cons of Java's access+increment iterator model (can you give an example of that)?
Well, there is the obvious: the fact that you cannot increment without accessing.
Let's say that you ...
4
votes
Accepted
Is it bad practice to do additional work in IAsyncEnumerable generator method?
I think this is less about IAsyncEnumerable and more about good OOP design. Mainly the rule of least surprise and high cohesion.
I would never expect method named GetRowsAsync to do anything like ...
3
votes
using-declaration or typedef for iterator tags?
These aren't using-declarations. They are alias-declarations. Aside from the different syntax, they are 100% semantically identical to typedefs.
I find them a lot more readable, though, and I imagine ...
3
votes
JS - two array filters vs. one forEach?
I know that the Array prototype method filter is generally preferred over forEach
What makes you say that?
Filters and forEach are two different things; they are not interchangeable. There are ...
3
votes
Accepted
How much an iterator should do
Different iterators provide different contracts. C++ probably has the most fine-grained iterator concepts, distinguishing operators that
can move forward
can move backwards
can jump to another element ...
2
votes
Make lambdas concise using enumerations?
In Java, the idiomatic way to group a bunch of functions is just a plain old class with plain old methods. Then your logFormatList looks something like List.of(LogFormatFunctions::date, ...
2
votes
Accepted
How to implement a cursor iterator?
The correct approach for the cursor is option 2
The answer is already in your quote, which describes option 2:
(...) We call this kind of iterator a cursor, since it merely points to the
...
2
votes
Indexable iterators
Here's something to consider; in terms of API-design, an iterator is an abstraction of a traversal strategy. So, calling next() doesn't have to do
"a, b, c, d, e, f, g, h, i, j",
it could do
...
1
vote
How to implement a cursor iterator?
tl;dr: use your language's recommended pattern.
The dilemma between iterator methods belonging to the aggregate or to the iterator looks like a classical pseudo-question of "object oriented design" ...
1
vote
Indexable iterators
PEP 234 says
A class that wants to be an iterator should implement two methods: a
next() method that behaves as described above, and an iter()
method that returns self.
The two methods correspond to ...
1
vote
Is it true that "A Java Iterator is an Abstract Data Type"?
So, may I state that an Iterator is an abstract data type?
An ADT is a collection of data and a set of operations that may be performed on that data. An iterator is composed of a set of data to ...
1
vote
Should I always use iterators when working with strings?
The primary reason for iterators is to allow/support generic algorithms that can work with containers of different sorts--arrays, trees, linked lists, etc. The iterator decouples the algorithmic part (...
1
vote
Should I always use iterators when working with strings?
Well, there are functional differences between the examples:
The index example can deal with the string being arbitrarily modified during the loop.
It buys that by always going back to it, and re-...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
iterator × 67c++ × 14
java × 10
python × 9
c# × 8
design-patterns × 8
loops × 6
collections × 5
design × 4
array × 4
object-oriented × 3
javascript × 3
.net × 3
list × 3
object-oriented-design × 2
programming-practices × 2
performance × 2
functional-programming × 2
interfaces × 2
naming × 2
language-design × 2
optimization × 2
ruby × 2
strings × 2
code-smell × 2