Asynchronous iterator Task

A more “batteries-included” implementation of this kind of thing, including language support, is now available as of C# 8.0. Now, when using at least C# 8.0 (or higher) with .NET Standard 2.1 (or higher) and/or .NET Core 3.0 (or higher), the code from the original question may be written as follows: private async IAsyncEnumerable<char> TestAsync(string … Read more

How get next (previous) element in std::list without incrementing (decrementing) iterator?

Yes, since C++11 there are the two methods you are looking for called std::prev and std::next. You can find them in the iterator library. Example from cppreference.com #include <iostream> #include <iterator> #include <list> int main() { std::list<int> v{ 3, 1, 4 }; auto it = v.begin(); auto nx = std::next(it, 2); std::cout << *it << … Read more

What is the relation between Iterable and Iterator?

In short: An Iterator does have state, whereas an Iterable does not. See the API docs for both. Iterable: A base trait for iterable collections. This is a base trait for all Scala collections that define an iterator method to step through one-by-one the collection’s elements. […] This trait implements Iterable’s foreach method by stepping … Read more

hasnext() for Python iterators?

The alternative to catching StopIteration is to use next(iterator, default_value). For example: >>> a = iter(‘hi’) >>> print(next(a, None)) h >>> print(next(a, None)) i >>> print(next(a, None)) None This way you can check for None to see if you’ve reached the end of the iterator if you don’t want to do it the exception way. … Read more

Flattening iterator

I don’t know of any implementation in a major library, but it looked like an interesting problem so I wrote a basic implementation. I’ve only tested it with the test case I present here, so I don’t recommend using it without further testing. The problem is a bit trickier than it looks because some of … Read more

iterrows pandas get next rows value

Firstly, your “messy way” is ok, there’s nothing wrong with using indices into the dataframe, and this will not be too slow. iterrows() itself isn’t terribly fast. A version of your first idea that would work would be: row_iterator = df.iterrows() _, last = row_iterator.next() # take first item from row_iterator for i, row in … Read more

How to do multiple imports in Python?

For known module, just separate them by commas: import lib1, lib2, lib3, lib4, lib5 If you really need to programmatically import based on dynamic variables, a literal translation of your ruby would be: modnames = “lib1 lib2 lib3 lib4 lib5”.split() for lib in modnames: globals()[lib] = __import__(lib) Though there’s no need for this in your … Read more