DataAnnotations: Recursively validating an entire object graph

Here’s an alternative to the opt-in attribute approach. I believe this will traverse the object-graph properly and validate everything. public bool TryValidateObjectRecursive<T>(T obj, List<ValidationResult> results) { bool result = TryValidateObject(obj, results); var properties = obj.GetType().GetProperties().Where(prop => prop.CanRead && !prop.GetCustomAttributes(typeof(SkipRecursiveValidation), false).Any() && prop.GetIndexParameters().Length == 0).ToList(); foreach (var property in properties) { if (property.PropertyType == typeof(string) || … Read more

Recursive ConcurrentHashMap.computeIfAbsent() call never terminates. Bug or “feature”?

This is of course a “feature”. The ConcurrentHashMap.computeIfAbsent() Javadoc reads: If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null. The entire method invocation is performed atomically, so the function is applied at most once per … Read more

Why is Python recursion so expensive and what can we do about it?

The issue is that Python has an internal limit on number of recursive function calls. That limit is configurable as shown in Quentin Coumes’ answer. However, too deep a function chain will result in a stack overflow. This underlying limitation¹ applies to both C++ and Python. This limitation also applies to all function calls, not … Read more

Functional Programming – Lots of emphasis on recursion, why?

Church Turing thesis highlights the equivalence between different computability models. Using recursion we don’t need a mutable state while solving some problem, and this make possible to specify a semantic in simpler terms. Thus solutions can be simpler, in a formal sense. I think that Prolog shows better than functional languages the effectiveness of recursion … Read more

Writing foldl using foldr

Some explanations are in order! What is the id function for? What is the role of? Why should we need it here? id is the identity function, id x = x, and is used as the equivalent of zero when building up a chain of functions with function composition, (.). You can find it defined … Read more

How does the fibonacci recursive function “work”?

You’re defining a function in terms of itself. In general, fibonnaci(n) = fibonnaci(n – 2) + fibonnaci(n – 1). We’re just representing this relationship in code. So, for fibonnaci(7) we can observe: fibonacci(7) is equal to fibonacci(6) + fibonacci(5) fibonacci(6) is equal to fibonacci(5) + fibonacci(4) fibonacci(5) is equal to fibonacci(4) + fibonacci(3) fibonacci(4) is … Read more