idioms
Best practice for long string literals in Go
This is what I do: q := `UPDATE mytable SET (I, Have, Lots, Of, Fields) = ` + `(‘suchalongvalue’, ` + `’thisislongaswell’, ` + `’wowsolong’, ` + `loooooooooooooooooooooooooong’)` db.Exec(q) I think it looks a lot cleaner
What’s the closest thing in C++ to retroactively defining a superclass of a defined class?
You can do the following: class C { struct Interface { virtual void bar() = 0; virtual ~Interface(){} }; template <class T> struct Interfacer : Interface { T t; Interfacer(T t):t(t){} void bar() { t.bar(); } }; std::unique_ptr<Interface> interface; public: template <class T> C(const T & t): interface(new Interfacer<T>(t)){} void bar() { interface->bar(); } }; … Read more
How do I manipulate $PATH elements in shell scripts?
Addressing the proposed solution from dmckee: While some versions of Bash may allow hyphens in function names, others (MacOS X) do not. I don’t see a need to use return immediately before the end of the function. I don’t see the need for all the semi-colons. I don’t see why you have path-element-by-pattern export a … Read more
Seeking constructive criticism on monad implementation
Stylistically this is very nice. In the real world, I would expect a 60% chance of this notation instead of the one you gave: C x c >>= f = C (value $ f x) (c + 1) But that is so minor it is hardly worth mentioning. On a more serious note, not stylistic … Read more
Why implicitly check for emptiness in Python? [closed]
This best practice is not without reason. When testing if object: you are basically calling the objects __bool__ method, which can be overridden and implemented according to object behavior. For example, the __bool__ method on collections (__nonzero__ in Python 2) will return a boolean value based on whether the collection is empty or not. (Reference: … Read more
Proper way to dynamically add functions to ES6 classes
Your solution is fine, though it’ll be better to create all those methods once on a prototype level: [‘GET’, ‘PUT’, ‘POST’, ‘DEL’].forEach((method) => { Executor.prototype[method] = function (body) { return this.request(method, body) } }) prototype approach is slightly faster, because this code is executed only once, while constructor code is executed every time new instance … Read more
Hashes of Hashes Idiom in Ruby?
You can pass the Hash.new function a block that is executed to yield a default value in case the queried value doesn’t exist yet: h = Hash.new { |h, k| h[k] = Hash.new } Of course, this can be done recursively. There’s an article explaining the details. For the sake of completeness, here’s the solution from … Read more
Alternative to the `match = re.match(); if match: …` idiom?
I don’t think it’s trivial. I don’t want to have to sprinkle a redundant conditional around my code if I’m writing code like that often. This is slightly odd, but you can do this with an iterator: import re def rematch(pattern, inp): matcher = re.compile(pattern) matches = matcher.match(inp) if matches: yield matches if __name__ == … Read more
Most idiomatic way to convert None to empty string? [closed]
Probably the shortest would be str(s or ”) Because None is False, and “x or y” returns y if x is false. See Boolean Operators for a detailed explanation. It’s short, but not very explicit.