iterator

How can I get a list from a Ruby enumerable?

I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby? I know of : elements = [] enumerable.each {|i| elements << i} I also know of the inject alternative. Is there some ready available method? ...

Unexpected results feeding Django File upload object to Python CSV module

I have no problems getting the file to upload and if I save it to disk, all formatting is intact. I wrote a function to read in the the file within Django using: data = csv.reader(f.read()) where f is the Django file object that I get from 'form.cleaned_data['file']' and yes the file is already bound to the form. When I try to read ...

Getting the Iterator for an inner STL Container?

Hey guys, I am having trouble trying to get iterators for inner sub-containers. Basically imagine this simplified code: typedef map<string, map<string, map> > double_map; double_map dm; .. //some code here for(double_map::iterator it = dm.begin(); it != dm.end(); it++){ //some code here it->first // string it->second // <...

use of goto versus runtime code evaluation

Recently for a programming class, we were given the assignment to write a program in any language that, given n, will produce all the possible derangements for an array p of size n such that p[i] != i for all i: 0 <= i < n. We had to use iterators, e.g. yield. Example: n=3, [0, 1, 2] is not a derangement, but [2, 0, 1] is as well as [...

iterator_to_array

DatePeriod is a PHP class for handling recurring dates. It has a very limited number of methods. So when I want to do basic array functions with the recurring dates, I have to copy it to an array with iterator_to_array. Strangely, copying it seems to clobber it. Any ideas why? $p=new DatePeriod(date_create('2008-01-01'), ...

Is end() required to be constant in an STL map/set?

§23.1.2.8 in the standard states that insertion/deletion operations on a set/map will not invalidate any iterators to those objects (except iterators pointing to a deleted element). Now, consider the following situation: you want to implement a graph with uniquely numbered nodes, where every node has a fixed number (let's say 4) of neig...

Iterate C++ list<derived> as base class

What I want to do is this: class Ba { } class Da : public Ba {} class Db : public Ba {} class Bb // abstract base class that must not be a template. { void Process() { list<Ba*>::iterator pos; // I known the derived class will also derive // from list<Dx*> where Dx derives from Ba for(pos = this-...

python arbitrarily incrementing an iterator inside a loop

I am probably going about this in the wrong manner, but I was wondering how to handle this in python. First some c code: int i; for(i=0;i<100;i++){ if(i == 50) i = i + 10; printf("%i\n", i); } Ok so we never see the 50's... My question is, how can I do something similar in python? For instance: for line in cdata.split('\...

iterating over each character of a String in ruby 1.8.6 (each_char)

Hello, I am new to ruby and currently trying to operate on each character separately from a base String in ruby. I am using ruby 1.8.6 and would like to do something like: "ABCDEFG".each_char do|i| puts i end This produces a undefined method `each_char' error. I was expecting to see a vertical output of: A B C D ..etc Is the each...

scala - Getting a read-only sublist view of a List

I would like a List, Seq, or even an Iterable that is a read-only view of a part of a List, in my specific case, the view will always start at the first element. List.slice, is O(n) as is filter. Is there anyway of doing better than this - I don't need any operations like +, - etc. Just apply, map, flatMap, etc to provide for list co...

Testing for an empty iterator in a Python for... loop

The code below is based on this recipe. However, the key point of the recipe - that it provides a way to break out of the iteration on an iterator if the iterator is empty - doesn't seem to work here, instead behaving in the following undesired ways: If get_yes_no_answer() == False and there are two or more items left in the iterator, ...

std::vector and its iterator as single template typename

In order to get an "easier-to-remember" interface to the index-generating function std::distance(a,b), I came up with the idea of a better distinction of it's arguments (when used against the base of a vector: vec.begin() ) by calling a templated function with the vector and its iterator, like: std::vector<MyType> vect; std::vector<My...

Renaming first and second of a map iterator

Is there any way to rename the first and second accessor functions of a map iterator. I understand they have these names because of the underlying pair which represents the key and value, but I'd like the iterators to be a little more readable. I think this might be possible using an iterator adaptor, but I'm not sure how to implement ...

'Finally' Block in Iterators

Is there any way in a C# iterator block to provide a block of code which will be run when the foreach ends (either naturally of by being broken out of), say to clean up resources? The best I have come up with is to use the using construct, which is fine but does need an IDisposable class to do the clean up. For example: public stat...

Partitioning an Iterator in scala 2.7.5

It looks as though there is no partition method on an Iterator in scala 2.7.5 (there is in 2.8). I'd like to have a partition without losing the laziness of the Iterator, so the following is not an option: itr.toList.partition( someTest(_) ) Can anyone recommend a way of doing this without implementing my own partition method? For exa...

convert a pointer to a reverse vector iterator in STL

I have sort(arr, arr+n, pred); How do I sort in reverse order? ...

Java TreeMap iterator not working correctly for String keys.

I have a TreeMap that maps String keys to a custom City class. Here is how it is instantiated: TreeMap<String, City> nameDictionary = new TreeMap<String, City>(new CityNameComparator()); CityNameComparator implementation: public class CityNameComparator implements Comparator<String> { public int compare (String c1, String c2...

why iterator.remove() has been described as optional operation?

i went through the documentation(http://java.sun.com/javase/6/docs/api/java/util/Iterator.html) of Iterator.remove() there remove() was described as void remove() Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next. The b...

N-Way Intersection of Sorted Sequential Access Iterators

I want to iterate over the intersection of n sub-iterators. Each sub-iterator is sorted from GREATEST TO LEAST, and the values must be accessed sequentially. Assume no duplicate values. So far I have thought of two approaches using a heap/priority queue, but I'm struggling to figure out which is more efficient - it seems like it may b...

Cloning iterators in Java

I have a LinkedList in Java, an iterator to browse the list and I would like to clone the iterator to do some temporary "look ahead" processing of the list with respect to the position of the original iterator. I understand that cloning an iterator is not possible in every situation, but is there a way to clone an iterator to a LinkedLi...