🔗 Lua’s and-or as a ternary operator
Lua is a nice programming language, which allows for readable and concise code. One of my favorite idioms in the language is this:
x = a and b or c
which usually works like the C ternary operator, “a ? b : c”, but in a more readable fashion. This works in Lua because the “and” and “or” operators return their matching value when they get a “true” value (anything other than false and nil).
One thing to keep in mind though, is that this is really “and and b or c” and not “a then b else c”. There’s one big difference: when you want to have nil or false in your “then” value.
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > = true and 10 or 20 10 > = false and 10 or 20 20 > = true and nil or 20 20
An ugly alternative is to do “not a and c or b”, like this:
> = not true and 20 or 10 10 > = not false and 20 or 10 20 > = not true and 20 or nil nil
But at this point it’s better just to use an “if” statement. (This hack works around the then-as-nil case, but not else-as-nil, of course.)
So, keep in mind that you should use the “a and b or c” construct as if-then-else only if you know that your “b” value is not nil or false.
It’s better, however, to use this construct for what it is. This behavior is not flawed, it is just different, and it has its own uses. For example, it is very useful to check if a table exists:
x = tbl and tbl.key
And to define fallback values:
x = tbl and tbl.key or "missing!"
Note that the above line reads naturally, and you wouldn’t be able to do that with one “?:” operator in C.
🔗 Cool talk: StackOverflow.com and building an online community
Following @gbedin’s suggestion, I will try to post here links to cool talks I watch and often tell him about, so the links can remain on a more “permanent” place than Twitter.
In this talk, Joel Spolsky describes some practical aspects related to community-building that went into the design of StackOverflow.com.
🔗 htop 0.9
Today I announced version 0.9 of htop, after a week of release candidates posted to the htop-general mailing list.
I had a number of changes committed to SVN for a few months but I wanted to take some time to clean up some rough edges and make sure everything was okay for a release. So I decided to take some time last Saturday to work on htop again and run through the bug tracker to check any pending issues, and I ended up spending the whole day from 2PM to 1AM working on it. It was a long time since I last coded non-stop for that long, and it was really fun.
(By the way, why doesn’t sf.net send me an email whenever someone posts a bug? If this feature is available, I never found it — in fact, their interface keeps changing; I admit it took me a while today to find how to release a file. Granted, the new procedure is much much simpler than the old one.)
The tool is pretty stable now and I don’t want to spend a lot of time on it, so it’s natural that the time between releases is growing. For this reason, it’s especially important to make good releases. The series of release candidates was great to catch silly bugs (inserted during Saturday’s coding rush, of course) — for that I’m thankful to the folks from htop-general.
This release brings a feature I wished for a long time: the ability to collapse and expand subtrees. I don’t know if I’ll actually use it that much, but for some reason I always missed it. Another improvement is that it now displays cmdlines of arbitrary length (actually, up to 4096 bytes, which is the kernel’s limit). No more truncated argument lists.
I think the only annoyances left in htop now, as long as I’m concerned, are the lack of search and save in strace view and the lack of history in process search. I think when I get all these done I’ll call it 1.0. Don’t hold your breath, though: it took me one and a half year to go from 0.8.3 to 0.9… :-)
On the subject of stability, I did catch one ridiculous off-by-one error in an array access which may be the cause for the random segfaults that were reported by a few users over the years. Amazing that it sat there for so long, but well, so is life coding in C.
🔗 LPEG Cheat Sheet
I once felt that every time I think LPEG is the solution to my problem I spend so much time trying to learn it that I lose focus from the actual problem.
The concept is admirable, though, so for once I decided to struggle through and actually get the job done using it. I learned a few tricks along the way, so I decided I should write them down (because I know I’ll certainly need them in the future).
These assume the one-letter functions are loaded into locals (e.g. P = lpeg.P
).
Match a pattern exactly n times
local function X(p,n) return n == 1 and p or p * X(p,n-1) end
Match anything not in a set
(1 - S"abc") -- [^abc]
Match a string converting backslash escape sequences
-- match backslash and capture following char, -- then convert match to captured char local escape = (P"\" * C(1)) / "%1" -- match anything but backslashes and spaces, or escapes -- turn everything into a capture, replacing inner captures local pathname = Cs( ( (1 - S"\ ") + escape ) ^ 1 )
Capture a number as a number
local num = (R"09" ^ 1) / tonumber
Follow
🐘 Mastodon ▪ RSS (English), RSS (português), RSS (todos / all)
Last 10 entries
- Frustrating Software
- What every programmer should know about what every programmer should know
- A degradação da web em tempos de IA não é acidental
- There are two very different things called "package managers"
- Last day at Kong
- A Special Hand
- How to change the nmtui background color
- Receita de Best Pancakes
- That time I almost added Tetris to htop
- Receita de Orange Chicken