... 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-01-06

Mathematica Module vs. Block vs. With

A common Mathematica-related question concerns the difference between Module, Block, and With. Here is a small example which illustrates those differences:

x = 1; y[] := x

{ Module[{x = 2}, {"Module", x, y[], Hold[x]}]
,  Block[{x = 2}, { "Block", x, y[], Hold[x]}]
,   With[{x = 2}, {  "With", x, y[], Hold[x]}]
} // TableForm

A close study of the results might help to build an intuition about the differences:

Module  2       1       Hold[x$561]
Block   2       2       Hold[x]
With    2       1       Hold[2]

In all three cases, the local value of x is the same: 2. So all three constructs are the same with respect to the purpose of creating local variables. However, they differ in other respects.

Module

Module localizes the symbol x so that it is distinct from the global x. It does this by generating a new unique symbol and then using the new symbol in place of the original x. Thus, the Module expression is effectively the same as if we had typed:

x$561 = 2; {"Module", x$561, y[], Hold[x$561]}

The generated symbols are generally temporary and disappear when they are no longer needed, although there are circumstances in which they will stay around (both intentional and inadvertent).

Block

Block does not localize the symbol x. Instead, it temporarily clears all definitions and attributes associated with the symbol, evaluates the body, and then restores the cleared definitions and attributes. Thus, the Block expression is effectively the same as:

ClearAll[x]; x = 2; {"Block", x, y[], Hold[x]}; x = 1

Since the global value of x has been changed, y[] will return the new "local" value. This behaviour can be surprising and unexpected, so one should be sure that this is the intent before using Block -- or be careful to only block symbols that are not used in any other definitions.

With

With does not localize the symbol x either. Instead, it replaces all occurrences of x within the body of the expression with the supplied value. The body is evaluated after this replacement has occurred. Thus, the With expression effectively is the same as:

{"With", 2, y[], Hold[2]}

Note that With is unique among the three constructs in that it will insert values into held expressions (Hold[x] in this case). This has utility when generating code.

2011-12-17

jTDS vs NVARCHAR(MAX)

jTDS truncates VARCHAR and NVARCHAR stored procedure output parameter values that are longer than 4000 characters for NVARCHAR, 8000 for VARCHAR. This is a consequence of the fact that jTDS is still using the old SQL Server 2000 version of the TDS protocol. The jTDS developers are aware of this problem, but the comments in the bug report suggest that it may be a while before the problem is corrected. In the meantime, the work-around is to replace the output parameter with a single row, single column result set. If one is reluctant (or unable) to change the stored procedure, then the following sequence will work as well:

DECLARE @result NVARCHAR(MAX)
EXEC dbo.myproc(..., @result OUTPUT)
SELECT @result

2011-11-16

Iterating Over Tables/Databases in SQL Server

SQL Server provides an undocumented stored procedure that is handy for applying commands to all of the tables within a catalog, sp_MSforeachtable:

sp_MSforeachtable 'print ''?'''

The question mark will be replaced by the table name. In a similar vein, we can iterate over all databases using sp_MSforeachdb. These procedures could disappear at any time, but they have been present in all versions from 7.0 to 2008R2 so far.

Both procedures accept the following arguments:

  • @command1 - the command to run for each table
  • @replacechar - the placeholder character for the table name (default '?')
  • @command2 - another command to execute for each table
  • @command3 - another command to execute for each table
  • @precommand - a command to execute before iterating
  • @postcommand - a command to execute after iterating

2011-08-19

Lambdas In Java

With Oracle's takeover of Java, interest in functional programming appears to be renewed. As the Java language evolves in this direction, perhaps it needs a new name. How about one of these names?

  • Visual J++
  • C#
  • !C#
  • C##
  • OpenC#Beans

:)

Blog Archive