iterable

Length of generator output [duplicate]

你。 提交于 2019-11-26 06:32:37
问题 This question already has answers here : Getting number of elements in an iterator in Python (16 answers) What's the shortest way to count the number of items in a generator/iterator? (6 answers) Closed last year . Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn\'t find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

核能气质少年 提交于 2019-11-26 06:28:58
问题 Both of these interfaces define only one method public operator fun iterator(): Iterator<T> Documentation says Sequence is meant to be lazy. But isn\'t Iterable lazy too (unless backed by a Collection )? 回答1: The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T> . For Sequence<T> , the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { .

Why does Stream<T> not implement Iterable<T>?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 06:04:35
问题 In Java 8 we have the class Stream<T>, which curiously have a method Iterator<T> iterator() So you would expect it to implement interface Iterable<T>, which requires exactly this method, but that\'s not the case. When I want to iterate over a Stream using a foreach loop, I have to do something like public static Iterable<T> getIterable(Stream<T> s) { return new Iterable<T> { @Override public Iterator<T> iterator() { return s.iterator(); } }; } for (T element : getIterable(s)) { ... } Am I

Convert Iterable to Stream using Java 8 JDK

混江龙づ霸主 提交于 2019-11-26 05:49:11
问题 I have an interface which returns java.lang.Iterable<T> . I would like to manipulate that result using the Java 8 Stream API. However Iterable can\'t \"stream\". Any idea how to use the Iterable as a Stream without converting it to List? 回答1: There's a much better answer than using spliteratorUnknownSize directly, which is both easier and gets a better result. Iterable has a spliterator() method, so you should just use that to get your spliterator. In the worst case, it's the same code (the

In Python, how do I determine if an object is iterable?

送分小仙女□ 提交于 2019-11-26 05:42:11
问题 Is there a method like isiterable ? The only solution I have found so far is to call hasattr(myObj, \'__iter__\') But I am not sure how fool-proof this is. 回答1: Checking for __iter__ works on sequence types, but it would fail on e.g. strings in Python 2 . I would like to know the right answer too, until then, here is one possibility (which would work on strings, too): try: some_object_iterator = iter(some_object) except TypeError as te: print some_object, 'is not iterable' The iter built-in

What is the difference between iterator and iterable and how to use them?

允我心安 提交于 2019-11-26 04:58:31
问题 I am new in Java and I\'m really confused with iterator and iterable. Can anyone explain to me and give some examples? 回答1: An Iterable is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces an Iterator . An Iterator is the object with iteration state. It lets you check if it has more elements using hasNext() and move to the next element (if any) using next() .

Convert Iterable to Stream using Java 8 JDK

天大地大妈咪最大 提交于 2019-11-26 03:06:26
I have an interface which returns java.lang.Iterable<T> . I would like to manipulate that result using the Java 8 Stream API. However Iterable can't "stream". Any idea how to use the Iterable as a Stream without converting it to List? Brian Goetz There's a much better answer than using spliteratorUnknownSize directly, which is both easier and gets a better result. Iterable has a spliterator() method, so you should just use that to get your spliterator. In the worst case, it's the same code (the default implementation uses spliteratorUnknownSize ), but in the more common case, where your

Why is Java&#39;s Iterator not an Iterable?

核能气质少年 提交于 2019-11-26 02:29:23
问题 Why does the Iterator interface not extend Iterable ? The iterator() method could simply return this . Is it on purpose or just an oversight of Java\'s designers? It would be convenient to be able to use a for-each loop with iterators like this: for(Object o : someContainer.listSomeObjects()) { .... } where listSomeObjects() returns an iterator. 回答1: Because an iterator generally points to a single instance in a collection. Iterable implies that one may obtain an iterator from an object to

What exactly are iterator, iterable, and iteration?

流过昼夜 提交于 2019-11-25 21:42:32
问题 What is the most basic definition of \"iterable\", \"iterator\" and \"iteration\" in Python? I have read multiple definitions but I am unable to identify the exact meaning as it still won\'t sink in. Can someone please help me with the 3 definitions in layman terms? 回答1: Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration. In Python, iterable and iterator have specific

Understanding slice notation

…衆ロ難τιáo~ 提交于 2019-11-25 21:29:13
问题 I need a good explanation (references are a plus) on Python\'s slice notation. To me, this notation needs a bit of picking up. It looks extremely powerful, but I haven\'t quite got my head around it. 回答1: It's pretty simple really: a[start:stop] # items start through stop-1 a[start:] # items start through the rest of the array a[:stop] # items from the beginning through stop-1 a[:] # a copy of the whole array There is also the step value, which can be used with any of the above: a[start:stop