aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-11-13 14:48:48 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-11-13 14:48:48 -0200
commit758e330d6e5ef83c1fc3bfd58c3d626d5968d12c (patch)
treecf2f0e850c6e3ea198401c9a42679852026c245f
parent8e3bd752bbd9a629cb6367db4a4237514709ca75 (diff)
downloadlua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.tar.gz
lua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.tar.bz2
lua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.zip
new functions "getn" and "foreachi"
-rw-r--r--manual.tex196
1 files changed, 143 insertions, 53 deletions
diff --git a/manual.tex b/manual.tex
index 55aaf3f8..8a6925b8 100644
--- a/manual.tex
+++ b/manual.tex
@@ -1,4 +1,4 @@
1% $Id: manual.tex,v 1.18 1998/08/21 17:43:44 roberto Exp roberto $ 1% $Id: manual.tex,v 1.19 1998/08/24 20:14:56 roberto Exp roberto $
2 2
3\documentclass[11pt]{article} 3\documentclass[11pt]{article}
4\usepackage{fullpage,bnf} 4\usepackage{fullpage,bnf}
@@ -41,7 +41,7 @@ Waldemar Celes
41\tecgraf\ --- Computer Science Department --- PUC-Rio 41\tecgraf\ --- Computer Science Department --- PUC-Rio
42} 42}
43 43
44%\date{\small \verb$Date: 1998/08/21 17:43:44 $} 44%\date{\small \verb$Date: 1998/08/24 20:14:56 $}
45 45
46\maketitle 46\maketitle
47 47
@@ -1883,10 +1883,9 @@ Calls function \verb|func| with
1883the arguments given by the table \verb|arg|. 1883the arguments given by the table \verb|arg|.
1884The call is equivalent to 1884The call is equivalent to
1885\begin{verbatim} 1885\begin{verbatim}
1886 func(arg[1], arg[2], ..., arg[arg.n]) 1886 func(arg[1], arg[2], ..., arg[n])
1887\end{verbatim} 1887\end{verbatim}
1888If \verb|arg.n| is not defined, 1888where \verb|n| is the result of \verb|getn(arg)| \see{getn}.
1889then Lua stops getting arguments at the first \nil\ value.
1890 1889
1891By default, 1890By default,
1892all results from \verb|func| are just returned by the call. 1891all results from \verb|func| are just returned by the call.
@@ -1900,6 +1899,7 @@ For instance, the following calls produce the following results:
1900\begin{verbatim} 1899\begin{verbatim}
1901a = call(sin, {5}) --> a = 0.0871557 = sin(5) 1900a = call(sin, {5}) --> a = 0.0871557 = sin(5)
1902a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) 1901a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments)
1902a = call(max, {1,4,5; n=2}, "p") --> a = {4; n=1}
1903t = {x=1} 1903t = {x=1}
1904a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} 1904a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2}
1905\end{verbatim} 1905\end{verbatim}
@@ -1926,8 +1926,8 @@ Returns the number of objects collected.
1926An optional argument, \verb|limit|, is a number that 1926An optional argument, \verb|limit|, is a number that
1927makes the next cycle occur only after that number of new 1927makes the next cycle occur only after that number of new
1928objects have been created. 1928objects have been created.
1929If absent, Lua uses an adaptive algorithm to set 1929If \verb|limit| is absent or equal to 0,
1930this limit. 1930Lua uses an adaptive algorithm to set this limit.
1931\verb|collectgarbage| is equivalent to 1931\verb|collectgarbage| is equivalent to
1932the API function \verb|lua_collectgarbage|. 1932the API function \verb|lua_collectgarbage|.
1933 1933
@@ -1997,46 +1997,6 @@ otherwise the semantics of \verb|nextvar| is undefined.
1997 1997
1998This function cannot be written with the standard API. 1998This function cannot be written with the standard API.
1999 1999
2000\subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach}
2001Executes the given \verb|function| over all elements of \verb|table|.
2002For each element, the function is called with the index and
2003respective value as arguments.
2004If the function returns any non-\nil\ value,
2005the loop is broken, and the value is returned
2006as the final value of \verb|foreach|.
2007
2008This function could be defined in Lua:
2009\begin{verbatim}
2010function foreach (t, f)
2011 local i, v = next(t, nil)
2012 while i do
2013 local res = f(i, v)
2014 if res then return res end
2015 i, v = next(t, i)
2016 end
2017end
2018\end{verbatim}
2019
2020\subsubsection*{\ff \T{foreachvar (function)}}\Deffunc{foreachvar}
2021Executes \verb|function| over all global variables.
2022For each variable,
2023the function is called with its name and its value as arguments.
2024If the function returns any non-nil value,
2025the loop is broken, and the value is returned
2026as the final value of \verb|foreachvar|.
2027
2028This function could be defined in Lua:
2029\begin{verbatim}
2030function foreachvar (f)
2031 local n, v = nextvar(nil)
2032 while n do
2033 local res = f(n, v)
2034 if res then return res end
2035 n, v = nextvar(n)
2036 end
2037end
2038\end{verbatim}
2039
2040\subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} 2000\subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring}
2041Receives an argument of any type and 2001Receives an argument of any type and
2042converts it to a string in a reasonable format. 2002converts it to a string in a reasonable format.
@@ -2104,12 +2064,12 @@ Issues an \emph{``assertion failed!''} error
2104when its argument is \nil. 2064when its argument is \nil.
2105This function is equivalent to the following Lua function: 2065This function is equivalent to the following Lua function:
2106\begin{verbatim} 2066\begin{verbatim}
2107function assert (v, m) 2067 function assert (v, m)
2108 if not v then 2068 if not v then
2109 m = m or "" 2069 m = m or ""
2110 error("assertion failed! " .. m) 2070 error("assertion failed! " .. m)
2111 end 2071 end
2112end 2072 end
2113\end{verbatim} 2073\end{verbatim}
2114 2074
2115\subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error} 2075\subsubsection*{\ff \T{error (message)}}\Deffunc{error}\label{pdf-error}
@@ -2181,6 +2141,136 @@ for a given pair \M{(tag, event)}.
2181Copies all tag methods from one tag to another; 2141Copies all tag methods from one tag to another;
2182it returns \verb|tagto|. 2142it returns \verb|tagto|.
2183 2143
2144\subsubsection*{\ff \T{getn (table)}}\Deffunc{getn}\label{getn}
2145Returns the ``size'' of a table, when seen as a list.
2146If the table has an \verb|n| field with a numeric value,
2147this is its ``size''.
2148Otherwise, the size is the largest numerical index with a non-nil
2149value in the table.
2150This function could be defined in Lua:
2151\begin{verbatim}
2152 function getn (t)
2153 if type(t.n) == 'number' then return floor(t.n) end
2154 local i = next(t, nil)
2155 local max = 0
2156 while i do
2157 if type(i) == 'number' and i>max then max=i end
2158 i = next(t, i)
2159 end
2160 return floor(max)
2161 end
2162\end{verbatim}
2163
2164
2165\subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach}
2166Executes the given \verb|function| over all elements of \verb|table|.
2167For each element, the function is called with the index and
2168respective value as arguments.
2169If the function returns any non-\nil\ value,
2170the loop is broken, and the value is returned
2171as the final value of \verb|foreach|.
2172
2173This function could be defined in Lua:
2174\begin{verbatim}
2175 function foreach (t, f)
2176 local i, v = next(t, nil)
2177 while i do
2178 local res = f(i, v)
2179 if res then return res end
2180 i, v = next(t, i)
2181 end
2182 end
2183\end{verbatim}
2184
2185
2186\subsubsection*{\ff \T{foreachi (table, function)}}\Deffunc{foreachi}
2187Executes the given \verb|function| over the
2188numerical indices of \verb|table|.
2189For each index, the function is called with the index and
2190respective value as arguments.
2191Indices are visited in sequential order,
2192from 1 to \verb|n|,
2193where \verb|n| is the result of \verb|getn(table)| \see{getn}.
2194If the function returns any non-\nil\ value,
2195the loop is broken, and the value is returned
2196as the final value of \verb|foreachi|.
2197
2198This function could be defined in Lua:
2199\begin{verbatim}
2200 function foreachi (t, f)
2201 local i, n = 1, getn(t)
2202 while i<=n do
2203 local res = f(i, t[i])
2204 if res then return res end
2205 i = i+1
2206 end
2207 end
2208\end{verbatim}
2209
2210\subsubsection*{\ff \T{foreachvar (function)}}\Deffunc{foreachvar}
2211Executes \verb|function| over all global variables.
2212For each variable,
2213the function is called with its name and its value as arguments.
2214If the function returns any non-nil value,
2215the loop is broken, and the value is returned
2216as the final value of \verb|foreachvar|.
2217
2218This function could be defined in Lua:
2219\begin{verbatim}
2220 function foreachvar (f)
2221 local n, v = nextvar(nil)
2222 while n do
2223 local res = f(n, v)
2224 if res then return res end
2225 n, v = nextvar(n)
2226 end
2227 end
2228\end{verbatim}
2229
2230\subsubsection*{\ff \T{sort (table [, comp])}}\Deffunc{sort}
2231Sorts table elements in ascending order, \emph{in-place},
2232from \verb|table[1]| to \verb|table[n]|,
2233where \verb|n| is the result of \verb|getn(table)| \see{getn}.
2234If \verb|comp| is given,
2235it must be a function that compares two table elements,
2236and returns true when the first is less than the second
2237(that is, \verb|not comp(a[i+1], a[i])| will be true after the sort).
2238If \verb|comp| is not given,
2239the standard \verb|<| Lua operator is used instead.
2240
2241Function \verb|sort| returns the (sorted) table.
2242
2243This function could be defined in Lua:
2244\begin{verbatim}
2245 function aux_qsort (a, l, u, leq)
2246 if l < u then
2247 local m = floor((l+u)/2) -- choose middle element as pivot
2248 a[l], a[m] = a[m], a[l] -- swap pivot to first position
2249 local t = a[l] -- pivot value
2250 m = l
2251 local i = l+1
2252 while i <= u do
2253 -- invariant: a[l+1..m] < t <= a[m+1..i-1]
2254 if leq(a[i], t) then
2255 m = m+1
2256 a[m], a[i] = a[i], a[m] -- swap
2257 end
2258 i = i+1
2259 end
2260 a[l], a[m] = a[m], a[l] -- swap pivot to a valid place
2261 -- a[l+1..m-1] < a[m] <= a[m+1..u]
2262 aux_qsort(a, l, m-1, leq)
2263 aux_qsort(a, m+1, u, leq)
2264 end
2265 return a -- return the table
2266 end
2267
2268 function sort (a, f)
2269 f = f or function (a,b) return a<b end
2270 return aux_qsort(a, 1, getn(a), f)
2271 end
2272\end{verbatim}
2273
2184 2274
2185\subsection{String Manipulation} 2275\subsection{String Manipulation}
2186This library provides generic functions for string manipulation, 2276This library provides generic functions for string manipulation,