diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-11-13 14:48:48 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-11-13 14:48:48 -0200 |
commit | 758e330d6e5ef83c1fc3bfd58c3d626d5968d12c (patch) | |
tree | cf2f0e850c6e3ea198401c9a42679852026c245f | |
parent | 8e3bd752bbd9a629cb6367db4a4237514709ca75 (diff) | |
download | lua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.tar.gz lua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.tar.bz2 lua-758e330d6e5ef83c1fc3bfd58c3d626d5968d12c.zip |
new functions "getn" and "foreachi"
-rw-r--r-- | manual.tex | 196 |
1 files changed, 143 insertions, 53 deletions
@@ -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 | |||
1883 | the arguments given by the table \verb|arg|. | 1883 | the arguments given by the table \verb|arg|. |
1884 | The call is equivalent to | 1884 | The 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} |
1888 | If \verb|arg.n| is not defined, | 1888 | where \verb|n| is the result of \verb|getn(arg)| \see{getn}. |
1889 | then Lua stops getting arguments at the first \nil\ value. | ||
1890 | 1889 | ||
1891 | By default, | 1890 | By default, |
1892 | all results from \verb|func| are just returned by the call. | 1891 | all 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} |
1901 | a = call(sin, {5}) --> a = 0.0871557 = sin(5) | 1900 | a = call(sin, {5}) --> a = 0.0871557 = sin(5) |
1902 | a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) | 1901 | a = call(max, {1,4,5; n=2}) --> a = 4 (only 1 and 4 are arguments) |
1902 | a = call(max, {1,4,5; n=2}, "p") --> a = {4; n=1} | ||
1903 | t = {x=1} | 1903 | t = {x=1} |
1904 | a = call(next, {t,nil;n=2}, "p") --> a={"x", 1; n=2} | 1904 | a = 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. | |||
1926 | An optional argument, \verb|limit|, is a number that | 1926 | An optional argument, \verb|limit|, is a number that |
1927 | makes the next cycle occur only after that number of new | 1927 | makes the next cycle occur only after that number of new |
1928 | objects have been created. | 1928 | objects have been created. |
1929 | If absent, Lua uses an adaptive algorithm to set | 1929 | If \verb|limit| is absent or equal to 0, |
1930 | this limit. | 1930 | Lua uses an adaptive algorithm to set this limit. |
1931 | \verb|collectgarbage| is equivalent to | 1931 | \verb|collectgarbage| is equivalent to |
1932 | the API function \verb|lua_collectgarbage|. | 1932 | the API function \verb|lua_collectgarbage|. |
1933 | 1933 | ||
@@ -1997,46 +1997,6 @@ otherwise the semantics of \verb|nextvar| is undefined. | |||
1997 | 1997 | ||
1998 | This function cannot be written with the standard API. | 1998 | This function cannot be written with the standard API. |
1999 | 1999 | ||
2000 | \subsubsection*{\ff \T{foreach (table, function)}}\Deffunc{foreach} | ||
2001 | Executes the given \verb|function| over all elements of \verb|table|. | ||
2002 | For each element, the function is called with the index and | ||
2003 | respective value as arguments. | ||
2004 | If the function returns any non-\nil\ value, | ||
2005 | the loop is broken, and the value is returned | ||
2006 | as the final value of \verb|foreach|. | ||
2007 | |||
2008 | This function could be defined in Lua: | ||
2009 | \begin{verbatim} | ||
2010 | function 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 | ||
2017 | end | ||
2018 | \end{verbatim} | ||
2019 | |||
2020 | \subsubsection*{\ff \T{foreachvar (function)}}\Deffunc{foreachvar} | ||
2021 | Executes \verb|function| over all global variables. | ||
2022 | For each variable, | ||
2023 | the function is called with its name and its value as arguments. | ||
2024 | If the function returns any non-nil value, | ||
2025 | the loop is broken, and the value is returned | ||
2026 | as the final value of \verb|foreachvar|. | ||
2027 | |||
2028 | This function could be defined in Lua: | ||
2029 | \begin{verbatim} | ||
2030 | function 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 | ||
2037 | end | ||
2038 | \end{verbatim} | ||
2039 | |||
2040 | \subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} | 2000 | \subsubsection*{\ff \T{tostring (e)}}\Deffunc{tostring} |
2041 | Receives an argument of any type and | 2001 | Receives an argument of any type and |
2042 | converts it to a string in a reasonable format. | 2002 | converts it to a string in a reasonable format. |
@@ -2104,12 +2064,12 @@ Issues an \emph{``assertion failed!''} error | |||
2104 | when its argument is \nil. | 2064 | when its argument is \nil. |
2105 | This function is equivalent to the following Lua function: | 2065 | This function is equivalent to the following Lua function: |
2106 | \begin{verbatim} | 2066 | \begin{verbatim} |
2107 | function 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 |
2112 | end | 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)}. | |||
2181 | Copies all tag methods from one tag to another; | 2141 | Copies all tag methods from one tag to another; |
2182 | it returns \verb|tagto|. | 2142 | it returns \verb|tagto|. |
2183 | 2143 | ||
2144 | \subsubsection*{\ff \T{getn (table)}}\Deffunc{getn}\label{getn} | ||
2145 | Returns the ``size'' of a table, when seen as a list. | ||
2146 | If the table has an \verb|n| field with a numeric value, | ||
2147 | this is its ``size''. | ||
2148 | Otherwise, the size is the largest numerical index with a non-nil | ||
2149 | value in the table. | ||
2150 | This 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} | ||
2166 | Executes the given \verb|function| over all elements of \verb|table|. | ||
2167 | For each element, the function is called with the index and | ||
2168 | respective value as arguments. | ||
2169 | If the function returns any non-\nil\ value, | ||
2170 | the loop is broken, and the value is returned | ||
2171 | as the final value of \verb|foreach|. | ||
2172 | |||
2173 | This 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} | ||
2187 | Executes the given \verb|function| over the | ||
2188 | numerical indices of \verb|table|. | ||
2189 | For each index, the function is called with the index and | ||
2190 | respective value as arguments. | ||
2191 | Indices are visited in sequential order, | ||
2192 | from 1 to \verb|n|, | ||
2193 | where \verb|n| is the result of \verb|getn(table)| \see{getn}. | ||
2194 | If the function returns any non-\nil\ value, | ||
2195 | the loop is broken, and the value is returned | ||
2196 | as the final value of \verb|foreachi|. | ||
2197 | |||
2198 | This 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} | ||
2211 | Executes \verb|function| over all global variables. | ||
2212 | For each variable, | ||
2213 | the function is called with its name and its value as arguments. | ||
2214 | If the function returns any non-nil value, | ||
2215 | the loop is broken, and the value is returned | ||
2216 | as the final value of \verb|foreachvar|. | ||
2217 | |||
2218 | This 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} | ||
2231 | Sorts table elements in ascending order, \emph{in-place}, | ||
2232 | from \verb|table[1]| to \verb|table[n]|, | ||
2233 | where \verb|n| is the result of \verb|getn(table)| \see{getn}. | ||
2234 | If \verb|comp| is given, | ||
2235 | it must be a function that compares two table elements, | ||
2236 | and 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). | ||
2238 | If \verb|comp| is not given, | ||
2239 | the standard \verb|<| Lua operator is used instead. | ||
2240 | |||
2241 | Function \verb|sort| returns the (sorted) table. | ||
2242 | |||
2243 | This 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} |
2186 | This library provides generic functions for string manipulation, | 2276 | This library provides generic functions for string manipulation, |