Quick sort at compilation time using C++11 variadic templates

Have you also looked at its memory consumption? Note that quicksort itself is worse than linear, with a quite bad worse case runtime. This multiplies with the worse than linear runtime behaviour of certain steps of template compilation and instantiation (sometimes those are exponentional). You should maybe graph your compiletime for various datasets to observe … Read more

How do I trap arguments to a target method when using a Proxy object?

There actually is a way to do this, of course! I just hadn’t thought it through thoroughly enough. I can just return a ‘proxy’ function and trap the arguments in there: var test = { doSomething: function() { console.log( arguments.length ); } }; var testProxy = new Proxy( test, { get: function( target, property, receiver … Read more

Template Metaprogramming – I still don’t get it :(

Just as factorial is not a realistic example of recursion in non-functional languages, neither is it a realistic example of template metaprogramming. It’s just the standard example people reach for when they want to show you recursion. In writing templates for realistic purposes, such as in everyday libraries, often the template has to adapt what … Read more

Python equivalent of Ruby’s ‘method_missing’

There is no difference in Python between properties and methods. A method is just a property, whose type is just instancemethod, that happens to be callable (supports __call__). If you want to implement this, your __getattr__ method should return a function (a lambda or a regular def, whatever suite your needs) and maybe check something … Read more

Is there a way to do a C++ style compile-time assertion to determine machine’s endianness?

If you’re using autoconf, you can use the AC_C_BIGENDIAN macro, which is fairly guaranteed to work (setting the WORDS_BIGENDIAN define by default) alternately, you could try something like the following (taken from autoconf) to get a test that will probably be optimized away (GCC, at least, removes the other branch) int is_big_endian() { union { … Read more

Modify default queryset in django

You can do this with a custom model manager and override the get_queryset function to always filter canceled=False. class CustomManager(models.Manager): def get_queryset(self): return super().get_queryset().filter(canceled=False) class MyModel(models.Model): # Blah blah objects = CustomManager() Then when calling MyModel.objects.all() it will always exclude canceled objects. Here is a blog post I found helpful on the subject. http://www.b-list.org/weblog/2006/aug/18/django-tips-using-properties-models-and-managers/ EDIT: … Read more

tech