... until the collector arrives ...

This "blog" is really just a scratchpad of mine. There is not much of general interest here. Most of the content is scribbled down "live" as I discover things I want to remember. I rarely go back to correct mistakes in older entries. You have been warned :)

2012-03-26

JScript vs. Function Expressions

In Javascript, a top-level function expression is not allowed. For example,

function(){return 3;}

will give a syntax error such as Unexpected token (. The work-around is to enclose the expression in parentheses:

(function(){return 3;})

At least, in most environments this is a work-around. In JScript, both expressions are quietly ignored. No error message is given, and no return value results. In JScript, a more elaborate work-around is required:

[function(){return 3;}][0]

or

0?0:function(){return 3;}

... anything that includes the function expression as part of a wider expression will do.

At this point, one might be wondering why anyone would care. What's the use of a top-level expression anyway? In my case, the context was one that used eval. The contents of a Javascript file were being loaded and evaluated where the contract was for the file to contain a single anonymous function expression.

2012-03-12

Changing the SA password in SQL Server

If you attempt to change the SQL Server SA password through the SQL Server Management Studio, you may get this error message:

Cannot set a credential for principal 'sa'.

The workaround is to tick the checkbox labelled Map to Credential. There is no need to actually select a credential from the drop-down list.

2012-03-06

More Leaks in Mathematica's Function Abstraction

I wrote earlier about problems with Mathematica's pure functions. The problems make the abstraction somewhat leaky. Here are some more problems with that abstraction:

Contrast the return values of the following expressions:

Function[expr, With[{x = 1}, expr]][x]
(* x *)

Function[Null, With[{x = 1}, #]][x]
(* 1 *)

With[{x = 1}, #]&[x]
(* 1 *)

Tracing reveals that Function is sometimes renaming variables:

Trace @ Function[expr,With[{x=1},expr]][x]
(* {Function[expr,With[{x=1},expr]][x], With[{x$=1},x], x} *)

Trace @ Function[Null,With[{x=1},#]][x]
(* {Function[Null,With[{x=1},#1]][x], With[{x=1},x], 1} *)

This renaming of variables within enclosed scoping constructs is somewhat erratic. It happens if the function arguments are named but not if the arguments are anonymous. The differences in behaviour can be exploited to good effect, to be sure, but they are certainly not intuitive. It is not clear to me which of these behaviours is "correct". But it is even more unclear why the behavioural difference is triggered by the naming of function arguments. This is another example of subtle Mathematica behaviour for beginners to trip over -- and experienced users too.

Function with named arguments will rename variables in inner With and Module constructs, but not Block constructs. The exception for Block makes sense given that it does not actually introduce any new variables, it just overrides the definitions of existing ones. However, Function with anonymous arguments does not rename variables in any of these constructs.

It is possible to "trick" Function so that it does not rename variables of enclosed scoping constructs. The trick is to disguise those constructs. For example:

Function[expr, With@@Hold[{x = 1}, expr]][x]
(* 1 *)

In this case, Function does not spot the inner With since it is not generated until a later evaluation.

Note that Function does not rename (non-pattern) symbols in replacement expressions, irrespective of whether the function arguments are named or not. For example, both of the following expressions return the same result:

Function[expr, expr /. x -> 1][x]
(* 1 *)

Function[Null, # /. x -> 1][x]
(* 1 *)

Maybe all of this behaviour can be divined from Variables in Pure Functions in the Mathematica help. But it didn't occur to me.

Singleton Tables in SQL Server

In SQL Server, you can create a "singleton" table (a table with one row) like this:

CREATE TABLE configurationData
( id AS 1 PRIMARY KEY
, properties NVARCHAR(MAX) NOT NULL
)

The primary key is a computed column with a constant value. Consequently, any attempt to insert a second row into the table will be greeted with a constraint violation error.

Note that this method only ensures that there cannot be two or more rows in the table -- it does not prevent the table from being empty.

Blog Archive