BF

When you know deep-down that a Brainfuck interpreter should only ever be written in C, but you’re not quite ready to leave the comfort of C#. So in a spirit of minimalism you find yourself writing C# in a C style just for the hell of it.

public static void Execute(string program, Stream? input, Stream? output)
{
    int ip = 0;
    int dp = 0;
    var data = new byte[30000];

    while(true)
    {
        if (ip >= program.Length)
            break;
        switch(program[ip])
        {
            case '>':
                dp++;
                break;
            case '<':
                dp--;
                break;
            case '+':
                data[dp]++;
                break;
            case '-':
                data[dp]--;
                break;
            case '.':
                output?.Write(data, dp, 1);
                break;
            case ',':
                input?.Read(data, dp, 1);
                break;
            case '[':
                if (data[dp] == 0)
                    ip = JumpTo(']', +1, program, ip);
                    break;
            case ']':
                if (data[dp] != 0)
                    ip = JumpTo('[', -1, program, ip);
                break;
        }
        ip++;
    }
}


private static int JumpTo(char match, int incr, string program, int ip)
{
    while (true)
    {
        ip += incr;
        if ( (match == ']' && program[ip] == '[') || 
             (match == '[' && program[ip] == ']') )
            ip = JumpTo(match, incr, ip, program);
        else if (program[ip] == match)
            return ip;
    }
}

This made me wonder if the style of a language affects the style of its implementation. Not sure.

(For any impressionable children reading this: don’t do it this way. Use descriptive variable names and comments and put braces around your if/else clauses.)

(And for any hiring managers reading this: its deliberate, I promise.)

The Strongman Fantasy

Timothy Snyder on The Strongman Fantasy — life in the near-future Trump-style authoritarian dictatorship. The way that it starts:

We dream that a strongman will let us focus on America. But dictatorship opens our country to the worst the world has to offer. An American strongman will measure himself by the wealth and power of other dictators. He will befriend them and compete with them. From them he will learn new ways to oppress and to exploit his own people.

And the way that it ends:

Once this process begins, it is hard to stop.  At the present stage of the strongman fantasy, people imagine an exciting experiment.  If they don’t like strongman rule, they think, they can just elect someone else the next time.  This misses the point.  If you help a strongman come to power, you are eliminating democracy.  You burn that bridge behind you.  The strongman fantasy dissolves, and real dictatorship remains.

Most likely you won’t be killed or be required to kill. But amid the dreariness of life under dictatorship is dark responsibility for others’ death. By the time the killing starts, you will know that it is not about unity, or the nation, or getting things done. The best Americans, betrayed by you when you cast your vote, will be murdered at the whim and for the wealth of a dictator. Your tragedy will be living long enough to understand this.

Link

SLotD: Cheap Thrills

My new hobby is reading the C# using statements that Visual Studio inserts as it tries to figure-out the dependencies for my fumbling typing.

using System.Security.Cryptography.X509Certificates;
using static Android.Media.Midi.MidiDeviceInfo;
using Android.Media;
using Kotlin.Jdk7;
using Android.Drm;
using System.Security.Cryptography;
using Android.AdServices.CustomAudiences

public class MyClass
{

It passes the time…

TakeoutExtractor v1.1

At the start of the year, as part of my general effort to minimise my exposure to Google, I ordered-up a Google Takeout and cleared-out and archived my photos and videos from Google Photos.

To this end, I’ve just released version 1.1 of Takeout Extractor over on github. This releases adds fixes for a few issues discovered during the year, including some kindly reported by other github users. It now correctly handles photos edited using the Google One add-on photo features, to which I subscribed some time back in the last year. I also updated the projects to .net 7 and did some general freshening up. As noted in the readme, this is a source-only release because publishing maui apps currently seems to be broken in Visual Studio 2022. I’ll see if this is resolved by future VS updates. For Windows and MacOS apps (sadly sans the latest fixes) see the v1.0 release

See here for some background on the project. I hope it is useful to someone.

Year End

“This is what you shall do: Love the earth and sun and the animals, despise riches, give alms to every one that asks, stand up for the stupid and crazy, devote your income and labor to others, hate tyrants, argue not concerning God, have patience and indulgence toward the people, take off your hat to nothing known or unknown or to any man or number of men, go freely with powerful uneducated persons and with the young and with the mothers of families, read these leaves in the open air every season of every year of your life, re-examine all you have been told at school or church or in any book, dismiss whatever insults your own soul, and your very flesh shall be a great poem and have the richest fluency not only in its words but in the silent lines of its lips and face and between the lashes of your eyes and in every motion and joint of your body.”

Walt Whitman, preface to Leaves of Grass, via Maria Popova

Happy new year. Maybe, just maybe, although I can’t see how, 2024 might just be better less bad than the one ending. We’re all going to have to try pretty hard though.

Time to burn the dying year down and start again.

SLotD: Pushing the model

Software Lesson of the Day for 16/10/2024:

Sometimes you will try something and it works. And then you extend it a bit. And then a bit more. And it stops working and/or doing what you want.

Consider the possibility that the fact it initially worked was just a false signal, and that actually you should never have done it that way at all. Because the model or pattern for doing $thing$ is actually somewhat different — and whether you approve of that pattern or not, and whether you like deleting superficially working code or not, that’s the way you have to do it. Because we rarely write software nowadays without what we write having to fit into a more or less opinionated container. So make sure you actually understand the pattern, not simply what you want it to be.

(This post is brought to you by trying to implement an Android ViewPager with hard-coded child elements in the layout xml. No matter how hard you try, you can never defeat the PagerAdapter‘s beliefs about page instantiation and caching.)