· 8 years ago · Jan 24, 2018, 06:52 PM
1lua tutorial notes
2
32018-01-18 -- jmcg
4
5I'm going through the Lua tutorial at https://www.tutorialspoint.com/lua
6
7My intention is to spend at least 1 hour a day going through the tutorial step-by-step, running the examples and making notes along the way.
8
9lua_overview.htm
10
11I like the way these tutorials are written in the sense that they take a common approach across many different languages (I intend to do the Go tutoral next), build up from fundamentals, with working examples along the way.
12
13What I don't like about these tutorials is that the writing is sometimes very awkward (as if translated from some language other than English) and that they contain a fair number of errors. I'm mostly planning to ignore the awkward English problems unless they truly get in the way of understanding.
14
15"It was designed from the beginning to be a software that can be integrated with the code written in C and other conventional languages." -- I take it that "software" was chosen because the writer wasn't sure what to call it. From the context, we could say that Lua was designed as a scripting language that could be integrated with programs written in C or other conventional language and could act as an extension language. There have been quite a few predecessors written with this sort of intention, with varying degrees of success. I think if Tcl as one of these examples, one of the earliest I know of, and following that Perl and Python made efforts to enable embedding them. As an alternative to the many purpose-built but small and clumsy interpreted interactive languages many tools tried to build for themselves, it seems like a great idea.
16
17One more overall comment: too many ads. I realize that you need to generate some revenue, and I can't fault you for having ads that are at least somewhat related to the subject matter of interest, but there's just too much real estate given over to ads for my taste. I'm using pi-hole to suppress them for now. [Several days later, pi-hole isn't supressing ads any more. Update: I had to explicitly turn of IPV6 at the LAN interface to stop sneaky ads from getting through.]
18
19lua_environment.htm
20
21I'm going to go ahead and install Lua on both my Xubuntu Linux system and on my Windows XP laptop (as best I can). I don't promise to try all examples in both environments. I've not used the SciTE editor much if at all, so I'll give it a spin on Windows. On Linux, I expect to stick to vi or vim, since that's what I'm most familiar and comfortable with. Somewhere along the way, I should try Notepad++ on Windows, since it surely has a Lua syntax coloring module available (but probably need to try it on Win7, since WinXP and Notepad++ plugin manager aren't happy with each other currently). Also, the ZeroBrane IDE looks pretty interesting, so I'll at least try the Windows version of that. Of course, each of these IDEs comes with its own Lua tutorial, so I may find I'm getting too deeply nested in tutorials.
22
23 Under: Installation on Linux
24
25 Linux users may prefer to use their native package manager to install Lua. I used
26
27 sudo apt-get install lua5.2
28
29 as that is the most recent version whose installation integrates into the user preferences system. Also note that on Linux, the interpreter does not abstract away the file suffix as generally occurs on Windows, so the command to test your lua installation may need to look like:
30
31 $ lua helloWorld.lua
32
33 if the test program is indeed created as instructed with the file name helloWorld.lua
34
35 Under: Installation on Windows
36
37lua_basic_syntax.htm
38
39When I do the interactive lua command, I don't get the extra instructions about "quit to end" etc. Also note that the version number reported may be different than in the example. It should match the version number installed in earlier steps; if it doesn't, we may need to ensure that the version of lua we are invoking matches the version of lua we think we installed by adjusting the PATH environment variable or user preferences.
40
41If you're going to introduce the shebang execution method, the tutorial should offer a better explanation. The first line is interpreted by the command execution system, perhaps the OS kernel itself, which uses the first string to locate the interpreter or executable file and passes any other strings and the name of the current file to that executable. The characters #! are sometimes known as shebang or hashbang -- starts with a # character which is a fairly common start-of-comment character in interpreted languanges.
42
43Also, the line given as
44
45 $./test.lua
46
47seems to be missing the space after the $ prompt, which is evident in other example lines but missing in this one.
48
49 Under: Tokens in Lua
50
51 This seems quite confused. There are a lot more than 3 tokens in the example code.
52
531 io
542 .
553 write
564 (
575 "Hello world, from "
586 ,
597 _VERSION
608 ,
619 "!\n"
6210 )
63
64assuming the tokenizer handles quoted strings as single tokens.
65
66 Under: Comments
67
68 Again somewhat confused. Comments are introduced by the double-hyphen. Multi-line comments can be created using the double-bracket method, but the double-bracketes are not needed if the comment does not extend beyond one line.
69
70lua_variables.htm
71
72I note that some example lines have semi-colons at the end and others don't. I hope there's an explanation soon about when a semi-colon is needed and perhaps an explanation of when it is not. I remember that Perl explicitly says that the semicolon is a statement separator as opposed to a statement termination. But until that explanation comes, the examples would be improved by leaving out the semi-colons that aren't needed.
73
74 Under: Variable Definition in Lua
75
76 The stuff about "variables with static storage duration are implicitly initialized with nil" seems out of place here. Readers following the tutorial will have no idea what "static storage duration" is supposed to mean.
77
78 Also: lower-case L is a poor choice to use as an example variable name because it looks too much like the numeral one in some fonts.
79
80[That's as far as I got in my first day's session. Ending at 15:30 on 2018-01-18.]
81
82lua_data_types.htm
83
84This line in the examples:
85
86 print(type(type(ABC))) --> string
87
88has skipped the step of showing
89
90 print(type("sample text")) --> string
91
92It's useful to know that the return value of the type() function is of type string, but it's opaque in the example. As an uninitialized variable, the value of ABC is nil.
93
94[Just a quick dip; Ending at 14:19 on 2018-01-19 - maybe more later...]
95
96lua_operators.htm
97
98No exclusive-or operator? Had to look up reasons people thought that was okay. Mostly the (dubious?) claim that it's not used very much. And the fact that they had used up the ^ character as a power operator - but could have used ! instead, right?
99
100I do like the # operator for taking the length, though.
101
102No shortcutting logical operators? Perhaps that keeps things simple.
103
104
105lua_loops.htm
106 (Apparently not fans of "next" or "continue" loop control statements.)
107
108lua_decision_making.htm
109
110Missed mention of elseif keyword in conditionals? The descriptions of if statements leaves a lot to be desired, especially in the description of nesting. No mention at all of keywords "then" and "end", either.
111
112lua_functions.htm
113
114 Under: Function with Variable Argument
115 I don't think the "for" loop and "ipairs" function have been properly introduced, so using them here is problematic.
116
117lua_strings.htm
118
119 Under: Finding and Reversing
120 Why is there so much space in front of the reversed string? It's a tab character. So the print function inserts a tab separator between the printout of each of its arguments.
121
122 Under: Formatting Strings
123 that %03d for the year needs some explanation. It works fine for 4-digit years. It will yield something funny looking for one- or two-digit years (leading zeroes) that might let the user catch that they didn't give it a 4-digit year. But why make it so odd? Nobody would bat an eye at %04d even though they think the "0" or even the "04" are somewhat redundant.
124
125lua_arrays.htm
126
127 Good note about sparse arrays.
128
129[End of third session at 00:30 on 2018-01-20.]
130
131[Been busy, resuming tutoral at 14:00 on 2018-01-23]
132
133lua_iterators.htm
134
135 Under: Stateful Iterators
136 It looks like closures should have been covered prior to describing their role in iterator functions.
137 This "elementIterator" function reproduces what some other languages call a "foreach" construct. Seems like it would be a pretty fundamental operation.
138
139lua_tables.htm
140
141 So, it looks like tables with only numerical indexes are somewhat special and rather akin to arrays or simple lists in other languages.
142
143 Under: Sorting Tables
144 This table.sort funtion has the property of changing the numeric index of the entries based on the entries' alphabetical sort position. I'm expecting there's some other thing that will appear, perhaps as an iterator, to provide the elements of a table in order of sorting the keys.
145
146 lua_modules.htm
147
148 Under: Specialty of Lua Modules
149 Is it worth mentioning that the Method 2 approach has a different effect on the namespace than either Method 1 or Method 2. Or does it? Does the require statement add its argument to the namespace regardless of the assignment to a local variable. Something to check out later. Also, is there a difference between require with and without parentheses?
150
151 And it seems strange that a list of function declarations have the effect of assigning the member functions to the module table. They don't look like ordinary assignments, it's just a side effect of declaring the functions in this way. I tried an example and found that I don't yet understand some fundamentals about tables, arrays, indexes, etc. Plunging ahead...
152
153lua_metatables.htm
154
155 I sort of get what the metatable is doing here but the examples as well as the explanations are pretty terrible. There's no motivation for why this construct exists. The various double-underscore identifiers are associated with Lua operators by default, but defining them in a metatable allows redefinition of what Lua does for that operator. In other OO languages, this may be referred to as "operator overloading". I'm waiting to learn if the metatable can call methods of the table it is metatable for. Ah, I see it: the metatable contains functions which are closures, the name of its parent table is packed into the closure. At least, that's how it appears to work for this example. But now you need rawset and rawget functions to avoid calling metatable functions. I don't see an operator overlay for ~=, so perhaps that is just treated as a boolean negation of the __eq operator.
156
157lua_coroutines.htm
158
159 If coroutines can't run concurrently as threads, that reduces their value a lot! And I can't consider accessing global variables from coroutines as a "feature".
160
161lua_file_io.htm
162
163 Seems like a fairly straightforward implementation of the standard C library io functions.
164
165lua_error_handling.htm
166
167 I see the analogy between pcall and the "try" and "catch" paradigm.
168
169 The statement "This function error never returns." is trying to explain a complex transfer of control that happens when it is called. I seem to remember discussions about "bad goto" relating to how to structure exception handling. So this is how Lua handles it, and I think that can be lived with.
170
171[End - finished with main part of tutorial - at 17:00 on 2018-01-23]
172[Resume with more advanced topics - at 12:30 on 2018-01-24]
173
174lua_debugging.htm
175
176 It looks like setupvalue() allows you to modify the value of local values in the scope of your callers (but not ordinarily accessible in your current scope). It will be interesting to try some of these debuggers once I get some more Lua code written. It will also be interesting to see how much can be used in the NodeMCU Lua environment.
177
178lua_garbage_collection.htm
179
180 Other than perl, I've always been rather leery of run-time garbage collection. Having a function that returns how much memory you are using seems like it would generally be pretty useful.
181
182lua_object_oriented.htm
183
184 Hadn't seen this colon operator before. Does that mean we can't use the dot operator to create extended calls?
185
186lua_web_programming.htm
187
188 Okay, now I've learned that luarocks is one accessor to community-developed Lua software. But I've mostly glossed over this section, since I don't immediately foresee how I would use it.
189
190lua_database_access.htm
191
192 So, note to self: semicolons are not needed as statement terminators when passing string to these execute functions.
193
194lua_game_programing.htm
195
196 Methinks Raj, the tutorial writer, is growing weary, so the sections of this tutorial page are simply regurgitations of the feature lists for each of the game programming frameworks. Since I'm not currently interested in developing games, I grow weary of this, too.
197
198lua_standard_libraries.htm
199
200 It looks like the descriptions of functions "pairs" and "print" are incorrect, copied perhaps from the "coroutine.yield" function? I'll have to look elsewhere for the "pairs" function and I'm a bit unclear on how to use "next".
201
202 Looks like the tutorial writer punted again for descriptions of other members of the standard library.
203
204lua_math_library.htm
205
206 Looks pretty standard. Nice of them to provide a value for pi.
207
208lua_operating_system_facilities.htm
209
210 Okay. These look fairly standard as well.
211
212 What I think I need to look for is how the main chunk of a Lua program receives any arguments passed on the command line.
213
214lua_quick_guide.htm
215
216 Alright, that seems to just be all the pages of the tutorial concatenated. Useful as a document to search across the whole tutorial, I suppose.
217
218lua_useful_resources.htm
219
220 Okay. I don't think I'm in the market for Lua books today. Thanks.