iterable

Turn iterable into iterator

◇◆丶佛笑我妖孽 提交于 2019-12-11 05:46:55
问题 In PHP 7.1 a new iterable pseudo-type was introduced. Is there a straightforward way to transform a generic iterable into an Iterator ? Or should I use something like if (is_array($iterable)) { return new \ArrayIterator($iterable); } return new \IteratorIterator($iterable); 来源: https://stackoverflow.com/questions/43474857/turn-iterable-into-iterator

Lambda iteration over an Iterator (not an Iterable)

时光毁灭记忆、已成空白 提交于 2019-12-10 22:34:02
问题 I often read it's impossible to call Lambda functions on iterators. And I lived in that belief until now. However reading the book Professional Haxe by Franco Ponticelli and Lee-McColl-Sylvester about what makes an objet an Iterable or an Iterator made me think of one trick, that seems to work; at least in the simple case I just tested. The trick is simply to declare an iterator() function inside the Iterator class, returning itself (weird yes, but not that incoherent). I don't know if this

Python 3: apply an operator over an iterable

不羁的心 提交于 2019-12-10 20:09:24
问题 sum(iterable) is effectively: def sum(iterable): s = 0 for x in iterable: s = s.__add__(x) return s Does Python have a built-in function that accomplishes this without setting the initial value? # add is interchangeable with sub, mul, etc. def chain_add(iterable): iterator = iter(iterable) s = next(iterator) while True: try: s = s.__add__(next(iterator)) except StopIteration: return s The problem I have with sum is that it does not work for other types that support the + operator, e.g.

Uneven chunking in python

冷暖自知 提交于 2019-12-10 18:07:53
问题 Given a list of chunk sizes, how would you partition an iterable into variable-length chunks? I'm trying to coax itertools.islice without success yet. for chunk_size in chunk_list: foo(iter, chunk_size) 回答1: You need to make an iter object of your iterable so you can call islice on it with a particular size, and pick up where you left off on the next iteration. This is a perfect use for a generator function: def uneven_chunker(iterable, chunk_list): group_maker = iter(iterable) for chunk_size

how python built-in function iter() convert a python list to an iterator?

我们两清 提交于 2019-12-10 17:21:30
问题 I have read my materials, which tell that a python iterator must have both __iter__ and __next__ method, but an iterable just needs __iter__ . I check a list and find it has no __next__ method. When using iter() on it, it will become an iterator. This means that iter() will add a __next__ method to a list to convert it to an iterator? If yes, how does this happen? 回答1: No. iter returns an iterator , it does not convert the list into an iterator. It doesn't modify the list at all, and

isinstance(x, list) when iterating a list containing strings and lists

我的梦境 提交于 2019-12-10 14:16:44
问题 At Iterating nested list inside-out, I was told that "type-checking isn't Pythonic". Generally, this is true: we want to look only at the interface (duck-typing) rather than a specific type. The question asks about nested lists of the form ['a', ['c', ['e'], 'd'], 'b'] , and we specifically consider strings atomic (non-iterable). So, we can't use a blanket collections.Iterable , but on the other hand isinstance(x, list) does seem a bit hacky. My answer was def traverse(l): for x in l: if

Recommended way to implement Iterator<T> in Typescript, before ES6 [duplicate]

浪尽此生 提交于 2019-12-10 12:49:17
问题 This question already has an answer here : typescript: make class objects iterable (1 answer) Closed 2 years ago . I have a project that includes many classes that ideally would implement the Iterable<T> and/or Iterator<T> interfaces. However I cannot seem to find a standard TypeScript definition of these interfaces (for example in typescript-collections or some similar package). I understand these are somewhat standardized in ECMAScript 6 through the Symbol.iterator mechanism, but my target

java iterator/iterable subinterface

穿精又带淫゛_ 提交于 2019-12-10 09:26:22
问题 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, think I'd prefer) to use public Iterator<B> iterator() {...} so that concrete use of the class could have the explicit type (in case I wanted methods that weren't in

Lazily transpose a list in Python

﹥>﹥吖頭↗ 提交于 2019-12-09 15:01:29
问题 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 example, I wish [(1, 2, 3), (4, 5, 6), (7, 8, 9)] to be turned into [1, 4, 7] , [2, 5, 8] , [3, 6, 9] . (Except I want iterables not lists.) The standard zip(*data) idiom doesn't work, because the argument unpacking expands the entire iterable. (You can

Emulating membership-test in Python: delegating __contains__ to contained-object correctly

南楼画角 提交于 2019-12-09 12:44:19
问题 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__ a = A() 1 in a I get: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: argument of type 'A' is not iterable What I am making wrong? When I call a._