Questions tagged [iterator]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1
vote
0answers
94 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 ...
1
vote
4answers
211 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
1answer
124 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 ...
0
votes
2answers
83 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 ...
8
votes
4answers
868 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 ...
0
votes
0answers
158 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 ...
2
votes
2answers
120 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
2answers
200 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
1answer
562 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 ...
5
votes
2answers
6k 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
1answer
182 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
1answer
2k 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 ...
4
votes
1answer
1k 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 ...
3
votes
4answers
587 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
0answers
222 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, ...
31
votes
6answers
13k 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 ...
9
votes
2answers
2k 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 ...
2
votes
0answers
71 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
3answers
606 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
3answers
338 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 = ...
17
votes
1answer
2k 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
3answers
380 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
2answers
165 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 ...
3
votes
3answers
2k 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
3answers
93 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) ...
5
votes
1answer
382 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
1answer
1k 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 (...
23
votes
6answers
4k 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
1answer
594 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 (...
4
votes
1answer
12k 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
1answer
515 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 ...
6
votes
5answers
5k 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 ...
4
votes
1answer
9k 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
3answers
3k 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
2answers
833 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
1answer
1k 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 ...
3
votes
4answers
3k 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: ...
3
votes
1answer
1k 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
2answers
133 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 ...
7
votes
0answers
505 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
3answers
414 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
3answers
5k 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 ...
5
votes
3answers
21k views

Best way to find whether a collection does or does not contain an element with a specific desired quality

It seems to be a very common thing to have to tell whether some list or set contains at least one object matching a given condition, yet my prior searching and reading have never found a satisfactory ...
1
vote
2answers
2k views

Python sorted iterable set, modifiable during iteration

I am looking for a datastructure to handle cycling through a large number of ordered subroutines, some of which are active, most of which are not. I am thinking I need an implementation of a python ...
8
votes
2answers
2k views

What are the advantages of next-iterator over this-iterator?

I don't work too often with Java/C# iterators directly but when I do I always wonder what was the reason to design iterators in "next" fashion. In order to start you have to move iterator, in order ...
2
votes
1answer
1k views

Randomly and uniquely iterating over a range

Say you have a range of values (or anything else) and you want to iterate over the range and stop at some indeterminate point. Because the stopping value could be anywhere in the range, iterating ...
5
votes
3answers
4k views

Is it bad practice to make an iterator that is aware of its own end

For some background of why I am asking this question here is an example. In python the method chain chains an arbitrary number of ranges together and makes them into one without making copies. Here is ...
7
votes
3answers
226 views

Are there languages that expand on the STL's iterator types? [closed]

Many languages use the concept of an iterator. The c++ STL expands on this with input iterators, output iterators, forward, bidirectional, random access and others. As far as I know, these ...
4
votes
3answers
326 views

Is there an idiom for a loop that executes some block of instructions between iterations? (In Ruby in particular)

I often need to do some operations in a loop and some other operations between the iterations. A simple example would be collecting words from an array into a string, spelled backwards and separated ...
1
vote
2answers
2k views

Nice iterator naming [closed]

How do you name your iterators when you return a begin and an end iterator from a class? Without it sounding clunky, that is. Example: typedef std::vector<Idea> Ideas_Type; Ideas_Type::...