In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable:
for (Object o : list) {
doStuff(o);
}
However, Enumerable still does not implement Iterable, meaning that do iterate over an Enumeration you must do the following:
for(; e.hasMoreElements() ;) {
doStuff(e.getNextElement());
...
Hi, I know it's unlikely but maybe there is someone who knows haXe language.
I have a variable of Dynamic type and I know for sure one of it's fields, lets call it an 'a' actually is an array. But when I'm writing
var d : Dynamic = getDynamic();
for (t in d.a) {
}
I get a compilation error on line two, saying 'You can't iterate on a ...
Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn't find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like:
def iterlen(x):
n = 0
try:
while True:
next(x)
n += 1
except StopIt...
Hi, I am used to that Python allows some neat tricks to delegate functionality to other objects. One example is delegation to contained objects.
But it seams, that I don't have luck, when I want to delegate __contains __:
class A(object):
def __init__(self):
self.mydict = {}
self.__contains__ = self.mydict.__contains_...
I am a beginner and I cannot understand the real effect of the Iterable interface.
...
I have an interface for a variety of classes, all of which should implement Iterator, so I have something like
public interface A extends Iterable<A> { ...otherMethods()... }
For the concrete classes, however, this means I must use
public class B implements A { public Iterator<A> iterator() {...} }
when I'd prefer (or at least, thi...
If I have a collection, such as Collection<String> strs, how can I get the first item out? I could just call an Iterator, take its first next(), then throw the Iterator away. Is there a less wasteful way to do it?
...
I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I've reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable.
Essentially in the following code, the uncom...
I have a class MyMap which wraps TreeMap.
(Say it's a collection of dogs and that the keys are strings).
public class MyMap {
private TreeMap<String, Dog> map;
...
}
I would like to turn MyMap iterable with the for-each loop. I know how I would've done it if my class was a LinkedList wrapper:
public class MyList implements Iterabl...
Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined.
I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level.
Any other ideas?
...
I've got a set of rows in a database, and I'd like to provide an interface to spin through them like this:
def findAll: Iterable[MyObject]
Where we don't require having all the instances in memory at once. In C# you can easily create generators like this using yield, the compiler takes care of converting code that loops through the r...
Hello! This is a problem from euler-project. No.13
import math
#no.13
sum = []
number = 0
a = 3710728753390210279879799822083759024651013574025046376937677490009712648124896970078050417018260538743249861995247410594742333095130581237266173096299194221336357416157252243056330181107240615490825023067588207539346171171980310421047513778...
So lets say I have an incredibly nested iterable of lists/dictionaries. I would like to print them to a file as easily as possible. Why can't I just redirect print to a file?
val = print(arg)
gets a SyntaxError.
Is there a way to access stdinput?
And why does print take forever with massive strings? Bad programming on my side for ...
I'm modifying some code that calls enumerate on a list declared via a list comprehension e.g.
self.groups = [Groups(self, idx) for idx in range(n_groups)]
then later:
for idx, group in enumerate(self.groups):
# do some stuff
but when I change the enumerate call to start at the 2nd list element via the start parameter e.g.
for...
I read http://stackoverflow.com/questions/839178/why-is-javas-iterator-not-an-iterable and http://stackoverflow.com/questions/27240/why-arent-enumerations-iterable, but I still don't understand why this:
void foo(Iterator<X> it) {
for (X x : it) {
bar(x);
baz(x);
}
}
was not made possible. In other words, unless I'm missin...
I know that Scala's Lists have a map implementation with signature (f: (A) => B):List[B] and a foreach implementation with signature (f: (A) => Unit):Unit but I'm looking for something that accepts multiple iterables the same way that the Python map accepts multiple iterables.
I'm looking for something with a signature of (f: (A,B) => C...
I'm using PDO inside a database abstraction library function query that I've made.
I'm using fetchAll(), which if you have a lot of results, is supposed to get memory intensive, so I want to provide an argument to toggle between a fetchAll associative array and a pdo result set that can be iterated over with foreach and requires less me...
Many Java framework classes implement Iterable, however String does not. It makes sense to iterate over characters in a String, just as one can iterate over items in a regular array.
Is there a reason why String does not implement Iterable?
...
Could you let me know how I can optimize the following code?
def f(y, list_or_elem):
if getattr(list_or_elem, '__iter__'):
y = max(y, *list_or_elem)
else:
y = max(y, list_or_elem)
...
Hi, I wrote this code:
public class C1 implements Iterable<NC1> {
private LinkedList<NC1> list;
public static class NC1 {
...
}
...
x public Iterator<NC1> iterator() {
return list.iterator();
}
}
but eclipse whines (at the x-ed line):
- The return type is incompatib...