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.
388
questions
0
votes
1answer
46 views
Random Access Iterator Implementation
(This is the Non-const version, I have to implement the const one too).
Could someone please review this implementation? This is made for std::vector
I'm unsure whether I respected all requirements ...
3
votes
3answers
242 views
Input Iterator implementation
I need some review on my implementation of input iterator. I am currently re-implementing std::vector, and need input_iterator for a specific constructor.
Any criticism is much appreciated! Thank you.
...
3
votes
1answer
58 views
Cyclic generator with access to current local state and executes callback after each cycle
I'm working on a Python class that is a cyclic generator, like itertools.cycle, but it offers access to its current state and executes a callback after the ...
5
votes
1answer
52 views
wrapper class for random access iterator to hide template method implementation from header file
I have a set of template functions that have itrator types as template parameters, so can be used with any kind of iterators. std::find can be a good example.
Also ...
4
votes
1answer
77 views
Const wrapper for iterator
Edit: added clarification for why I want this, and updated the code since I don't have any answers yet
I have a C++11 array-like class which (can be) a wrapper around a random-access iterator. Index-...
6
votes
3answers
656 views
Shorten dict comprehension with repeated operation
dc = {line.split('=')[0]: line.split('=')[1] for line in txt}
Below avoids duplication but is even longer:
...
4
votes
1answer
207 views
Convert array of u32 to Vec<u8> in Rust
In Rust, I want to take an array of u32 values, convert each to four bytes in big endian, and concatenate them to yield a ...
7
votes
3answers
1k views
Counting words from stored .md files
The following searches recursively for all the mark down files - i. ending with the extension .md - inside a folder. It then stores the text of the files in an ...
2
votes
1answer
39 views
Get all routes that start with rt_
The code reads all my routes and return to my JQUERY AJAX the names of the routes without duplicates and just those that have "rt_" prefix.
...
3
votes
1answer
160 views
Rust shortest way to find the maximum product of a fixed length substring
I'm trying to code in a Rust a function, that gives the maximum product of adjacent digits.
So given the string 123456789, with 3 adjacent digits, the maximum is <...
8
votes
1answer
87 views
base64 iterators
Was a tiny bit bored reading authentication protocols.
Needed to clear the mind and read some base64 encode text.
So I implemented these iterators that will encode or decode base64 text.
Not sure ...
2
votes
0answers
38 views
Async linked list to and from async iterable
I'm working on some stuff that works better with (immutable async) linked lists such as this:
...
3
votes
1answer
53 views
Extending method chaining IEnumerable with Add..If
I'm trying to write an extension (method chaining) for playing around a bit.
What I made work is an extension to add an item to the list (what is pretty easy):
...
2
votes
1answer
93 views
Generate binary image using coordinate vectors
I have a matrix M of size [n_rows, n_cols] and a list of x and ...
0
votes
0answers
49 views
Rust Iterator repeat_each
repeat_each transforms an iterator by repeating each value count times. It should be equivalent to ...
4
votes
0answers
210 views
Deep map, Python
Goal: apply fn to every element of an arbitrarily nested iterable (tuple, list, dict, np.ndarray), including to iterables. Ex:
...
3
votes
2answers
101 views
Deep len, Python
Goal: find total number of elements in a nested iterable of arbitrary depth. My shot:
...
2
votes
2answers
103 views
0
votes
1answer
54 views
Ensure queue is in ascending order
The program reads in a queue of integers and returns determines whether or not it is valid (ascending).
How can this code be improved?
I assume iterator include statement is unnecessary?
...
2
votes
1answer
51 views
Iterator-Pattern for an iterator that returns one objects that changes
I wrote an algorithm that returns all k-sized subgraphs of another given graph. Because I don't want to make the runtime of the next() call of the iterator depended on k, I don't want to return a new ...
1
vote
4answers
459 views
Exercise: Design a Stack
Exercise: Design a Stack
A Stack is a data structure for storing a list of elements in a LIFO
(last in, first out) fashion. Design a class called Stack with three
methods.
...
5
votes
1answer
115 views
`zip` operator to iterate on multiple container in a sign
I worked out a zip operator similar to Python's, because I didn't find one in std. It allows to use range-based ...
2
votes
1answer
77 views
template iterable numeric range class with multiple min/max inclusion modes
Problem my code solves:
Lack of lightweight numeric range classes which can tell if arbitrary values lie inside them, supporting more than just simple exclusive max modes and integers, and that ...
3
votes
1answer
80 views
Splits up two comma separated items, if they exist
This method splits up two comma separated items, if they exist, and increments the amount if the are already in the list; otherwise add the item to the list
I want to know if this code can be ...
2
votes
1answer
68 views
Compare length of iterator with as few `next` calls as possible. How can I deduplicate this code?
I'm writing a few simple functions that compare the length of an iterator to an integer, returning as early as possible. Right now I have:
...
0
votes
1answer
44 views
c# QuadTree yielding properly [closed]
I have implemented a QuadTree of my own, and I'm afraid I didn't use yield properly when I query my tree, my fear is that I create O(HN) iterators.
Could you direct me how to better improve the ...
3
votes
0answers
49 views
Populate a list with combinations of one- and two- object functions
I have a nested set of for loops which achieves the desired outcome, but I don't know if it is the most efficient / pythonic way of populating this list.
I would ...
9
votes
2answers
623 views
Manipulating list values in python
I wrote a function that I use to preprocess pandas dataframes before running them through a machine learning model. The function works perfectly, however I don't think it's the most pythonic way to ...
4
votes
1answer
60 views
Summation Over Lines in a File (Advent of Code 2018 Day 1)
Preface: I am learning Rust through the Advent of Code
The task is to read lines from a file, parse each as an integer, then provide a summation of all the numbers. My solution looks like this:
<...
3
votes
1answer
115 views
Iterate through Expression Tree
I'm trying to build an iterator (enumerator) that can select specific elements in an Expression tree by traversing the tree and deferring further iteration until ...
8
votes
2answers
2k views
C++17 zip iterator compatible with std::sort
Here are ~100 lines of code implementing three classes (ZipRef, ZipIter, Zip) which should ...
10
votes
2answers
879 views
Iterator for traversing a tree [v2]
this is an update of the implementation after I followed the feedback from Iterator for traversing a tree. After that I did changed quite a bit on the design and decided to ask for another round of ...
3
votes
1answer
87 views
4
votes
1answer
85 views
1
vote
1answer
31 views
Bidirectional numeric (floating point) sequence iterator
I wanted a neat iterator that will always give me neat sequence of step spaced numbers. This is what I came up with, but I am a bit skeptical that I covered all ...
2
votes
0answers
463 views
Combining two nested for loops into one [closed]
Description
What this does is it takes a randomly generated 64x64 stack and checks
for overlap within the 64x64 array. Here is an example:
https://imgur.com/a/kkPRzhn. Each color is its own ...
2
votes
1answer
222 views
A simple priority queue in Java via linked list sorted by priority keys
Now I have this very simple priority queue. add and changePriority both run in \$\mathcal{O}(n)\$ and ...
1
vote
1answer
61 views
A GapHelper static class to create unique (non-existing) size restricted names
I am working with a 3th party library which has following interface defined for a processing task:
...
7
votes
1answer
248 views
Map unique raw words to a list of code words
Problem
Write a function that replaces the words in raw with the words in code_words such that the first occurrence of each ...
3
votes
1answer
72 views
Aggregation on list forEach and maps improvals
I am using a function in a SpringBoot @RestController, with Lombok
This function is an intermediate result that permit me to do an aggregation between two classes.
This 2 classes are:
...
1
vote
1answer
151 views
c++ python like range function to use in for loops
It's a simple range function that works like python range and it is only a syntactic sugar for the usual for loop
if we want to operate on numbers in the range from 1 to 10 (1 ... 9) we use this code ...
7
votes
3answers
2k views
LINQ Extension methods MinBy and MaxBy
LINQ Extension
I present a LINQ method ArgBy that returns the item of a sequence that corresponds to the predicate specified. Two additional methods ...
8
votes
2answers
335 views
Doubly-linked-list implementation in C++
I am fairly new to C++ and have been trying to make a doubly-linked-list class that imitates std::list (though not all of it). I'd appreciate if someone can look at my code and let me know if I have ...
3
votes
2answers
88 views
A basic trivia app
The app grabs 10 questions from an API, displays them one by one with 4 multiple choice answers. These answers are clickable buttons that alert to whether the answer is correct or not. Finally the ...
7
votes
2answers
421 views
Python-style Generator in Java
I have implemented Python-style generators within Java. I might be reinventing the wheel here, but I feel that the ability to define a Generator with an anonymous class is the most flexible approach. ...
2
votes
1answer
71 views
C++ singly linked list with iterator
I have written a simple singly linked list in C++. I have previously had a data structure in an interview criticised for being too C-like (no iterators and using new/delete) instead of smart pointers. ...
2
votes
1answer
151 views
Add method to Ruby's Enumerable module that produces every nth element
The task is to enhance the Enumerable module with a method nth().
The method shall create a new list, which contains every nth-...
6
votes
0answers
132 views
General algorithm to calculate sums of all subsets of a given sequence of numbers
Background
A recent question Print sums of all subsets made its way to the Hot Network Questions list. The problem is simple:
Print sums of all subsets of a given set
Given an array of ...
5
votes
2answers
79 views
Filename sequencer with outdated C++
My path to learning C++ has been a rough one. Currently, I'm writing a project and my knowledge is very outdated. While I can write code that accomplishes the tasks required, I'd like to improve and ...
2
votes
2answers
63 views
Implementation of enumerate
I'm trying to implement the enumerate built-in using iterators. So far it works but for unordered objects, such as sets or dictionaries, I'm not sure if this implementation is considered valid, ...