I guess the MAC address guy is up next. 48 bits may not go so far if every light bulb is going to want its own.
I guess the MAC address guy is up next. 48 bits may not go so far if every light bulb is going to want its own.
Imagine if you were the guy who made the call on IPv4 addresses…
Falsehoods About Time
Having a background in astronomy, I knew going into programming that time would be an absolute bitch.
Most recently, I thought I could code a script that could project when Easter would land every year to mark it on office timesheets. After spending an embarrassing amount of…er…time on it, I gave up and downloaded a table of pre-calculated dates. I suppose at some point, assuming the code survives that long, it will have a Y2K-style moment, but I didn’t trust my own algorithm over the table. I do think it is healthy, if not essential, to not trust your own code.
Falsehoods About Text
I’d like to add “Splitting at code-point boundary is safe” to your list. Man, was I ever naive!
Fair, though I guess my interpretation was that void*
is kind of like a black hole in that anything can fall into it in an unsettling way that loses information about what it was?
Actually, now that I think of it, there’s no reason you need to join the 2 names into a single str
. You could just leave it as a tuple
of last, first and Python will know what to do in comparing them.
>>> sorted(student_ids, key = lambda i: ((rec := student_recs[i])['last'], rec['first']))
[632453, 1261456, 532153]
So the lambda would be returning ('Potter', 'Harry')
rather than 'Potter, Harry'
. But whatever. The :=
part is still the same.
Can you use it to initialize vars outside the scope of the lambda?
No, that’s not what it’s for. It lets you define a temporary local variable within an expression. This is useful in situations where you might want to use the same value more than once within the expression. In a regular function, you would just define a variable first and then use it as many times as you want. But until the walrus operator came along, you couldn’t define a variable within a lambda expression.
Can you give an example?
Ok, I’m trying to think of a simple example. Let’s say you had a database that maps student IDs to records contain their names. To keep things simple, I’ll just make it plain old dict
. And then you have a list
of student IDs. You want to sort these IDs using the student names in the form “last, first” as the key. So you could go:
>>> student_recs = {1261456: {"first": "Harry", "last": "Potter"}, 532153: {"first": "Ron", "last": "Weasley"}, 632453: {"first": "Hermione", "last": "Granger"}}
>>> student_ids = [1261456, 532153, 632453]
>>> sorted(student_ids, key = lambda i: (rec := student_recs[i])['last'] + ', ' + rec['first'])
[632453, 1261456, 532153]
The problem here is that student_ids
doesn’t contain the student names. You need use the ID to look up the record that contains those. So let’s say the first ID i
is 1261456
. That would mean:
rec := student_recs[i]
evaluates to:
{"first": "Harry", "last": "Potter"}
Then we are effectively going:
rec['last'] + ', ' + rec['first']
which should give us:
'Potter, Harry'
Without the :=
you would either have to perform 2 student_recs[i]
look-ups to get each name which would be wasteful or replace the lambda with a regular function where you can write rec = student_recs[i]
on its own line and then use it.
Am I making any sense?
They’re somewhat more capable now that we have the walrus (:=
) operator.
One thing most text editors can do is print. I was shocked the other day when I couldn’t print a readme from vscode when someone asked for hard copy.
This is why I fear activating any AI features in the IDE.
Compiler/interpreter: Can’t find variable farfignewton
.
Earlier:
Me: Declare variables near, far
IDE: Oh! You mean farfignewton
right? I found that in some completely unrelated library you didn’t write. Allow me complete that for you while you’re not paying attention.
This was a struggle for me going from hobbyist programmer to working at a company. I tried to tone it down. Really. But eventually I got “promoted” to having my own office with a suspiciously thick door. Hmm…
True story. I was looking for an answer to an obscure problem and found it in a 10-year-old stackoverflow post. Then I looked more closely at the author…
Hey! Me from 10 years ago, stop being such a smart ass! It’s obnoxious.
I have some vague recollection of a hacker convention from the 90s where people were challenged to come up with wireless networking in a one night coding marathon. (This was long before wifi.) So some dude used speech synthesis to get a machine to say “one zero one one zero…” and another to assemble the binary data into packets using speech recognition. It was hilarious, and the dev had to keep telling people to shut up and stop laughing so he could complete the demo.
But anyways… what I’m trying to suggest here is you might have the best luck if your notification sounds contain spoken commands and you use speech recognition to trigger scripts? That tech is pretty mature at this point.
I was astonished to find the other day that LibreOffice has no problem opening ClarisWorks files. That is an ancient Mac format that even Apple’s Pages has long since abandoned.
There is an issue with templated code where the implementation does have to be in the header as well, though that is not the case here. C++20 introduced modules which I guess were meant to sort out this mess, but it has been a rocky road getting them to be supported by compilers.
Looks like we’ve got a Java programmer here taking C++ for a spin.
Oh yeah, that’s another way to make a subshell. But don’t forget to stick the find
in there also:
sudo sh -c 'cd ./testar && find . -maxdepth 1 -type d,f' | ...
The commands within the parentheses run in a temporarily created subshell with its own environment. So you can change the working directory within it and it won’t effect your main shell’s working directory.
Let’s say you’re in the home directory that’s called /home/joe
. You could go something like:
> (cd bin && pwd) && pwd
/home/joe/bin
/home/joe
If find
had something equivalent to tar -C
, you wouldn’t need to do this, but I don’t think it does?
Ok, I actually tried something like this at a terminal. You do still need the -C ./testar
if you use the subshell since tar
won’t know where to look otherwise.
(sudo cd ./testar && sudo find . -maxdepth 1 -type d,f) | sudo tar -czvf ./xtractar/tar2/testbackup2.tgz -C ./testar -T -
This will still give you a listing with ./text.tz
and so on because find
prints ./whatever
when you search .
. I think this is harmless? But I suppose you could remove them if it bothers you.
(sudo cd ./testar && sudo find . -maxdepth 1 -type d,f) | cut -c3- | sudo tar -czvf ./xtractar/tar2/testbackup2.tgz -C ./testar -T -
I started in C and switch to C++. It’s easy to think that the latter sort of picked up where the former left off, and that since the advent of C++11, it’s unfathomably further ahead. But C continues to develop and occasionally gets some new feature of its own. One example I can think of is the
restrict
key word that allows for certain optimizations. Afaik it’s not included in the C++ standard to date, though most compilers support it some non-standard way because of its usefulness. (With Rust, the language design itself obviates the need for such a key word, which is pretty cool.)Another feature added to C was the ability to initialize a
struct
with something likeFooBar fb = {.foo=1, .bar=2};
. I’ve seen modern C code that gives you something close to key word args like in Python using structs. As of C++20, they sort of added this but with the restriction that the named fields have to come in the same order as they were originally defined in the struct, which is a bit annoying.Over all though, C++ is way ahead of C in almost every respect.
If you want to see something really trippy, though, have a look at all the crazy stuff that’s happened to FORTRAN. Yes, it’s still around and had a major revision in 2018.