Saturday 19 November 2022

Off-topic / no technical content: I have a dilemma

G'day:

This is off-topic and has no technical content to it at all. It is about a personal situation I have. This is the only place I have to post large wodges of text publicly, hence putting it here. And hey it's my blog: I'll post what I want ;-)

I have a dilemma.

Context / history

My neighbour and I share a front door to the street, and we had got onto nodding terms with each other. When her new baby arrived I fussed a bit over the new wee girl and got to talking more. My neighbour has three girls, approximately 15, 8, <1. They live in a flat that is at most 2 bedrooms. It will be like mine, which is to say it will be very very basic.

She is on a key meter for her electricty, and these display the balance on the meter, which is inside the front door, so I pass it daily. Often (once a month? Every coupla months?) it's in the red, and their power is off.

We each pay for one of the lights in the shared area (stairs, hallway to the front door), and I have been making a point of giving her £20 every few months to cover her share of it.

She has had bailiffs around chasing her ex, who owes the council money for parking fines.

She has had the police around to escort her current partner away from the flat, as he was being abusive. He is, btw, back.

Someone from a charity brought a bag of food around "for the lady who lives upstairs with the kids. She looks like she could do with some food".

I think it's reasonable to say she's struggling with life.

Recently she came to me in a flap saying she hd a debt she needed to pay now, and she couldn't and could she borrow £20? I offered £50, and she did not hesitate to take it. I told her she did not have to pay me back as I could afford it, and she needed it more than me. This is not terribly altruistic of me: I give £50 to charity each month, and that month I gave it to her instead. [shrug].

A few weeks later she texted me saying she needed another £20 in her account as she needed to pay a bill urgently. And she'd pay me back. I transferred £50, and reassured her she didn't need to pay me back, but that we can't keep operating like this.

She's knocked on the door to give me £20 back - I reiterated she didn't need to but she insisted - but within the day she meekly came back and asked if she could take it back again. Because reasons. Fine.

This is all fine. Thusfar.

However now she is texting me every week almost for more money, and I have drawn a line under this enterprise saying "look, 'borrowing' money from your neighbour is not a workable way of solving your issues. I don't know what is, but this has to stop. Please don't ask me for more money".

The calls/texts continue. I am ignoring them. She is still friendly when I meet her on the landing or on the street.

She has actually paid back the £120 she ended up "owing" me. I asked he to keep it: she refused.

Here is my dilemma

She's broke. The UK is not a good place to be broke. Social services are struggling, food banks are struggling. Really a lot of people are struggling. I don't want anyone reading this as anything as a fact that is contextually significant: she is from an eastern European country, her English is not great and I suspect her education is not great either. She does not appear to have any family over here.

On the other hand I am not struggling. I can afford to just give her the money (and I can also afford to also keep giving £££ to charity each month too if I wanted to). I'm not a fountain of cash, but £50 here or there doesn't matter.

Theoretically I stand by my position of "asking yer neighbour for money is not a sustainable solution to your problems". But she has kids who need to eat & have a roof & be warm. Should theoretical economics trump hungry kids?

I have found myself thinking "this is not my problem" or "this should not *be* my problem". But it's my neighbour. Someone in my community. Who needs help and has asked for help.

Whether or not the govt or local council should be dealing with this is neither here nor there. We have a shit, evil (actually evil) government, and we know they don't give a shit about poor people. I can't change the govt (other than one vote at a time), but I can change this situation maybe.

I feel I am being more "middle-class" than I am being a caring person. But all the points above nag at me. I'm currently holding the position of "no, no more money; I am not the solution to your problems". But I suspect I might be mistaken, or being overly "priveleged" about this because I can afford to prevaricate over principles vs hunger.

Thoughts? And if you don't wanna put them publicly here, my email address is on the blog's Communications policy page. Or a lot of my readers know other channels to ping me on and discuss.

Righto.

--
Adam

Thursday 3 November 2022

Kotlin: more operator overloading

G'day:

The Kotlin koans are still focusing on operator overloading, so so am I. Previously (Kotlin: overriding operators), I had a mess around with overloading:

This evening I'm gonna look at a coupla others.

Plus operator

