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.
391
questions
10
votes
2answers
2k views
Using a Pythonesque range() generator function with the Java foreach loop
Now that we have the nice new foreach loop in Java, the old-style loop looks ugly be comparison.
I like the way Python has a ...
2
votes
2answers
15k views
Iterator for binary tree - pre, in, and post order iterators
Implemented iterator for a binary tree and "pre" "in" and "post" order flavors. I'm looking for code review, best practices, optimizations etc.
...
8
votes
2answers
901 views
Iterator class using a square list
I am trying to implement squarelist by using iteration that points to the head element. I've create this iterator class to make my iterator id directional. I just want to see if I implemented it ...
3
votes
2answers
11k views
Creating linked list data with iterator pattern
I've created a linked list to use it in SquareList. I've started to create a linked list based on just that. I want my class to follow the iterator pattern. How can it be improve to use vector ...
3
votes
1answer
238 views
Serializing tabular data in ruby — is map, flatten, hash the correct approach?
I wanted a hash which lets me reference Japanese syllables by their romanized names. In hindsight I could have searched for an existing one column table, but I wanted to improve my ruby by writing a ...
1
vote
0answers
585 views
REST Response: Checking for Success and Error
I thought this was pretty slick - maybe not, I just wanted to share and maybe get some feedback:
As part of a REST client class:
Accepts the JSON response from REST or REST-like service, searches ...
11
votes
1answer
2k views
Iterator adaptor for iterators returning tuple-like values
Today, I will require your help to improve an iterator adapter. The goal of the adapter (I called it get_iterator) is to adapt ...
2
votes
1answer
19k views
Nested select Linq
Is there a simpler and optimized way to calculate listAdminCorePriveleges and privileges ?
...
7
votes
1answer
40k views
Iterator Implementation
I had to write my own iterator implementation for my structure which has ArrayList<Vector> field. An iterator is supposed to iterate over mentioned ...
3
votes
3answers
25k views
Check IEnumerable Count
Let's say I have a collection of expensive-to-generate objects stored in an IEnumerable:
...
1
vote
1answer
5k views
Filtering a long list of files through a set of ignore patterns using iterators
I have a backup job that walks across a huge directory with potentially millions of files. Python's os.walk() works just fine.
Now, I have implemented a feature to ignore files based in a black list ...
6
votes
3answers
10k views
Flatten iterator for nested list
Given a list which can can contain elements as well as lists, write an iterator to flatten a nested list. Please make this code better and suggest any improvements.
...
3
votes
2answers
248 views
Remove item from a list, which (of the 2) methods is better?
I have a List, and I have to delete one item from the list when I find it. I am using the code below to do that:
...
29
votes
3answers
24k views
“Zip-like” functionality with C++11's range-based for-loop
My goal was to get the following code to work:
...
3
votes
1answer
118 views
copy_if_max() algorithm
I need a copy_if_max() function, i.e. copy all the elements in an input range that are equal to the max in that range. The most obvious implementation does two ...
2
votes
1answer
1k views
Refactor IQueryable and IEnumerable that share the same condition
I have extension methods to encapsulate query conditions, however I have to separate IQueryable and IEnumerable. Is there any ...
2
votes
3answers
1k views
Is this a correct recursive Scala iterator creation?
I am diving into Scala (using 2.10) headfirst and am trying out a simple task of taking bulk data and then doing something with that stream. The first part is generating that stream. I have seem to be ...
3
votes
1answer
3k views
Creating custom random access iterator in C++ 2011
I would like an opinion on my code. I need a random access iterator for my custom container.
If I read the C++ 2011 standard, specially chapter 24, I understood that an implementation
could be the ...
2
votes
1answer
229 views
How to remove goto statement from iterator
I'm implementing a simple iterator in C# over an xml document, in order to process an xml document in as much a streaming manner as possible. For this reason, I'm using an XmlReader variable in a <...
8
votes
1answer
672 views
Python reversed in C++11
I was playing with some of the wonderful new C++11 features and tried to reimplement a function reversed that would behave just like the one in Python to allow ...
5
votes
3answers
6k views
Most efficient way to iterate through arrays/can I use .times method in place of while method?
I created a quick program to test the precision of randomness generated by the .rand method. The primary question is whether I can use the ...
5
votes
1answer
15k views
How to make an iteration in a for-loop faster?
I have a list with contentlets and want all languages from every contentlet (I get these with the method ...
7
votes
1answer
2k views
Setting output width with ostream_iterator
I'd like to display a vector (implemented in my custom Vector class) and have the output width of the elements be configurable. I've done this by creating a simple ...
4
votes
1answer
1k views
Iterator and Generator versions of Python's range()
I have created iterator and generator versions of Python's range():
Generator version
...
2
votes
1answer
756 views
Polymorphic STL foreach without passing the container type
I was trying to figure out how to make a breakable foreach macro for STL containers and I came up with this method that uses templates to recognize the container type automatically. Are there any ...
3
votes
3answers
4k views
Converting base-10 numbers into base-26 letters
I'm writing something that basically takes a bunch of files and renames them into a nice format, such as pic_000.jpeg, ...
68
votes
6answers
3k views
infix_iterator code
I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up here for peer review first, and see ...
3
votes
2answers
1k views
Leap year program
I'm switching to Ruby from Java and so, for exercise purposes, I wrote this verbose leap year Ruby program. The comments express my concerns about Ruby best practices. I'd be glad if some Ruby ...
5
votes
3answers
4k views
Generic cached IEnumerable<T>
I've an IEnumerable<T> source which takes time to generate each item. Once an item is generated, I want to cache it to avoid recomputing the same thing. I ...
2
votes
4answers
729 views
Iterator-like interface for extracting records from a blob
I have a blob whose format is something like:
(id1,key1,value1)(id2,key2,value2)....
where id is a uint32_t, and key and value are null-terminated strings. I am ...
1
vote
1answer
3k views
Tortoise and hare cycle detection algorithm using iterators in Python
Reading the article in Wikipedia about cycle detection I came across a relatively small code snipped of Floyd's cycle-finding algorithm. I'll put it here to keep everything in one place:
...
1
vote
1answer
153 views
Iterating through 128-byte records in a file
I need to read records from a flat file, where each 128 bytes constitutes a logical record. The calling module of this below reader does just the following.
...
5
votes
1answer
116 views
3
votes
2answers
451 views
Iterating over unequal unordered pairs in one collection
This code kinda hurts my feelings -- everything else in my code is done in neat one-liners, exploiting algorithms and, sometimes boost::bind, except for this piece. ...
11
votes
2answers
37k views
How can I avoid unchecked cast warning in my generic recursive Iterator?
It's somewhat odd that Java's collection framework has no iterator for recursive data structures. Since I needed something like this, I wrote my own. First off, I need recursive elements:
...
3
votes
4answers
4k views
Iterator pattern/iterator class
I have implemented a simple iterator. Just want to check if I have missed anything.
...
6
votes
1answer
3k views
Iterator over row and columns
I am writing a simple raytracer just for fun. I want to iterate over rows, and each row must be an iterator over columns. A strategy I produced is the following:
...
4
votes
3answers
561 views
2
votes
1answer
168 views
Can you make this Key-interable view of a List of Maps better?
I have a list of map entries, and I need an iterable that returns the keys.
Of course, we could be naive and copy over into a new collection of the desired type, but that's inefficient.
So let's ...
4
votes
3answers
3k views
15
votes
2answers
628 views
Sudoku Grid special purpose Iterators
Please have a look at these iterators which I use for my Sudoku solver. They behave slightly different from STL iterators and don't implement all functionality that would be needed to use them in a ...