Iterable objects and array type hinting?

I think you mean instanceof Iterator, PHP doesn’t have an Iterable interface. It does have a Traversable interface though. Iterator and IteratorAggregate both extend Traversable (and AFAIK they are the only ones to do so). But no, objects implementing Traversable won’t pass the is_array() check, nor there is a built-in is_iterable() function. A check you … Read more

JavaScript – Nuances of myArray.forEach vs for loop

The most substantive difference between the for loop and the forEach method is that, with the former, you may break out of the loop. You can simulate continue by simply returning from the function passed to forEach, but there is no way to stop looping altogether. Aside from that, the two accomplish effectively the same … Read more

Creating my own Iterators

/EDIT: I see, an own iterator is actually necessary here (I misread the question first). Still, I’m letting the code below stand because it can be useful in similar circumstances. Is an own iterator actually necessary here? Perhaps it’s sufficient to forward all required definitions to the container holding the actual Points: // Your class … Read more

Convert Iterator to List

Better use a library like Guava: import com.google.common.collect.Lists; Iterator<Element> myIterator = … //some iterator List<Element> myList = Lists.newArrayList(myIterator); Another Guava example: ImmutableList.copyOf(myIterator); or Apache Commons Collections: import org.apache.commons.collections.IteratorUtils; Iterator<Element> myIterator = …//some iterator List<Element> myList = IteratorUtils.toList(myIterator);

What is an idiomatic way to collect an iterator of &T into a collection of Ts?

Is a bicycle an idiomatic way to get from one city to another? Like most things in software (and life), it depends. If your type implements Copy I’d prefer these in this order: some_iter.copied() some_iter.cloned() some_iter.map(|&v| v) some_iter.map(|v| *v) some_iter.map(Clone::clone) some_iter.map(|v| v.clone()) If your type implements Clone I’d prefer these in this order: some_iter.cloned() some_iter.map(Clone::clone) … Read more

Iterate twice on values (MapReduce)

Unfortunately this is not possible without caching the values as in Andreas_D’s answer. Even using the new API, where the Reducer receives an Iterable rather than an Iterator, you cannot iterate twice. It’s very tempting to try something like: for (IntWritable value : values) { // first loop } for (IntWritable value : values) { … Read more

How to iterate across lines in two files simultaneously?

Python 2: Use itertools.izip to join the two iterators. from itertools import izip for line_from_file_1, line_from_file_2 in izip(open(file_1), open(file_2)): If the files are of unequal length, use izip_longest. In Python 3, use zip and zip_longest instead. Also, use a with to open files, so that closing is handled automatically even in case of errors. with … Read more

What is iterator invalidation?

Iterators are glorified pointers. Iterator invalidation is a lot like pointer invalidation; it means it suddenly points to junk data. Because it’s very natural but wrong to do things like this: for(iterator it = map.begin(); it != map.end(); ++it) { map.erase(it->first); // whoops, now map has been restructured and iterator // still thinks itself is … Read more

tech