Actually

It's Randolph Connor Berry

Thoughts on Lua

I’ve been messing with Lua for the past week or so. I’ve been fairly successfully using it to go through the challenges on Code Eval (sea6ear on the leaderboard). So far I really like Lua. I’m kind of a sucker for simplicity and performance, and the abstraction abilities of Lua are pretty good for a language that isn’t Scheme, Common Lisp or Haskell.

However, I’ve used it enough recently that I’ve started to see what are probably the most obvious annoyances. [NOTE: I've only been using it steadily for a week, I'm still a newbie. These are mostly first impressions.] Also these are more gotchas than showstoppers, I think once you hit these and are aware of them, you can avoid them.

The most obvious annoyance once you start writing code is the need to explicitly declare local variables. It’s easy to forget and end up with global variables when you didn’t intend for them to be. This is pretty much the same way that JavaScript or Perl handle things, so this isn’t unique to Lua.

A more important annoyance is the interaction between two features that don’t seem dangerous in themselves. Lua automatically coerces values between numbers and strings. Usually that’s something I find convenient. However, Lua’s tables also switch between acting like an array and acting like an associative array (dictionary/map/hash etc) based on whether integer indexing or string based (or other value) indexing is used.

What this means is that you have to be very careful when dealing with data that could be either a string or number and may be used as an index of a table. If you accidentally use it in the wrong format, or accidentally switch between trying to index with an integer vs string format, you will put or try to access data in the wrong part of the table. This can make it look like your data has disappeared when really it’s just been shuffled off to another part of the table that you weren’t trying to use right now.

On a different topic, and really just a minor annoyance, arrays are 1-based instead of 0-based. One based array indexing wouldn’t really be an annoyance except that the rest of the computer world has standardized on zero based indexing (Except for Awk, a language with some similar goals to Lua). It’s not too bad in practice, except when you need to be translating between a zero-based scheme and the one-based scheme.

Overall I really like Lua. I’m really impressed with it. It seems to have almost everything that I want to have in a language as far as semantics. I like that it has tail-call optimization so well constructed recursive functions don’t have to blow the stack. I like that functions can be passed as values. I like (overall) that tables are the central data structure and that you can use them to build all the other kinds of data structures that you desire. I think I will enjoy it’s lightweight approach to object orientation.

I wish that it was more evidently useful as a general class scripting language instead of being primarily thought of as a language for embedded scripting. On the other hand, I don’t want to change it so that it looses that focus. What I’d like to see is a real secondary distribution that had all the libraries to allow Lua to function in cross-platform general purpose fashion. (Luarocks might be that, but I’m not sure how comprehensive it is or how widely it’s used. LuaDist seems like a similar attempt) So basically, I’d like the core distribution to stay small and focused, but there to be some kind of additional library or platform that would allow one to get all the libraries on needed for doing stand-alone work with Lua.

I’m not sure how much work that would be. I’d like to see that happen. I want that enough that I’m willing to work to try to make that happen.

On the other hand, it looks like other people have occasionally tried similar things, and it doesn’t look like any of the other attempts that I saw (outside of Luarocks, and maybe LuaDist?) have gotten persistent traction. Part of that may be that each other attempt at bundling additional functionality has been focused on some fairly narrow niche. I don’t know how much work it would be to create a curated library of various modules that would give you the basics you would need to do real work on various platforms. I’d like it to be something that you would download in addition to the standard Lua download rather than instead of.

I’m not sure what kinds of additional functionality (outside the Lua core) would be necessary for Lua to be able to easily handle the same kinds of tasks that one would do in Python, Ruby or Perl.

Here’s what I can think of:

  • GUI bindings
  • File system manipulation
  • HTTP request libraries
  • Web server capabilities
  • Process interaction and control

Many of these exist in some form currently, but they are scattered and I’m unsure of the level of documentation or consistency of maintenance.

I wonder how many people focused on making it happen it would take to make Lua into a viable language for general purpose computing. I think it could probably be done with less than 20.

I think if I was involved in starting a company I would be tempted to use Lua for the scripting level (along with C for low-level, and Erlang for very high-level tasks) in hopes that the company could contribute code to making Lua a viable language for general use outside the game industry and niche areas. Even though Lua wouldn’t be as useful as Python or Ruby starting out, I think it would be worthwhile to focus on it and make libraries that did enable it to be used in a wide range of areas.

Lua could also use some more beginner oriented books. Programming in Lua is awesome, but it’s a pretty advanced text in terms of the language concepts it deals with. I would be interested in writing one of those beginner books once I am more familiar with it.

It’s been almost a year since my last post. I’ve been journaling mostly offline for a while. I’ve been trying to write 1000 words a day each day. A lot of the time I achieve that, some days I don’t. But I haven’t been good at going back through my writing to pull out things and put them up here on the blog. I’d like to start doing a better job of that.

Testing RSS

Testing RSS feed from connorberry.com

Language Choices

I think that if things were up to me I’d have one list of languages that I concentrate on for continuous study and application development, and a list that I bounce around in for fun and study and making toys and stuff.

The core languages I’d concentrate on for continous study and industrial application development:

* C++
* Perl
* Common Lisp
* OCaml

The languages for prototypes, utilities, fun, exploration and experimentation:

* C
* Lua
* JavaScript
* Scheme
* Tcl
* Smalltalk
* Forth
* Prolog
* Erlang
* SML
* Awk
* Assembly

I think the core languages are strong enough that any of them could be used for almost any task,
but there’s enough flexibility in the list to pick a good match for any particular use.

I’d use the larger list for single purpose apps and experimentation. Each of them is small enough
that I think I could get into a partincular language for a month or so and either learn something useful
or develop something small but real using it.

Other Things to Learn

To be an effective programmer, here are a few other things it really would help to have a grounding in as far as general use / concepts.

  • HTML / CSS
  • SQL
  • Assembly Language (I think any one would give you the concepts that are useful)
  • Command Line (probably preferably Unix based)

An argument can be made that these are / aren’t languages in the same sense of programming language concepts covered by the earlier list of languages. However, ones life as a programmer will be subtly constrained to the extent that one is not familiar with these technology areas.

 

Languages for Learning

Here is a list of languages that seem to me useful to learn in the sense that between them they cover all the major concepts that exist in other languages, yet they are all individually small enough that you can easily learn the particular language and keep it in your head.

I think that one should be able to become somewhat proficient in any of these languages with about a month of reasonably serious study. Experimentally, I seem to be able to keep about 3 of these going as far as casual study at the same time.

I believe that most interesting language concepts are expressed in some way by at least two languages in this list. So each major concept should have at least two potential implementation to compare.

  • C
  • Scheme
  • Forth
  • Prolog
  • Erlang
  • SML
  • Lua
  • Tcl
  • Ruby
  • Awk
  • JavaScript
  • Go

I think that from this base one could pick up almost any industry language with a decent understanding of the concepts. So given about a year of exploratory study of these languages (perhaps 1 per month?), one should be well equipped to handle learning any of the more complex languages currently used.

The surprising use case for Vim

I’ve spent a lot of time trying out different text editors. Among the reasons that I continue to come back to Vim, there is one that is surprisingly powerful, and somewhat unexpected.

The reason: I can print from Vim on every computer I’ve installed it on.

Most other cross-platform editors fail as far as printing documents on some  OS or another (often Windows). Vim is the only one that I’ve seen reliably print everywhere I’ve tried it. As a bonus, it can print just as well from the terminal as from the gui.

Follow

Get every new post delivered to your Inbox.