defaultdict of defaultdict?

Yes like this: defaultdict(lambda: defaultdict(int)) The argument of a defaultdict (in this case is lambda: defaultdict(int)) will be called when you try to access a key that doesn’t exist. The return value of it will be set as the new value of this key, which means in our case the value of d[Key_doesnt_exist] will be … Read more

LINQ .Any VS .Exists – What’s the difference?

See documentation List.Exists (Object method – MSDN) Determines whether the List(T) contains elements that match the conditions defined by the specified predicate. This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList) IEnumerable.Any (Extension method … Read more

Is it better to return null or empty collection?

Empty collection. Always. This sucks: if(myInstance.CollectionProperty != null) { foreach(var item in myInstance.CollectionProperty) /* arrgh */ } It is considered a best practice to NEVER return null when returning a collection or enumerable. ALWAYS return an empty enumerable/collection. It prevents the aforementioned nonsense, and prevents your car getting egged by co-workers and users of your … Read more

Retrieving a List from a java.util.stream.Stream in Java 8

What you are doing may be the simplest way, provided your stream stays sequential—otherwise you will have to put a call to sequential() before forEach. [later edit: the reason the call to sequential() is necessary is that the code as it stands (forEach(targetLongList::add)) would be racy if the stream was parallel. Even then, it will … Read more

Get nth character of a string in Swift programming language

Attention: Please see Leo Dabus’ answer for a proper implementation for Swift 4 and Swift 5. Swift 4 or later The Substring type was introduced in Swift 4 to make substrings faster and more efficient by sharing storage with the original string, so that’s what the subscript functions should return. Try it out here extension … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)