Javascript as a functional language
JavaScript supports first class functions. See Use functional programming techniques to write elegant JavaScript.
JavaScript supports first class functions. See Use functional programming techniques to write elegant JavaScript.
Unicode escape sequences allow you to store and transmit your source code in pure ASCII and still use the entire range of Unicode characters. This has two advantages: No risk of non-ASCII characters getting broken by tools that can’t handle them. This was a real concern back in the early 1990s when Java was designed. … Read more
For old-style classes, there is a difference: >>> class X: pass … >>> type(X) <type ‘classobj’> >>> X.__class__ Traceback (most recent call last): File “<stdin>”, line 1, in <module> AttributeError: class X has no attribute ‘__class__’ >>> x = X() >>> x.__class__ <class __main__.X at 0x171b5d50> >>> type(x) <type ‘instance’> The point of new-style classes … Read more
The visitor pattern is a way of doing double-dispatch in an object-oriented way. It’s useful for when you want to choose which method to use for a given argument based on its type at runtime rather than compile time. Double dispatch is a special case of multiple dispatch. When you call a virtual method on … Read more
A simple hash is close to an array. Their initializations even look similar. First the array: @last_name = ( “Ward”, “Cleaver”, “Fred”, “Flintstone”, “Archie”, “Bunker” ); Now let’s represent the same information with a hash (aka associative array): %last_name = ( “Ward”, “Cleaver”, “Fred”, “Flintstone”, “Archie”, “Bunker” ); Although they have the same name, the … Read more
I’ve just read this article from the MSDN Magazine: Building Tuple Here are excerpts: The upcoming 4.0 release of Microsoft .NET Framework introduces a new type called System.Tuple. System.Tuple is a fixed-size collection of heterogeneously typed data. Like an array, a tuple has a fixed size that can’t be changed once it … Read more
I end up having to remember what the macro is and substitute it in my head as I read. That seems to reflect poorly on the naming of the macros. I would assume you wouldn’t have to emulate the preprocessor if it were a log_function_entry() macro. The ones that I have encountered that were intuitive … Read more
Getting the current datetime as milliseconds: Date.now() For example, to time the execution of a section of code: var start = Date.now(); // some code alert((Date.now() – start) + ” ms elapsed”);
Here’s how we designed C# 4. First we made a list of every possible feature we could think of adding to the language. Then we bucketed the features into “this is bad, we must never do it”, “this is awesome, we have to do it”, and “this is good but let’s not do it this … Read more
class << self is more than just a way of declaring class methods (though it can be used that way). Probably you’ve seen some usage like: class Foo class << self def a print “I could also have been defined as def Foo.a.” end end end This works, and is equivalent to def Foo.a, but … Read more