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
12
votes
3 answers

Is there a macro for creating fast Iterators from generator-like functions in julia?

Coming from python3 to Julia one would love to be able to write fast iterators as a function with produce/yield syntax or something like that. Julia's macros seem to suggest that one could build a macro which transforms such a "generator" function…
schlichtanders
  • 313
  • 1
  • 7
12
votes
1 answer

Recommended way to implement Iterator in Typescript, before ES6

I have a project that includes many classes that ideally would implement the Iterable and/or Iterator interfaces. However I cannot seem to find a standard TypeScript definition of these interfaces (for example in typescript-collections or some…
Alon
  • 699
  • 2
  • 9
  • 17
12
votes
5 answers

How to put string in a set as an individual item?

I have a string sample = "http://www.stackoverflow.com" I want convert this string to a set final = {"http://www.stackoverflow.com"} I tried the following code: final = set(sample) But i got the wrong out as {u'.', u'/', u':', u'a', u'b', u'c',…
crax
  • 546
  • 2
  • 9
  • 20
12
votes
1 answer

Lazily transpose a list in Python

So, I have an iterable of 3-tuples, generated lazily. I'm trying to figure out how to turn this into 3 iterables, consisting of the first, second, and third elements of the tuples, respectively. However, I wish this to be done lazily. So, for…
TLW
  • 1,373
  • 9
  • 22
12
votes
7 answers

How does a Python for loop with iterable work?

I'm confused by Python's for loop syntax. Consider a code example like: for party in feed.entry: print(party.location.address.text) What does for party in feed.entry actually mean, given that feed.entry is an iterable? Step by step, what…
amit
11
votes
2 answers

Getting the last element of a lazy Seq in Raku

I want to get the last element of a lazy but finite Seq in Raku, e.g.: my $s = lazy gather for ^10 { take $_ }; The following don't work: say $s[* - 1]; say $s.tail; These ones work but don't seem too idiomatic: say (for $s<> { $_ }).tail; say…
Nikola Benes
  • 2,372
  • 1
  • 20
  • 33
11
votes
5 answers

Why are Iterable and Iterator in different packages?

Iterable is in java.lang whereas Iterator is in java.util. Is there a good reason for this or is this merely an artifact of bad design? It seems strange since the only thing that an Iterable is good for is providing an Iterator. EDIT:…
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
11
votes
4 answers

tqdm: extract time passed + time remaining?

I have been going over the tqdm docs, but no matter where I look, I cannot find a method by which to extract the time passed and estimated time remaining fields (basically the center of the progress bar on each line: 00:00<00:02). 0%| |…
Coolio2654
  • 1,589
  • 3
  • 21
  • 46
11
votes
3 answers

mapstruct map iterable to non iterable

I want to map a list of Objects to an Object that contains a list: public class Group { private List people; } public class Person { private String name; } I tried creating a mapper like this: Group toGroup(List
John Ellis
  • 173
  • 2
  • 2
  • 7
11
votes
1 answer

PHP generator return type

I've never used generators in PHP before and there are no examples in the documentation that show the return type declaration. In PhpStorm, there is an error in the IDE when I do this: public function getDataIncrementally(): void { yield from…
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
11
votes
2 answers

Why does Javascript `iterator.next()` return an object?

Help! I'm learning to love Javascript after programming in C# for quite a while but I'm stuck learning to love the iterable protocol! Why did Javascript adopt a protocol that requires creating a new object for each iteration? Why have next() return…
11
votes
3 answers

TypeError: 'type' object is not iterable - Iterating over object instances

I am working on a project and I would like to make one of my classes iterable. To the best of my knowledge I can do that with using metaclass. First of all I would like to understand how metaclass works. Therefore I would like to present my own…
mirind4
  • 1,423
  • 4
  • 21
  • 29
10
votes
1 answer

Iterate over arbitrary-length tuple

I just started with Scala and ran into a problem: Scala has the Types Tuple1, Tuple2, …, Tuple22. Scalaquery returns tuples when iterating over queries. I have now a given class (ZK’s ListitemRenderer), which accepts Objects and populates gui lists…
flying sheep
  • 8,475
  • 5
  • 56
  • 73
10
votes
5 answers

list() vs iterable unpacking in Python 3.5+

Is there any practical difference between list(iterable) and [*iterable] in versions of Python that support the latter?
Roman Odaisky
  • 2,811
  • 22
  • 26
10
votes
2 answers

Make class castable to both tuple and dict

I would like to define a class so that its instances can be casted to both tuple and dict. An example: class Point3: ... p = Point(12, 34, 56) tuple(p) # gives (12, 34, 56) dict(p) # gives { 'x': 12, 'y': 34, 'z': 56 } I have found that if…
md2perpe
  • 3,372
  • 2
  • 18
  • 22