2016-11-30

mindstalk: (Default)
I was catching up on C++11 last night. Learned about lambdas, and started playing around with them. A neat addition. But did they provide full closure capability, like creating a snapshot of state that the function can privately modify? Looked like no, and I was all set to be disappointed.

But at a job interview yesterday, I learned about the mutable keyword, which was described as letting you modify const things, like deep in a const object hierarchy. "Why would you even do that?" was my first reaction, though I can vaguely imagine why, "for the same reason that Haskell's purity annoyed me."

So I remembered that, and figured I would try adding mutable to the lambda. Ka-ching!


#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    vector  v;
    for (int i=1; i<=10; i++) {
        v.push_back(i);
    }
    int sum=0;
    for_each(begin(v), end(v), [sum](int n) mutable {sum+=n; cout << sum << endl;});
    cout << sum << endl;

}


Output:
1
3
6
10
15
21
28
36
45
55
0

Not that you need the external sum here, you could drop it and have [sum=0] for the lambda, but it illustrates the idea. Which isn't obscure, I saw it in the docs I was reading shortly after returning to them, but still, I found this on my own.

I've been applying to a bunch of C++ jobs more because of experience than because of any deep love for the language, but features like this and auto (type inference) and others from C++11/14 make it a lot more appealing.
mindstalk: (Mami)
So we have these things called artificial neural networks, that learn in a supposedly neurologically-inspired manner. But AIUI, they typically take many repetitions to really learn something. Many many. And sometimes human learning seems like that, like rote memorization.

But other times, we learn with single-instance burn in. And not always because of some great emotional association, or repeated reflection. I had two instances of that yesterday.

First, I was up in Lowell for a job interview, and took the train. Now, I did that a few years ago, when a bunch of us went together to museums there. So as I emerged from the station this time, and looked across the street, suddenly I remembered exploring the park last time, before I'd gone to meet the others (who biked up). It's not like it's a particularly exciting or distinctive park, and I doubt I've thought of it since... but the impressions were there to be recalled.

As were the memories of being daunted and confued by crossing the nigh-freeway streets to get downtown, but that actually was mildly traumatic.

Second, I've been reading someone's Where I Read thread of the Robotech novels, based on the Robotech animated series, much of which I saw as a child. Last night the reader described a late scene where Minmei is seated outside somewhere, and her douchebag cousin-lover-manager Kyle is chugging a liquor bottle, before he finishes it and smashes it in mid air with a dropkick.

And I remember all that! Not well enough to guess who was on which side of the screen, but all of that suddenly seemed vividly familiar, in a way that other described scenes recently haven't. And unlike other remembered scenes -- the firing of the main gun, Roy dying, Max and Miriya fighting/courting, the SDF-1 punching a Zentraedi ship in Operation Daedalus -- I don't think I've thought about or reflected on this scene in the intervening nigh thirty years; it doesn't seem that iconic (though true, raging alcoholics are rare on Saturday morning cartoons, along with many other things distinctive about Robotech.) It's just some scene... that suddenly feels very fresh, after all this time.

I can't prove it's not some false memory constructed in response to the text. But I see no reason it has to be.

Profile

mindstalk: (Default)
mindstalk

June 2025

S M T W T F S
123 45 67
89 10 1112 1314
15161718192021
22232425262728
2930     

Most Popular Tags

Expand Cut Tags

No cut tags

Style Credit

Page generated 2025-06-15 10:57
Powered by Dreamwidth Studios