Questions tagged [iterable]

An iterable is an object, such as a string or collection, that can be iterated over, yielding up its members one at a time.

1303 questions
10
votes
4 answers

Why is BitSet not Iterable?

BitSet has a stream() method but it does not implement the Iterable interface like other types that provide this method. Is there a specific reason for this?
Raffi Khatchadourian
  • 3,042
  • 3
  • 31
  • 37
10
votes
11 answers

Java: why are iterators not copyable

I would think that Iterator.copy() would be quite a handy function. You could implement iterator filters in a much better way. For example, the only reason in Googles Java Collection for the filter (and similar) functions to use UnmodifiableIterator…
Albert
  • 65,406
  • 61
  • 242
  • 386
10
votes
3 answers

Should Iterator or Iterable be used when exposing internal collection items?

I have a class with a private mutable list of data. I need to expose list items given following conditions: List should not be modifiable outside; It should be clear for developers who use getter function that a list they get can not be…
kza
  • 1,590
  • 13
  • 32
10
votes
1 answer

main loop 'builtin_function_or_method' object is not iterable

I get this error "main loop 'builtin_function_or_method' object is not iterable" when I run the code below: I have search stackoverflow, but cant find a answer to my question... I have checked for typos, but cant find any error. Please help…
Isak La Fleur
  • 4,428
  • 7
  • 34
  • 50
10
votes
3 answers

Implementing Iterable

How to implement Scala equivalent to Java Iterable and C# IEnumerable? Basically, I want my collection to be mappable, filterable etc. What traits should the collection class extend and are there easy ways (something like yield return and…
synapse
  • 5,588
  • 6
  • 35
  • 65
10
votes
3 answers

Caching a generator

A recent similar question (isinstance(foo, types.GeneratorType) or inspect.isgenerator(foo)?) got me curious about how to implement this generically. It seems like a generally-useful thing to have, actually, to have a generator-type object that…
Corley Brigman
  • 11,633
  • 5
  • 33
  • 40
9
votes
2 answers

What is the difference between iterable and enumerable in JS? I am going through For/of and For/In loop and these terms are coming up frequently

I am coming across terms Iterable and Enumerable while studying For/in and For/of loops. Objects are supposed be enumerable and we have to use For/in loop to loop over the properties of the object and For/of to loop over the arrays and strings. I…
Mahwash
  • 91
  • 1
  • 4
9
votes
5 answers

"Pythonic" way to return elements from an iterable as long as a condition based on previous element is true

I am working on some code that needs to constantly take elements from an iterable as long as a condition based on (or related to) the previous element is true. For example, let's say I have a list of numbers: lst = [0.1, 0.4, 0.2, 0.8, 0.7, 1.1,…
Shaun Han
  • 2,676
  • 2
  • 9
  • 29
9
votes
3 answers

In JavaScript, should an iterable be repeatedly iterable?

I found that some iterable can be repeatedly iterable: const iterable = { [Symbol.iterator]: function* () { yield 1; yield 3; yield 5; …
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
9
votes
2 answers

What is Iterables in PHP and why we use it?

I Just heard about Iterables from PHP 7.1 docs. But didn't got its actual use case and neither the concept is clear for me. so can anyone explain it with some easy example to grab it faster? I want to know why and where we use it? What are the…
Naveen Giri
  • 535
  • 6
  • 17
9
votes
1 answer

Why `__iter__` does not work when defined as an instance variable?

If I define the __iter__ method as follows, it won't work: class A: def __init__(self): self.__iter__ = lambda: iter('text') for i in A().__iter__(): print(i) iter(A()) Result: t e x t Traceback (most recent call last): File…
AXO
  • 8,198
  • 6
  • 62
  • 63
9
votes
5 answers

Python filter / max combo - checking for empty iterator

(Using Python 3.1) I know this question has been asked many times for the general question of testing if iterator is empty; obviously, there's no neat solution to that (I guess for a reason - an iterator doesn't really know if it's empty until it's…
max
  • 49,282
  • 56
  • 208
  • 355
9
votes
1 answer

convert list of data object to csv

I'm using python 2.7.6. I would like to convert my list of objects into csv format. I have a list of cdr object, this object contains some string, int and datatime object. class cdr(): def __init__(self): # some init def…
kklw
  • 858
  • 3
  • 13
  • 28
9
votes
3 answers

What exactly does "iterable" mean in Python? Why isn't my object which implements `__getitem__()` an iterable?

First I want to clarify, I'm NOT asking what is "iterator". This is how the term "iterable" is defined in Python's doc: iterable An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as…
laike9m
  • 18,344
  • 20
  • 107
  • 140
9
votes
2 answers

Continue until all iterators are done Python

I cannot use itertools So the coding seems pretty simple, but I'm having trouble thinking of the algorithm to keep a generator running until all iterations have been processed fully. The idea of the function is to take 2 iterables as parameters…
FlyingBumble
  • 195
  • 1
  • 11