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
17
votes
5 answers

Contains method for Iterable and Iterator?

Is there a simple method to check if an element is contained in an iterable or iterator, analogous to the Collection.contains(Object o) method? I.e. instead of having to write: Iterable data = getData(); for (final String name : data) { …
Roland
  • 7,525
  • 13
  • 61
  • 124
16
votes
5 answers

Suppressing treatment of string as iterable

UPDATE: An idea to make built-in strings non-iterable was proposed on python.org in 2006. My question differs in that I'm trying to only suppress this features once in a while; still this whole thread is quite relevant. Here are the critical…
max
  • 49,282
  • 56
  • 208
  • 355
16
votes
3 answers

Implement Java Iterator and Iterable in same class?

I am trying to understand Java Iterator and Iterable interfaces I am writing this class class MyClass implements Iterable { public String[] a = null; public MyClass(String[] arr) { a = arr; } public…
icn
  • 17,126
  • 39
  • 105
  • 141
16
votes
1 answer

Getting attribute error: 'map' object has no attribute 'sort'

I am trying to sort array in increasing order. But getting the following error for the code: a = [] a = map(int, input().split(' ')) a.sort() print(a) Error: AttributeError: 'map' object has no attribute 'sort'
Sushil Chaskar
  • 181
  • 1
  • 1
  • 8
15
votes
1 answer

Type Hint for finite iterable

My function foo accepts an argument things which is turned into a list internally. def foo(things): things = list(things) # more code The list constructor accepts any iterable. However, annotating things with typing.Iterable does not give…
actual_panda
  • 1,178
  • 9
  • 27
15
votes
1 answer

Is the arguments object supposed to be an iterable in ES6?

In ES6, I was trying to use the arguments object as an iterable when passed to the Set constructor. It works fine in IE11 and in Chrome 47. It does not work in Firefox 43 (throws a TypeError: arguments is not iterable). I've looked through the…
jfriend00
  • 683,504
  • 96
  • 985
  • 979
15
votes
1 answer

Simplest way to stream an iterator

Say you want to stream the elements of an iterator; let's use a concrete example of a Scanner, which implements Iterator. Given an Iterator, say: // Scanner implements Iterator Iterator scanner = new…
Bohemian
  • 412,405
  • 93
  • 575
  • 722
15
votes
4 answers

Why is there a method iterator() on java.util.Collection

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…
Dan
  • 9,681
  • 14
  • 55
  • 70
15
votes
2 answers

Is there any official contract for the Iterable interface with respect to multiple usage?

Since Java 5, we have the new java.lang.Iterable type that can be used in foreach loops as such: for (Object element : iterable); The Iterable contract does not specify whether its iterator() method can be called more than once before disposing of…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
14
votes
3 answers

Scala: Exposing a JDBC ResultSet through a generator (iterable)

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…
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
13
votes
2 answers

Separating the __iter__ and __next__ methods

In Python 3, it is standard procedure to make a class an iterable and iterator at the same time by defining both the __iter__ and __next__ methods. But I have problems to wrap my head around this. Take this example which creates an iterator that…
DocDriven
  • 3,726
  • 6
  • 24
  • 53
13
votes
4 answers

Convert first N item in iterable to Array

Something similar to question Convert ES6 Iterable to Array. But I only want first N items. Is there any built-in for me to do so? Or how can I achieve this more elegantly? let N = 100; function *Z() { for (let i = 0; ; i++) yield i; } // This wont…
tsh
  • 4,263
  • 5
  • 28
  • 47
13
votes
4 answers

C++: How to pass any iterable type as a function parameter

As an exercise I'm trying to implement Python's str.join method in C++. I will eventually add the function as a method of the std::string class but I figure getting it to work is more of a priority. I've defined the function as…
aydow
  • 3,673
  • 2
  • 23
  • 40
13
votes
10 answers

"Iterable cannot be cast to List" - Isn't `List` a type of `Iterable`?

I called a getElements method which returns Iterable. I did this: List elements = (List) getElements(); This generates the error: java.lang.ClassCastException: com.utesy.Element$3 cannot be cast to java.util.List I…
snoopy
  • 603
  • 2
  • 6
  • 12
12
votes
1 answer

python make class iterable by returning embedded iterable

I have a class in python, which has an iterable as instance variable. I want to iterate the instances of the class by iterating over the embedded iterable. I implemented this as follows: def __iter__(self): return self._iterable.__iter__() I…
jzwiener
  • 8,182
  • 4
  • 21
  • 23