Questions tagged [iterator]

An iterator is an object-oriented programming pattern which for the most part functions similarly to a pointer to an object inside a collection with the added ability to traverse through the collection in a defined manner, agnostic of the actual implementation or even object addresses in physical memory. Iterators may be further limited in particular traversal directions.

Filter by
Sorted by
Tagged with
5
votes
1answer
804 views

Binary trees in Rust: more iterators! (non-consuming variant)

This is a follow-up to Binary trees in Rust: iterators, where Shepmaster suggested that I implement a non-consuming iterator. This was relatively straightforward. I chose to mirror the design of the ...
8
votes
1answer
4k views

Binary trees in Rust: iterators

This is a follow-up to Basic binary tree manipulation in Rust, where Shepmaster suggested that I implement the Iterator trait for the binary tree. This ended up ...
8
votes
1answer
223 views

Saving and resuming position while iterating over a container

Background: I'm in the process of writing a relatively simple behavior tree driven AI system for a game that I'm working on. Basically, the behavior tree is made up of individual gameplay tasks (move,...
8
votes
2answers
737 views

Yield statement in a using block from a clarity point of view

I want an opinion of the community about readability and maintainability of the following code. This is quite a rare case when yield statement included in a using construction. I know that this code ...
0
votes
2answers
532 views

Implementation of Vector in C++

This is a simple implementation of vector in C++. Please offer your reviews to improve the code. ...
4
votes
2answers
409 views

Using an IEnumerator without a foreach loop in order to simulate a state machine

I am working with an object that needs to run an ExecuteEffect() method. Sometimes, the ExecuteEffect() only needs to run once, ...
7
votes
3answers
1k views

Implementing xrange

I wanted to learn about Iterable and Iterators and hence thought about getting my hands dirty rather than reading theory and ...
8
votes
2answers
341 views

Iterating over Dungeon Map

I'm working on an RPG so of course I have to have some dungeons. I've got the dungeon generation working, with the output being a DungeonTileType[][]. Now I ...
8
votes
1answer
2k views

Chain multiple containers

Python has itertools.chain(), but C++ has nothing. This is my attempt to implement such a thing. Looking mainly for comments about potentially smarter ways of doing ...
6
votes
3answers
463 views

begin and end overloads for istream_iterator

I wrote this pretty straight forward begin and end overloads for istream_iterator. Is this standards compliant? Any problems with usage? ...
7
votes
2answers
4k views

Implement Java hasNext() and next() in C++

Recently came upon this question asked in interview with major tech company. Implement Java's next and hasNext function in your favorite programming language. Here is the solution I came up with....
7
votes
2answers
7k views

C++ Array with iterators

I wrote an Array class with iterators: ...
3
votes
1answer
108 views

Directed graph path enumerator in Java - follow-up

I have refactored the graph path enumerator. DirectedGraphNode was not changed, so refer to the link above in case you want to have a look at it. The node type is ...
4
votes
2answers
195 views

Directed graph path enumerator in Java

I have this iterator class that expects in its constructor two directed graph nodes, source and target, and upon each ...
3
votes
1answer
268 views

Chaining function for range-based for loop

Motivation: I'm relatively new to C++. I'm writing a function meant to be used with range-based for loops. The goal is to be able to iterate over multiple containers at once, each in turn (essentially ...
3
votes
3answers
111 views

Linked list implementation - follow-up

Follow-up to this question as recommended. Can this code be improved from an OOP paradigm perspective? ...
1
vote
1answer
225 views

OOP paradigm implementation of a linked list

Can this code be improved from an OOP paradigm perspective? ...
10
votes
2answers
166 views

String sequence in ES6

Given an unique string that represents a sequence of characters, the class should implement three methods (getNextChar, getNextString and the genSequence generator). How it works We give a sequence ...
6
votes
1answer
149 views

Return the element of collection if the collection contains only one element

Recently I have found in my code several places where I was first gathering some solutions, and then continued processing them only if the solution was unique (solution collection contained only one ...
2
votes
2answers
94 views

Python script to read CSVs and regroup it into the data types I need

This code does the following: Read CSV file and determine sub_type based on series_desc. Turn row into dictionaries ...
6
votes
1answer
3k views

Asynchronous iterator for AngularJS

I wrote this as a convenient way to batch together multiple, repetitive AJAX calls. I was working in angular at the time, so I use the $q service, but I'm sure it ...
2
votes
1answer
168 views

Idiomatic iterator for a linear function

A while back, I asked a question about implementing an idiomatic iterator in C++. I've recently revisited the code, and I'm bothered by the duplication in the begin and end iterators. The code goes ...
3
votes
1answer
241 views

Sieve32, a simple 32 bit sieve returning IEnumerable<uint> using C#

What a difference 1 bit makes! This very fast, simple sieve of Eratosthenes quickly finds 32 bit primes. It uses a memory efficient BitArray for odd numbers – but ...
14
votes
3answers
693 views

Sieve31, my sieve of Eratosthenes returning IEnumerable<int>

This very fast, simple sieve quickly finds 31 bit primes. It uses a memory efficient BitArray for odd numbers. How a 32 bit int...
0
votes
1answer
126 views

A non-static Sieve of Eratosthenes class, version 1

Over the years I’ve seen many C# sieves and despite their varying internals all that I have seen invariably are a static method that returns a List<int>, ...
2
votes
3answers
5k views

Thread-safe Iterator wrapper

Aside: This code assumes a class Option that can be Some or None to represent the presence (Some) or absence (None) of the value it contains. Haskell calls it Maybe. Here is my first pass at ...
1
vote
0answers
134 views

Array slice type in Java - follow-up

See the previous iteration. I have essentially fixed most of the issues mentioned by maaartinus. However, I left the size field as it is. Namely, if the user wants ...
3
votes
1answer
3k views

Array slice type in Java

Edit See the next iteration at Array slice type in Java - follow-up. I have this "slice" type for managing array subranges. It is kind of the same thing as Pythons slice notation, yet I did not add ...
11
votes
2answers
219 views

Streaming int support

This recent question Print Consecutive numbers by comparing two parameters frustrated me because I could not find a convenient way in Java 8 to support the conditions that are required. I answered it ...
0
votes
2answers
2k views

Iterator to remove duplicate element

The code implements custom iterator, which skips duplicate integers. The array expected is to be sorted. ...
2
votes
1answer
113 views

Avoid branching code for degenerate cases in Fibonacci generator

I was writing a simple function that would return the Fibonacci sequence up to the nth term and for whatever reason started wasting a lot of time on it. Here's what I came up with: ...
8
votes
1answer
861 views

Functional tree iteration in Common Lisp

I'm adding some functionality to an existing library of data structures in Common Lisp, with a view to asking the original author if I can take over maintenance and development of it. While the ...
4
votes
2answers
290 views

Modern C++ compliant insertion sort

This is a kind of follow up to my previous sort implementation question with specific questions to insertion sort, and Modern C++ idioms. Anybody seeing this post can see how I got to this modern ...
5
votes
2answers
5k views

Doubly linked list: iterator/pointer arithmetic

I'm using a doubly linked list container I've written as the working example. The textbook I'm using is from 2001, so feel free to point out where later versions of the C++ standard should be used ...
2
votes
1answer
83 views

Extract incomplete weeks from a DateTime collection

The code below attempts to extract incomplete weeks from a List<DateTime>. For example, a list containing all the days in Jan 2015 would result in the 5th to ...
6
votes
1answer
111 views

AdjacentElementsIteratorAdaptor for Random Access Iterators

Inspired by this comment I decided to implement an iterator adaptor that takes another iterator and returns each element and a given number of its neighbors. The final solution should be applicable ...
4
votes
2answers
870 views

Iterating over the lines of a stream

This structure is supposed to iterate over the lines of an input stream, using std::getline. Any remark is welcome! ...
1
vote
1answer
1k views

Efficient use of LINQ - Indexing

I have a Class with a List of objects, and those objects contain a list which has lists to objects in the first class, and I'm trying to quickly index them. Simplified: ...
11
votes
2answers
729 views

Lazy String.Split

C#'s String.Split method comes from C# 2.0, and lazy operations weren't a feature back then. The task is to split a string according to a (single) separator. Doing ...
1
vote
0answers
62 views

A context-based partition_copy

I recently posted a question on Stack Overflow asking how I could go about implementing a context-based partition copy. I actually came up with my own solution, but I would like to see if it could be ...
2
votes
1answer
95 views

Adaptive cyclic database iterator

This is a bit tricky to explain. I'm trying to model a cyclic iterator that would return the next available DataSource(SQL). So infinite calls would just pick the ...
2
votes
1answer
640 views

Iterator requirements for a fake container supporting C++11 range-based for

Around a year ago, I wrote this Stack Overflow answer where I suggested an idea to replace deeply nested loops with a single iterable virtual-container object that "contains" all the indices. The ...
3
votes
1answer
152 views

Wrapping WinAPI FindNextVolume as std iterator

This is my first attempt at creating an STL-style iterator. Is this code ok? ...
0
votes
2answers
223 views

Two permutation iterators for classes in Python with and without a generator

I am just testing out a couple of small classes for learning purposes. Here is my current working code: ...
4
votes
4answers
343 views

Functions taking iterables: peaks, compress, and skipper

I am just testing out a few small functions for learning purposes: ...
8
votes
1answer
2k views

Serialization: Escape Input String

In Json strings characters can be escaped with \\. Here is an iterator that can read such strings and convert the escaped characters to UTF-8 ...
3
votes
2answers
5k views

Bag data structure (a set that can store multiple copies of items)

I am trying to make my class a bit cleaner, especially some of its methods like Count, __add__, ...
5
votes
2answers
872 views

Interleaving values from several iterables

I am new to Python and am trying to see if there is a cleaner way to write my code. The following code take iterables as parameter it produces the first value from the first parameter, then the first ...
4
votes
2answers
6k views

Uppercase the initial char of every string in a list

My problem is that I have a sequence, potentially infinite, of strings. Let's suppose it to be {"Mickey Mouse", "Bugs Bunny", "Winnie the Pooh"} I have to make ...
7
votes
4answers
543 views

Classify values depending on predicates

I've to do a generic method Classify<T> that, given a sequence of elements of type T and an arbitrary number of predicates,...

1
4 5
6
7 8