Questions tagged [iterator]
The iterator tag has no summary.
67 questions
1
vote
2
answers
477
views
using-declaration or typedef for iterator tags?
I'm creating a class representation of a mathematical tuple (or simply, an ordered set of numbers). Being a list-like object that could benefit from an iterator representation, I have decided to give ...
2
votes
2
answers
3k
views
JS - two array filters vs. one forEach?
I've had a question for a while. I know that the Array prototype method filter is generally preferred over forEach, and I believe it is typically faster at the job as well.
But what about if I have ...
1
vote
0
answers
191
views
Visitor pattern with internal iterators
I'm trying to understand the following description of implementing the visitor pattern with an internal iterator. It is from the GoF's Design Pattern Elements of Reusable Object-Oriented Software book ...
1
vote
3
answers
1k
views
Is it bad practice to do additional work in IAsyncEnumerable generator method?
Imagine you have to process each row in a large table.
For every single row you have to download some data from a web service and store it in a different database.
Loading all rows at once into the ...
3
votes
3
answers
533
views
Indexable iterators
Suppose someone designs an indexable iterator class, i.e. an iterator that supports __getitem__ for (non-negative) integer indexes such as the following:
In [1] it = indexed_iter("abcdefghij"...
0
votes
1
answer
627
views
Using for_each instead of iterators to avoid iterator invalidation
I am writing a simple custom (special purpose) container and would like to allow for iteration over each element, however, avoid using iterators due to the problem of iterator invalidation.
Instead of ...
5
votes
3
answers
1k
views
Is it true that "A Java Iterator is an Abstract Data Type"?
I'm trying to deepen my understanding on this, the only thing I know for sure is that Iterator is an interface in Java.
I've been reading CS literature, for example here and here and looking for ...
3
votes
2
answers
861
views
How to implement a cursor iterator?
In the "Structure" section of the Iterator design pattern, the book Design Patterns: Elements of Reusable Object-Oriented Software defines the Iterator class with four basic operations: First, Next, ...
2
votes
0
answers
134
views
Implementing an iterator vs. looping over a list
I want to loop over a number of rows in a database and I have implemented an object in order to access the rows. Now when using this object I am not sure about the best practice. Implementing an ...
2
votes
4
answers
2k
views
Should I always use iterators when working with strings?
Here is the known old way to iterate over the string:
for (int i = 0; i < str.length(); i++) {
char c = str[i];
}
However recently I have also seen in multiple places the usage of ...
1
vote
1
answer
241
views
How much an iterator should do
I am working on creating iterators, for strings, lists, trees, graphs, and potentially other things.
First a side note.
I have a string data type in my engine. The string is implemented as a bunch of ...
0
votes
2
answers
117
views
Make lambdas concise using enumerations?
Generally, we are looking to create a logging framework that can target human readable output as well as various structured data formats. So, a goal is minimizing code duplication in packaging the ...
17
votes
4
answers
8k
views
C++ Iterator, Why is there no Iterator base class all iterators inherit from
I am learning for an exam and I have a question which I am struggling to give and answer for.
Why does no iterator base class exist all other iterators inherit from?
My guess my teacher is ...
1
vote
0
answers
1k
views
Combination of visitor and iterator pattern
While studying the visitor design pattern i found this phrase:
You can use Visitor along with Iterator to traverse a complex data structure and execute some operation over its elements, even if they ...
3
votes
2
answers
141
views
Should I move tasks which is just for a specific element only out of for loop?
For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be:
1.place inside for loop
for(int i=0;i&...
0
votes
2
answers
236
views
How to Implement a `function` with `return` Without Using the `function` keyword
Along the lines of How to Simulate Control-Flow without using Control-Flow Primitives, I am wondering how to simulate return from a function.
Given an example setup like this:
console.log(a(10))
...
3
votes
1
answer
1k
views
Hash Table with iterators as the keys, is this poor design and can I do this better?
I'm developing a program where twice I've found the solution to a problem was to use hash tables with iterators as keys and some other arbitrary type as the value.
I found my self using this pattern ...
8
votes
1
answer
19k
views
pre-increment vs. post-increment
In the Google C++ Style Guide it says:
Preincrement and Predecrement
Use prefix form (++i) of the increment and decrement operators with iterators and other template objects.
When a ...
-1
votes
1
answer
380
views
Iterator/Range design, can a lightweight input iterator implemented with C++/java's iterator model?
I'd like to better understand Iterator/Range design decisions (focusing on InputIterators).
Basically, this is the java model:
while (iter.hasNext()) {
Object o = iter.next();
// do ...
18
votes
1
answer
3k
views
Unit tests: deferred assertions with Linq
Is it ok to add deferred assertions like this
var actualKittens = actualKittens.Select(kitten => {
Assert.IsСute(kitten);
return kitten
});
Why? So I can iterate just once even with ...
6
votes
1
answer
3k
views
Is there a reason Iterator and Stream do not implement Iterable?
The other day I was playing around with an experiment and I had a for loop something like so:
for (Node node : children) {
// do stuff with node ...
}
And then I changed it to do this:
for (Node ...
4
votes
4
answers
3k
views
Iterating over objects of a specific class in a container of polymorphic base objects
Let's suppose there is a base object class - let it be called Object - and a list<Object> container. There are many child classes of Object - Child1, Child2 etc.. They all are stored in the ...
1
vote
0
answers
283
views
Why are most ReactiveX implementations push-based?
Feel free to correct my history, but as far as I understand it, Rx and the Reactive Manifesto trace their roots back to C# and its Reactive Extensions, which is uses push (callback-based) messaging, ...
34
votes
6
answers
24k
views
Why does Python only make a copy of the individual element when iterating a list?
I just realized that in Python, if one writes
for i in a:
i += 1
The elements of the original list a will actually not be affect at all, since the variable i turns out to just be a copy of the ...
8
votes
2
answers
3k
views
What Makes the Iterator a Design Pattern?
I have been wondering what it is that makes the Iterator special when compared to other similar constructs, and that made the Gang of Four list it as a design pattern.
The Iterator is based on ...
3
votes
0
answers
111
views
What's the most practically efficient way to store differences in adjacent matrix values?
I am implementing a certain algorithm that works like this:
Create a closed contour (list) of elements in a matrix, where closed means that the last element is adjacent (by row, column) to the first.
...
2
votes
3
answers
2k
views
What does these UML diagram arrows mean in Iterator pattern?
What does these UML diagram arrows mean in Iterator pattern that are drawn from ConcreteAggregate to ConcreteIterator and backwards. I have looked thorough the legend (notations) of UML diagrams, but ...
4
votes
3
answers
403
views
Is it reasonable to write worse debug code in order to improve production code?
The title pretty much speaks for itself, but I'll provide the current decision I am facing.
I am migrating python code towards the use of generators. The current code looks like this:
...
l = ...
26
votes
1
answer
4k
views
Are generator functions valid in functional programming?
The questions are:
Do generators break the functional programming paradigm? Why or why not?
If yes, can generators be used in functional programming and how?
Consider the following:
function * ...
2
votes
3
answers
604
views
Is there a good way not to hand-write all twelve required Container functions for a custom type in C++?
On stackoverflow I asked, what is the preferred way to expose custom STL-style iteration?. The answer seems to be to implement twelve functions: six members, six non-members (perhaps using some ...
3
votes
2
answers
230
views
What is the most idiomatic way to iterate collection with different action for first element?
Sometimes we meet a situation where we should iterate (or map) over a collection, applying the same procedure (function) for all elements except the first one. The simplest example is finding the max ...
5
votes
3
answers
5k
views
Is implementing IEnumerable required to use foreach on collections?
I have the following class that doesn't implement IEnumerable but is working perfectly with foreach.
And also, arrays are working without implementing IEnumerable.
So why does it keep saying that ...
0
votes
3
answers
126
views
How to optimize iterable queries with external arguments
I will use C# here as an example, but my question is about any language.
My question is from framework to compiler perspective (i.e the solution can be by implementing given idea inside compiler)
...
7
votes
1
answer
566
views
What is the most generic way to provide a variable amount of outputs from a Rust function?
I am currently writing an API for machine learning algorithms in Rust and I would like for a single genetic algorithm, artificial neural network, or Bayesian network to provide multiple outputs so ...
-3
votes
1
answer
5k
views
Why can't I return an implementation of an interface for a method that requests the interface? [closed]
I am implementing an interface I on a class A.
Class A implements I.
I have an Iterator T that is supposed to return an object which implements I everytime I call next.
My iterator goes as follows (...
25
votes
6
answers
6k
views
Iterator pattern - why is it important to not expose the internal representation?
I am reading C# Design Pattern Essentials. I'm currently reading about the iterator pattern.
I fully understand how to implement, but I don't understand the importance or see a use case. In the book ...
1
vote
1
answer
1k
views
How to properly handle indefinite generators in Python
Let's say we have a generator that is indefinite, where new elements can arrive at any moment with significant (up to indefinite) delay.
An example of such generator is tail -F command. In python (...
8
votes
1
answer
23k
views
Template Function: Passing Iterators
I am struggling with making a design choice in the following setup:
I am writing (C++) functions which take a pair of iterators (to template containers) and compute a return value of the same type ...
-2
votes
1
answer
934
views
Does 'Iterable' interface look redundant in java? [closed]
By this below definition(pre 1.8) of Iterable,
package java.lang;
import java.util.Iterator;
public interface Iterable<T> {
Iterator<T> iterator();
}
I would say that, Iterable is ...
7
votes
5
answers
10k
views
C++ Iterators: Best practice to represent end of range - Last or Beyond-last?
I am writing a library which deals a lot with sub-sequences of ordered containers.
So for example I have a container (1,2,3,4,5,6) and a user wants to access (3,4,5).
I am providing the subsequence ...
8
votes
1
answer
21k
views
When should I use a generator and when a list in Python? [duplicate]
I often find it cleaner to write a generator than to return a list. For example, I prefer
def my_func_gen(foo):
for i in foo:
# Do some stuff that's too complicated for a list or ...
4
votes
3
answers
7k
views
Java & REST API: How do I check for duplicate before inserting record?
I am developing an application in Java to parse and upload records from a CSV to an online database, via a REST API.
While I know for sure that there are no duplicate records in each CSV file, I ...
3
votes
2
answers
2k
views
Complex iterators in C
note: this was originally asked on SO.
Part of my current project involves iterating over sequences that don't necessarily exist. For example, I have something similar to a relational database in ...
7
votes
1
answer
2k
views
In Java, why does (unsorted) Set implement Iterable, but SortedMap does not?
There are two aspects to this question that I felt were too closely related to ask as separate questions.
Why doesn't SortedMap implement Iterable<Map.Entry<K,V>>?
If you need to perform ...
4
votes
4
answers
4k
views
What do you call an iterator that returns the current, previous and next siblings of each node of a list? [closed]
What do you call an iterator that given a list [a, b, c], returns an object of the form { prev, curr, next } for each iteration?
e.g, (? === undefined)
{ prev: ?, curr: a, next: b }
{ prev: a, curr: b,...
3
votes
1
answer
2k
views
Why is iterating through List<T> more expensive than iterating through Array in .NET?
According to the answers in this post, List<T> is backed by an Array. According to this article, list iteration is considerably slower than array iteration.
If Lists are arrays 'under the hood',...
0
votes
2
answers
152
views
Library design: better to leave potentially destructive feature in with a warning, or cut it out completely?
I recently created a red-black tree in C# to better understand how it works. With it, I implemented an in-order enumerator, however I quickly realized that enumerating a tree can have destructive ...
6
votes
0
answers
992
views
Which languages have the best support for stackful coroutines? [closed]
Various languages support (some flavor of) coroutines.
One way to discriminate coroutines is whether they are stackful or not (terminology based on Ana Lucia de Moura; Roberto Ierusalimschy (2004). "...
4
votes
3
answers
539
views
Interface to enumerate over files
I'm trying to design a generic interface for enumerating over a list files. Each iteration will open a file, allow the consuming code to access the file to perform some task, and then close the file.
...
3
votes
3
answers
7k
views
Alternatives to foreach iterators involving ref and out
I am trying to make a flexible particle system for my XNA game, and I've got these interfaces:
public interface IParticle : IUpdateable
{
bool Alive { get; }
float Percent { get; }
}
public ...