• 0 Posts
  • 33 Comments
Joined 1 year ago
cake
Cake day: June 11th, 2023

help-circle
  • 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 like FooBar 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.




  • 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!



  • 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?








  • 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.



  • tunetardis@lemmy.catoProgrammer Humor@lemmy.mlTrue Story
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    3 months ago

    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.





  • 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 -