What is the Iterable interface used for?

Besides what Jeremy said, its main benefit is that it has its own bit of syntactic sugar: the enhanced for-loop. If you have, say, an Iterable<String>, you can do: for (String str : myIterable) { … } Nice and easy, isn’t it? All the dirty work of creating the Iterator<String>, checking if it hasNext(), and … Read more

Why do I get “TypeError: ‘int’ object is not iterable” when trying to sum digits of a number? [duplicate]

Your problem is with this line: number4 = list(cow[n]) It tries to take cow[n], which returns an integer, and make it a list. This doesn’t work, as demonstrated below: >>> a = 1 >>> list(a) Traceback (most recent call last): File “<stdin>”, line 1, in <module> TypeError: ‘int’ object is not iterable >>> Perhaps you … Read more

iter() not working with datetime.now()

This is definitely a bug introduced in Python 3.6.0b1. The iter() implementation recently switched to using _PyObject_FastCall() (an optimisation, see issue 27128), and it must be this call that is breaking this. The same issue arrises with other C classmethod methods backed by Argument Clinic parsing: >>> from asyncio import Task >>> Task.all_tasks() set() >>> … Read more

Chart of IEnumerable LINQ equivalents in Scala? [duplicate]

I am only listing out the equivalents of functions from Enumerable<A>. This is incomplete as of now. I will try to update this later with more. xs.Aggregate(accumFunc) -> xs.reduceLeft(accumFunc) xs.Aggregate(seed, accumFunc) -> xs.foldLeft(seed)(accumFunc) xs.Aggregate(seed, accumFunc, trans) -> trans(xs.foldLeft(seed)(accumFunc)) xs.All(pred) -> xs.forall(pred) xs.Any() -> xs.nonEmpty xs.Any(pred) -> xs.exists(pred) xs.AsEnumerable() -> xs.asTraversable // roughly xs.Average() -> xs.sum … Read more

Check if all values of iterable are zero

Use generators rather than lists in cases like that: all(v == 0 for v in values) Edit: all is standard Python built-in. If you want to be efficient Python programmer you should know probably more than half of them (http://docs.python.org/library/functions.html). Arguing that alltrue is better name than all is like arguing that C while should … Read more

Why doesn’t the String class in Java implement Iterable?

There really isn’t a good answer. An iterator in Java specifically applies to a collection of discrete items (objects). You would think that a String, which implements CharSequence, should be a “collection” of discrete characters. Instead, it is treated as a single entity that happens to consist of characters. In Java, it seems that iterators … Read more