I want to implement a method dim(x,y) which will assign spaces for a matrix(y rows, x cols).
I want to make "dim(x,y)" more powerful by passing an optional function 'filler' to it and then 'dim' will set the element located at (x,y) to filler(x,y)
my code goes as below:
List2D dim := method(x, y, z,
target := list()
filler := if(z == nil,
method(return nil),
z)
for(i, 1, y,
subTarget := list()
for(j, 1, x,
subTarget append( filler(i,j) ))
target append(subTarget) )
return target)
it worked well when 'dim' is called with 2 arguments, but failed with
List2D dim(3,2, method(x,y, 10*x+y))
which throwed an exception at line filler := if(z == nil
The exception said nil does not respond to '*'
I realized the argument 'z' got activated undesirably when comparing with nil.
So I'm wondering how to get my 'List2D dim' work properly?