Saturday, June 21, 2008

Google App Engine

How I started up Google App Engine for the first time:

In /tmp/googleappengine [where I had unzipped SDK]
./dev_appserver.py /home/peter/appengine/helloworld/

Luckily, I already have Python 2.5.1 installed.
Also, /home/peer/appengine/helloworld/ is a directory with a valid YAML file.

Monday, January 21, 2008

Colon notation

A:Inc(5) is treated like A.Inc(A,5).

where A is an object which has the Inc function and Inc expects two arguments (an object and an integer).

---

function T:F(X) is equivalent to
function T.F(self, X) which is equivalent to
T.F = function (self, X)

Thursday, January 17, 2008

The following two tables are equivalent:
NTI = {["John"] = "rhythm guitar"}
NTI = {John = "rhythm guitar"}

if the key is a valid identifier, it can be written out without the [" "] around it.

ipairs loops through arrays
pairs loops through non-arrays (and loops through in an arbitrary order)

functions inside an object are called methods.

Wednesday, January 16, 2008

Tables

"If a function call is used as the value of an explicit key ({K = F()}, for example), it's adjusted to one return value. If it's used as the value of an implicit integer key, it's only adjusted to one return value if it's not the last thing in the table constructor; if it's the last thing, no adjustment is made."

> function ReturnNothing ()
>> end

> function ReturnThreeVals ()
>> return "x", "y", "z"
>> end

> TblA = {ReturnThreeVals (), ReturnThreeVals()}
> print (TblA[1], TblA[2], TblA[3], TblA[4])
x x y z

> TblC = {ReturnThreeVals (), ReturnNothing ()}
> print (TblC[1], TblC[2], TblC[3], TblC[4])
x nil nil nil

TblC[2] is nil because it is set to the result of ReturnNothing (). 3 and 4 are nil because they were not set.

If a table has a "gap" (a nil in element 1 or later, but not all elements are nil) then the length operator "#" is not defined.

Common bugs - global instead of local

"There are two ways to accidentally use a global when a local was intended. One is to misspell the name of a local variable. The other is to forget the local keyword. Both of these are common sources of bugs."


"If you are ever in doubt about the scope of a variable, start from the statement where the variable's name is used and search upwards for the following:


- A
local statement that creates a variable of this name or a local function statement that creates a function of this name.

- A function definition (a function statement, local function statement, or function expression) that uses this name as a formal argument

- A for loop that uses this name as a loop variable

The first one of these that you run into whose scope extends to the statement where you started searching is the place where your (local) variable was created. If your search hits the top of the file without finding anything, the variable is global."

Upvalues

From Beginning Lua Programming:

"When a function uses a variable that is local to a containing scope ... that variable is called an external local variable or an upvalue. ... A function that has one or more upvalues is called a closure." In the following code N is an upvalue.

> function MakeLessThan(N)
>> return function (X)
>> return X < N
>> end
> end

> LessThanFive = MakeLessThan(5)
> print (LessThanFive(4))
true

You can also call a function directly, without giving it a name.
> print (MakeLessThan(5) (4))
true

Lua local functions

In the following snippet, the "end)(2,3)" need to be on the same line for the code to work. Not sure why, maybe Lua interprets the (2,3) on a separate line as a possible function call. The error call says "ambiguous syntax".

> function(A,B)
>> print(A+B)
>> end)(2,3)

The above works, the below doesn't.

>function(A,B)
>> print(A+B)
>> end)
>> (2,3)

According to Beginning Lua Programming "... Lua doesn't allow a newline before the open parenthesis of a function call. [page 107]"