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
10
votes
1answer
1k views

Merge n sorted iterators

Over the weekend I was curious about efficiently merging multiple sorted iterators together, and for them to be in sorted order. This is quite like a challenge on HackerRank: You’re given the ...
1
vote
1answer
95 views

Sorting a list of IComparables

I have a DateTime class (my own, not System.DateTime) which implements the IComparable ...
6
votes
2answers
788 views

Iterator of Container of Container

Based on this question I though I would pull together an iterator that iterated over a container of containers. ...
10
votes
3answers
2k views

Table of Tribonacci sequence using NumPy and PANDAS

What I am trying to accomplish is calculate the Tribonacci numbers and the ratio. I can do this in Excel easy like this. So I tried do the same using Python 3 now. It works and gives such output. ...
4
votes
2answers
472 views

A C++ custom iterator for indirection iteration

This program does not quite conform to the previous related question, yet does basically the same thing: given a collection of elements and a collection of indices, return via an iterator collection ...
1
vote
1answer
74 views

File names to database table

I have files in subfolders on a network drive. I want to create a list of files, and insert that list into an Oracle table. I can successfully iterate through the files in the subfolders and do the ...
8
votes
2answers
2k views

Interleave two lists of strings

I've got a fairly simple task here, but I think it's worth trying to perfect the implementation, because I can probably learn a thing or two about using .NET's enumerables correctly in the process. I ...
5
votes
1answer
137 views

First all zero row of a matrix in C++

The problem of finding the first all zero row in a NxN matrix was featured in a letter to the CACM in 1974 by Frank Rubin who claimed it was a problem for which using a ...
3
votes
1answer
69 views

Loop + Take for repeatedly taking elements from the same collection

I sometimes need to enumerate the same collection multiple times so I wrote an extension that does this for me. I call it Loop. It takes one optional parameter <...
5
votes
2answers
210 views

Lag Lead Enumerator

I had a need to do a look-ahead and look-behind when enumerating though and IEnumerable. Here is a classic situation: ...
3
votes
1answer
854 views

Binary search tree in C++ 17 using iterator pattern

I am new to C++ and am attempting to write the following binary search tree using the iterator pattern (and std::optional in C++17): ...
7
votes
2answers
359 views

Lazy string_view splitter range

Trying something out of my comfort zone here, and attempting to implement a D-style range around std::string_view. Looking for any and all suggestions or ...
4
votes
0answers
54 views

Iterator structure for block and interlace counting schemes

We have a set of time slices, in the order of 50. Throughout this question I will use 6. We group these slices into blocks, in this example the block size is 2. Then we want to iterate through all ...
6
votes
3answers
412 views

Find median of two sorted ranges

Given two sorted ranges of elements, compute a median of all the elements in O(log(n+m)) time. This solution contains only one call to std::lower_bound ...
4
votes
0answers
495 views

Selection sort implementation in C++ iterator approach

I am trying to learn the C++ way of writing code. I was wondering if this code could be improved or if there are any major mistakes. Is it ok how I name the variables? ...
1
vote
1answer
117 views

Base64 iterator adaptor for Rust

As a Rust learning exercise, I'm working on implementing cryptopals challenges using iterators to perform lazy processing of data. For the first challenge, this is a base64 decoder implemented as an ...
2
votes
1answer
236 views

Custom iterator and proxy pattern C++

The task was: Create a custom iterator to work with files. I also was told to use a proxy pattern which is new to me. Is there everything I can change to make my code look better? Or maybe I have ...
7
votes
3answers
484 views

Generator iterator with push back function

