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
6
votes
3answers
1k views

Counting words from stored .md files

The following searches recursively for all the .md files inside a folder. It then stores the text of the files in an array. Finally, it sums their word count using ...
2
votes
1answer
33 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. ...
2
votes
0answers
28 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
74 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
31 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
52 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
91 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
25 views

Rust Iterator repeat_each

repeat_each transforms an iterator by repeating each value count times. It should be equivalent to ...
4
votes
0answers
163 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
98 views

Deep len, Python

Goal: find total number of elements in a nested iterable of arbitrary depth. My shot: ...
2
votes
2answers
80 views

Fibonacci iterator

I've implemented the following Fibonacci iterator: lib.rs ...
0
votes
1answer
52 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
49 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
310 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
88 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
74 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
79 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
67 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
43 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
619 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
53 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
90 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 ...
7
votes
2answers
1k 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
800 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
79 views

Iterator for traversing a tree

What I want to achieve is the following: ...
4
votes
1answer
78 views

Paginate IEnumerable<T> sequence

I used the following to split sequence into batches: ...
1
vote
1answer
28 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
237 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
94 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
60 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
235 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
118 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
247 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
84 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
290 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
64 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
131 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
127 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
58 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, ...
5
votes
3answers
417 views

SkipLast of an IEnumerable<T> - Linq Extension

As my answer to this question, I came up with this solution: ...
4
votes
1answer
51 views

Using best practice with complex iteration

I'm new to Rust so I'm still learning how to use best practice in complex examples. Below is some code I've used in one of my programs, a simple implementation of a standard neural network. Here is ...
4
votes
1answer
114 views

Email Generator

I've made a email generator, that uses a HTML template and replaces keywords in the template with information taken from a database. I'm looking to see if there is a way to improve the coding. ...
3
votes
1answer
70 views

C function to fully consume a Python iterator

I was reading into the proper way to consume an iterator on this question as well as here and I notices there is no actual proper function for this that clearly explains itself. Using: ...
2
votes
1answer
51 views

Rendering JSON arrays of unequal length as a table

I have a website where my users can create "table-like" layout dynamically. So imagine that my users can upload a text document, and then append columns to the document, and the output will then be ...
4
votes
1answer
80 views

Iterable hashtable implementation in C

I'm just finished my version of iterable hashtable. I want to review code in general (code style, data structures, remarks about the algorithms) and I have some questions: I don't know how to ...
1
vote
1answer
61 views

Replace values in R data frame according to values in other data frame

I would like to change row names values from a first data frame if these rows names are present in another data frame, and change it to a corresponding value (defined in the second data frame). I ...

1
2 3 4 5
8