Like… just the + operator. As in a + b.

This is a daft example, but I'm gonna define a buildable Critter class. A Critter can have a number of arms, legs and heads. Why? Oh I don't know. It just seemed funny to me at the time.

To do this I need to overload the plus operator multiple times:

class Critter(){
    private var heads = mutableListOf<Head>()
    private var arms = mutableListOf<Arm>()
    private var legs = mutableListOf<Leg>()

    operator fun plus(head: Head) : Critter {
        heads.add(head)
        return this
    }
    operator fun plus(arm: Arm) : Critter {
        arms.add(arm)
        return this
    }
    operator fun plus(leg: Leg) : Critter  {
        legs.add(leg)
        return this
    }
}

I'm overloading the plus operator three times. One each for Critter + Head, Critter + Arm, Critter + Leg. Note also that I'm specifically returning the Critter itself from these operations, so that further operations can be chained.

For the purposes of this exercise, the Arm, Leg, Head classes are a bit light on implementation:

class Arm
class Leg
class Head

For the test, I've also added a helper method:

fun describe()= """
        Critter with ${heads.size} ${English.plural("head", heads.size)},
        and ${arms.size} ${English.plural("arm", arms.size)},
        and ${legs.size} ${English.plural("leg", legs.size)}
    """.trimIndent().replace("\n", " ")

(That pluralising thing is a third-party library I dug up. No need to worry about that just now).

And now we can have the rest of our test code (all the above was the "arrange" part of the test):

val critter = Critter()
critter + Head()
critter + Arm() + Arm()
critter + Leg() + Leg() + Leg() + Leg()

critter.describe() shouldBe "Critter with 1 head, and 2 arms, and 4 legs"

It's silly, but I quite like that.

For the sake of completeness, I've tested the chaining works with different types:

fun `it can mix-up the body parts` () {
    val critter = Critter() + Head() + Arm() + Leg() + Leg() + Arm() + Leg() + Head() + Leg()

    critter.describe() shouldBe "Critter with 2 heads, and 2 arms, and 4 legs"
}

Invoke operator

I've messed around with this sort of thing with PHP's __invoke before. The invoke operator function represents the () operator one calls functions with; IE: if f is the function, one uses the () operator to call it: f(). Implementing this operator on a class lets one call objects like methods. Should one want to do that. Which, admittedly, is pretty rarely. But still.

class Logger {
    private var _log = mutableListOf<String>()
    val log: List<String>
        get() = _log

    operator fun invoke(message: String) {
        _log += message
    }
}

Here I have a logger class whose objects can be called as methods:

val logMessage = Logger() // logMessage is an object: an instance of Logger
logMessage("This is a log message")
logMessage("This is another log message")

The bit that effects this is the invoke function.

And the assert part of the test:

logMessage.log shouldBe listOf("This is a log message", "This is another log message")

The next thing I thought to try is how I might have a variadic invoke function: one I can pass any number of arguments to. This example is a bit contrived, but it shows it. Here a Task is an object that is initialised with a lambda, and when the task is invoked as a function, it runs the lambda with whatever the invocation is passed:

class Task(var lambda : (args: Array<String>) -> List<String>) {
    operator fun invoke(vararg args: String) : List<String> {
        return lambda(arrayOf(*args))
    }
}

One thing that is annoying is that a lambda can't be defined as having a vararg parameter, so I need to use an array of things there. However the invoke function can use vararg, so that's cool.

The * there is the spread operator, which converts the varargs passed to the invoke call to an array that the lambda needs. I could not find any other docs for this other than a reference in the vararg docs linked in the previous paragraph.

So I create my task with its lambda:

val task = Task { args -> args.toList() }

And I check the results:

val taskResult = task("This", "is", "a", "variadic", "invoke", "handler")
taskResult shouldBe listOf("This", "is", "a", "variadic", "invoke", "handler")

Both those lines look similar, so it's important to note the first one is passing six separate arguments to task (which is an object remember, not a function!), and the second line is one list with six items. The lambda coverted the varargs from the invoke call to the list it returned.


And that is as far as I got tonight, so I'll leave off here.

Here's the code:

Righto.

--
Adam