lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


it should be easy to setup tag-methods for that, so that you can write:

fib[0] = 1
fib[1] = 1
fib.fn = function(n) return fib[n-1]+fib[n-2] end

now you only need to set tagmethods so that every time a value is not found
in the table, it is generated from the "fn" function entry.

Cheers,
Peter

> -----Original Message-----
> From: owner-lua-l@tecgraf.puc-rio.br
> [mailto:owner-lua-l@tecgraf.puc-rio.br]On Behalf Of rhq093s@tninet.se
> Sent: Monday, March 04, 2002 7:59 PM
> To: Multiple recipients of list
> Subject: Misc. request was: Faster tag methods
>
>
> On the subject of requests. Use of pattern matching in
> definitions of lua functions
> would be nice. I was thinking of something of the order of the following:
>
> function fib(0) return 1 end
>
> function fib(1) return 1 end
>
> function fib(n)
> 	return fib(n-1)+fib(n-2)
> end
>
> print fib(5)
>
> It's easy to write a preprocessor to do the transformation but it
> would be nice if it fit
> into the base language.
>
> MvH Dan Andersson