Skip to main content
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 ...
MSeifert's user avatar
  • 604
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 ...
t3chb0t's user avatar
  • 2,592
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 ...
Jerry Coffin's user avatar
  • 44.7k
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 ...
Idan Arye's user avatar
  • 12.1k
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 ...
amon's user avatar
  • 135k
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, ...
Doc Brown's user avatar
  • 214k
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 ...
Caleth's user avatar
  • 11.6k
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 ...
enderland's user avatar
  • 12.1k
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... ...
Kain0_0's user avatar
  • 16.3k
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 ...
Erdrik Ironrose's user avatar
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 ...
amon's user avatar
  • 135k
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 ...
Caleth's user avatar
  • 11.6k
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.
David Thornley's user avatar
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. ...
Christophe's user avatar
  • 80.6k
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 ...
walpen's user avatar
  • 3,241
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 ...
Filip Milovanović's user avatar
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 ...
Walfrat's user avatar
  • 3,536
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. ...
hoffmale's user avatar
  • 777
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 ...
Alecto's user avatar
  • 533
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 ...
Greg Burghardt's user avatar
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), ...
Erik Eidt's user avatar
  • 34.4k
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 ...
Nicol Bolas's user avatar
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 ...
Bill Door's user avatar
  • 1,090
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 ...
amon's user avatar
  • 135k
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 ...
amon's user avatar
  • 135k
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 ...
user949300's user avatar
  • 8,929
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 ...
Lovis's user avatar
  • 502
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 ...
Idan Arye's user avatar
  • 12.1k
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 ...
amon's user avatar
  • 135k
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 ...
Arseni Mourzenko's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible