How do I avoid unwrap when converting a vector of Options or Results to only the successful values?

I want to ignore all Err values Since Result implements IntoIterator, you can convert your Vec into an iterator (which will be an iterator of iterators) and then flatten it: Iterator::flatten: vec.into_iter().flatten().collect() Iterator::flat_map: vec.into_iter().flat_map(|e| e).collect() These methods also work for Option, which also implements IntoIterator. You could also convert the Result into an Option and … Read more

C++ template typename iterator

In list<tNode<T>*>::iterator, you have a dependant name, that is, a name that depends on a template parameter. As such, the compiler can’t inspect list<tNode<T>*> (it doesn’t have its definition at this point) and so it doesn’t know whether list<tNode<T>*>::iterator is either a static field or a type. In such a situation, the compiler assumes that … 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

RecursiveIteratorIterator and RecursiveDirectoryIterator to nested html lists

Here is one using DomDocument The basic idea is the contents of each directory is represented by a <ul> and each element in the directory by a <li> If element is a non-empty directory it will contain a <ul> to represent its contens and so on. $path = $_SERVER[‘DOCUMENT_ROOT’].’/test’; $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); … Read more