• 1 Post
  • 88 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!



  • I’m with you on this one. There are lyrics on almost every single track for crying out loud. Throw us instrumental lovers a bone won’t you? Songs that are lyrically driven but are otherwise super-repetitive instrumentally tend to put me to sleep.

    What I love about concerts is when the band goes off script and just starts jamming. Even a 5-minute drum solo will have me grinning ear to ear, and that’s what I’ll be remembering on the way home.


  • The city where I live has a musical instrument lending library. I don’t know how common these are? Ours started when a cherished local musician passed away and his eclectic collection became the library. Over the years, more people have donated instruments and there is an annual festival to raise funds for their upkeep. (As a local musician, I’m actually playing at said festival today.)

    Anyway, it works just like a regular library. You get your library card and check out an instrument and it doesn’t cost you a penny. And there are all kinds of videos online these days to give you pointers on how to play. I guess if you get really serious, you’ll probably want some one-on-one tutoring, but if you’re just doing it for kicks and don’t have any plans to join a band or whatever, you can just have some fun and see how far you can get on your own?


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






  • Wow, that is fascinating!

    Makes me wonder about the other direction, going into the near infrared as opposed to UV. I remember from a class in remote sensing that many plants are actually most reflective in that band (more so than in green, even). NIR air photos are often used by biologists to get an indication of the health of a forest. But I have no idea whether animals also reflect NIR? It may be that most animals cannot see in that band in the first place, so it would not offer any camouflage advantage.


  • Great read! That explains a lot.

    I’ve been deep diving a bit myself and found this article that explains another thing that’s puzzled me over the years. Some birds have crazy vibrant coloration that almost glistens, like peacock feathers. Outside of the zoo, I’ve noticed it a bit in common grackles. They look black on first glance, but when you study them closely, they have this kind of purple sheen around their heads. Apparently, it’s still melanin at work here, but it’s structured in a very special way.