Determine if a string contains only alphanumeric characters (or a space)
Looks good to me, but you can use isalnum(c) instead of isalpha and isdigit.
Looks good to me, but you can use isalnum(c) instead of isalpha and isdigit.
Is it possible to nevertheless loop over entries in some way? No, as you say, the contents of a WeakMap are not accessible by design, and there is no iterability. If not … how does the Chrome console do this? The console uses the debugging API of the JS engine, which allows access to the … Read more
Rust 1.26 fn filter_one(input: &[u8]) -> impl Iterator<Item = &u8> { input.iter().filter(|&&x| x == 1) } fn main() { let nums = vec![1, 2, 3, 1, 2, 3]; let other: Vec<_> = filter_one(&nums).collect(); println!(“{:?}”, other); } Rust 1.0 fn filter_one<‘a>(input: &’a [u8]) -> Box<Iterator<Item = &’a u8> + ‘a> { Box::new(input.iter().filter(|&&x| x == 1)) } … Read more
The following Python lines should be equivalent to what you have (assuming func, or lambda in your code, returns a Boolean): # Any contains = any(func(x) for x in enumerable) # Count count = sum(func(x) for x in enumerable) # Distinct: since we are using a custom comparer here, we need a loop to keep … Read more
Depends on the container. e.g. if it’s a vector, after modifying the container all iterators can be invalidated. However, if it’s a list, the iterators irrelevant to the modified place will remain valid. A vector’s iterators are invalidated when its memory is reallocated. Additionally, inserting or deleting an element in the middle of a vector … Read more
Iterating over two things simultaneously is called a “zip” (from functional programming), and Boost has a zip iterator: The zip iterator provides the ability to parallel-iterate over several controlled sequences simultaneously. A zip iterator is constructed from a tuple of iterators. Moving the zip iterator moves all the iterators in parallel. Dereferencing the zip iterator … Read more
The reason behind the preincrement being faster is that post-increment has to make a copy of the old value to return. As GotW #2 put it, “Preincrement is more efficient than postincrement, because for postincrement the object must increment itself and then return a temporary containing its old value. Note that this is true even … Read more
Create a custom iterator which implement the AutoCloseable interface public interface CloseableIterator<T> extends Iterator<T>, AutoCloseable { } And then use this iterator in a try with resource statement. try(CloseableIterator iterator = dao.findAll()) { while(iterator.hasNext()){ process(iterator.next()); } } This pattern will close the underlying resource whatever happens: – after the statement complete – and even if … Read more
Perhaps a bit more theoretical. Mathematically, collections in C++ can be described as a half-open interval of iterators, namely one iterator pointing to the start of the collection and one iterator pointing just behind the last element. This convention opens up a host of possibilities. The way algorithms work in C++, they can all be … Read more
One option is to use Guava: ImmutableList<Character> chars = Lists.charactersOf(someString); UnmodifiableListIterator<Character> iter = chars.listIterator(); This produces an immutable list of characters that is backed by the given string (no copying involved). If you end up doing this yourself, though, I would recommend not exposing the implementation class for the Iterator as a number of other … Read more