In a compiler project for an LL(1/2) grammar, I have had a need for a generator iterator with a push back function. I have been surprised not to find any perfectly applicable solution (clean, simple, ...
0
votes
1answer
95 views

Working with cached generator values

I have set of values represented by a class. The class should be initialised with a generator to limit memory use. The class itself is the iterable, having ...
1
vote
1answer
170 views

Stream “on the fly” from matrix

I want to produce a Stream<Pixel> from a Pixel[][]. My code for this is extremely verbose so I suspect I am doing ...
1
vote
1answer
41 views

JointIterable: iterates over all elements of a list of collections

I have this small utility class that allows me to iterate over a series of collections. Please tell me anything that comes to mind. JointIterable.java ...
3
votes
1answer
54 views

Streamlining series of try-except + if-statements for faster loop processing in Python

I'm processing strings using regexes in a bunch of files in a directory. To each line in a file, I apply a series of try-statements to match a pattern and if they do, then I transform the input. After ...
3
votes
1answer
170 views

Efficiently determine every factor of a given natural number

It's not hard to find all of the factors of a given natural number. Below is a pretty fast python function that can do just that: (Modified version of Steinar Lima's function, https://stackoverflow....
2
votes
1answer
52 views

inroder_iterator for syntax tree of markargs

Introduction It is a spin-off to my compiler creation. Problem Traverse the markargs::syntax_tree in order, e.g. first goes ...
5
votes
1answer
1k views

An iterator wrapper that on dereferencing, returns the value of a member (function)

I wrote a simple iterator wrapper that can be used in e.g. std::find and std::copy and iterator pair constructors to search on ...
3
votes
5answers
115 views

Product iteration while checking for overlaps

This is an extended question to the following question I posted Class scheduling tool with image output Context of previous question: I have a multilayered dictionary that contains information ...
3
votes
1answer
184 views

Internal Zip-like Iterator

My goal was to create an zip-like iterator to selectively iterator over a subset of arrays in a larger manager object, compare itself to any other zip-like iterator, but can be copy constructed from ...
5
votes
1answer
489 views

C++ simple enumerator implementation

I write this enumerator class out of the necessity. I am working on a project that heavily depends on inheritance, abstract (interface) classes, etc. Therefore I ...
5
votes
1answer
460 views

Splitting a UTF-8 string into equal-sized byte-arrays for parallel processing

Based upon a question from Stack Overflow, I wanted to expand on the answer I wrote and define a solution that would support ordering the strings (after processing). So this starts off with a basic <...
2
votes
5answers
847 views

Diff comparison for List<string>

List<string> difference tool for two input List<string> listA and listB Output ...
3
votes
1answer
269 views

Selection algorithm using merge sort and IEnumerable

For educational purposes, I wrote a selection algorithm based on a Merge sort. I would like to improve performance. ...
2
votes
1answer
85 views

JavaScript Python Arrays

Here is an implementation in JavaScript that attempts to resemble Python-like arrays by mimicking slice comprehension for both getters and setters, as well as negative indexing from the end of the ...
4
votes
2answers
420 views

Iterators in JavaScript and remembering position

This is an attempt to write a function link that creates a linked list from an array: ...
4
votes
1answer
222 views

Sorting algorithm, possibly a new one [closed]

I'm a beginner who has never seen a sorting algorithm's implementation and have decided to try make one on my own (can someone tell me if this has been already used?). The fact is I have never used ...
6
votes
4answers
3k views

Splitting a string on delimiter outside of quotes while returning item-by-item

One of the more recent functions I've needed for personal and work purposes is to split a string on a delimiter, but while ignoring the delimiter when inside a quote. Just as well, if within a quote ...
12
votes
3answers
9k views

Extension method replacing elements from an IEnumerable<T> collection

Today I needed a method to replace all 'x' values in a said collection. Since there is no such method by default in .NET I wrote my own: ...
2
votes
1answer
697 views

Single element from iterator

When I was working in Rust this week, I missed kotlin's fun <T> Sequence<T>.single(): T. Basically, calling this function means that you are asserting ...
11
votes
4answers
1k views

random_iterator in C++

This is a small utility that permutes a C++ standard container without modifying the container being iterated: random_iterator.hpp ...
3
votes
0answers
50 views

SparseGraph: A representation of a mathematical graph

I'm working on a SparseGraph which reflects the mathematical notion of a graph. All code included here works in all of the basic test cases that I've tried. However, I'm most concerned about the ...
5
votes
2answers
171 views

Read lines from a file in chunks

I created a function to read lines from an file into chunks. My hidden agenda in creation this script was in python the yield function in interaction with chunks. The script works fine, but now i ...
7
votes
5answers
741 views

“The Genuine Sieve of Eratosthenes” in C++14

The following C++ code implements the "Genuine Sieve of Eratosthenes" algorithm as described in Melissa O'Neill's classic paper. On my MacBook it computes the first 1,000,000 primes in about 11 ...
5
votes
1answer
392 views

Retrieve the index of the first element using a predicate

I want to change the legacy C-style code by using stl: ...
3
votes
3answers
1k views

Iterator producing unique random numbers in a specified range

I needed an iterator that produced random numbers, never repeating them, in a specified range of 0..max-1 (index for a collection). When the numbers are exhausted it should not have a next anymore. ...
2
votes
0answers
36 views

DynamicIterable

I have written this DynamicIterable that can be used as a Lazy Iterable, where you give it a Supplier<T> of something and ...
5
votes
2answers
549 views

Zigzag Iterator

I've recently solved the LeetCode's "ZigZag iterator" problem: Given two 1d vectors, implement an iterator to return their elements alternately. For example, given two 1d vectors: ...
4
votes
1answer
463 views

Testing procedure with several nested yield statements in Unity3D

I made a small game that does the following: Displays a popup with the instructions of the game Turns on the GUI Start a list of trials for the player For each trial: It shows a symbol Waits for a ...
7
votes
3answers
1k views

Generic List code with Iterator

I was wondering if this implementation of generic list with iterator is correct. The code compiles and goes fine. What would you think shall I improve? (first time I post here, I'm scared). I'm also ...
3
votes
1answer
424 views

Implementation of iterator pattern for any array of elements

I have implemented Iterator pattern: The interface Container.java: public interface Container<T> { Iterator<T> getIterator(); } Interface ...
5
votes
1answer
127 views

Iterator to generate all the words (of a given words) that are one change away

I'm trying to create an iterator, that will generate all the words that are one change away. For example, if my input is "sam", I should get the following words: "aam", "bam", ..."ram","tam",...."...
7
votes
1answer
2k views

Transforming iterator

The question is follow-up to preprocessing iterator. Specification: Given Functor functor (which is Callable) and ...