70
votes
Accepted
Why does Python only make a copy of the individual element when iterating a list?
I already answered a similar question lately and it's very important to realize that += can have different meanings:
If the data type implements in-place addition (i.e. has a correctly working ...
37
votes
Accepted
Unit tests: deferred assertions with Linq
Is it ok to add deferred assertions like this [..]
No, it isn't. Why? Because if you for any reason remove the second assert the test would still turn green and you would think it still works but it ...
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 ...
19
votes
Why does Python only make a copy of the individual element when iterating a list?
Clarification - terminology
Python does not distinguish between the concepts of reference and pointer. They usually just use the term reference, but if you compare with languages like C++ that do ...
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 ...
11
votes
Accepted
What Makes the Iterator a Design Pattern?
Most of the patterns from the GoF book have the following things in common:
they solve basic design problems, using object-oriented means
people often face these kind problems in arbitrary programs, ...
11
votes
Accepted
Is there a reason Iterator and Stream do not implement Iterable?
They would break the Liskov Substitution Principle implementing Iterable<>.
An important part of the Iterable<> contract that isn't describable in Java is that you can iterate multiple ...
11
votes
Why does Python only make a copy of the individual element when iterating a list?
None of the answers here give you any code to work with to really illustrate why this happens in Python land. And this is fun to look at in a more deep approach so here goes.
The primary reason that ...
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...
...
9
votes
Iterating over objects of a specific class in a container of polymorphic base objects
When you have a polymorphic collection like your scenario, the problem you have is that the object itself knows what it is but nothing else does so anything outside of the container needs some extra ...
8
votes
What Makes the Iterator a Design Pattern?
The Gang of Four quote Christopher Alexander's pattern definition:
Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution ...
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
Why does Python only make a copy of the individual element when iterating a list?
@Idan's answer does a good job of explaining why Python does not treat the loop variable as a pointer the way you might in C, but it's worth explaining in more depth how the code snippets unpack, as ...
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
Is it reasonable to write worse debug code in order to improve production code?
On a generic case : if the logging make you some non-productions operations that can have a real impact on production performance wrap your code with code like isDebugEnabled() (or directive #ifdef ...
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
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 ...
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 ...
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
Iterating over objects of a specific class in a container of polymorphic base objects
Polymorphic operations are traditionally handled by the visitor pattern. As @Erdrik Ironrose indicates, the object knows it’s type so let it do it’s work. The iteration is performed by calling a ...
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
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 ...
3
votes
Iterating over objects of a specific class in a container of polymorphic base objects
Elaborating on Erdrick's answer, I would strongly advocate that you scrap the "everything" container, and just have separate containers for all the Child1s, Child2s, etc.
If you need to occasionally ...
3
votes
What does these UML diagram arrows mean in Iterator pattern?
The Gang Of Four Book does not use UML but OMT (or it's at least based on OMT)
The dotted arrow means "instantiation", so it says that the ConcreteAggregate creates the ConcreteIterator.
The normal ...
3
votes
Is it reasonable to write worse debug code in order to improve production code?
When something is unreadable, it often helps to encapsulate it in a function(or, sometimes, a class). The function invocation is readable(as long as the name is meaningful and the API is clear) and ...
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 ...
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 ...
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