"What Am I Doing: December 2022"

2022-12-23

I am starting to use my blog as a web log, just write whatever I am tinkering with at the moment. Nothing here is something I am necessarily committed to, just things to tinker with. This will continue month to month and new posts will be added per month.

Some of this stuff is primarily influenced by getting myself ready for new projects and new work opportunities at the moment.

My 'what I am doing' are an ongoing log of stuff for a month. I might add more to this one. As I write this, I have other things to try out.

Getting back into React

I wanted to start learning React again, primarily if I would start working more with JavaScript and TypeScript again. I have been on a little hiatus away from web development generally and JS/TS/React specifically. (This was also influenced by a potential job opportunity ;) )

In an effort to start build something I would use, I wrote a simple Bible daily-reading browser, using the King James version as input. The result is here: https://gitlab.com/bolsen80/bibledaily.

  1. I pulled the text from Project Gutenberg. I thought the text was written in a parseable way, since initially it seemed like there was some order to it. So I started to edit the file and produce diffs from it. I am not interested in storing the full text in Git, but rather just store a diff file I can generate a patch to. The result however was that I was making a lot of changes manually. Without a useful sed command immediately in my head (it was actually though simple), I just hacked around it with my Python script because I wanted to not spend too much time parsing it. The result was not perfect, but it was good enough.

    • In particular, there was verses on the same "line", like in a form like "and then the LORD said: 1:10 it is so!" and my initial script didn't consider that. If I want to do another pass at it, I can fix those up and it to the diff.
    • The boring bits, like the "API" use Flask in Python. The script that parses the text also shoves the data into an SQLite3 database, making the text searchable by verse. There is some code to parse out verse inputs, but right now, it is just to grab ranges. The reading plan comes from https://www.biblica.com/resources/reading-plans/?plan=1&translation=kjv. As part of setting up the text, I needed to align the naming of Bible books to what is in the reading plan. This was just some simple but tedious work to get through. The Python script just parses through each line and generates rows in the SQLite table to describe the ranges easily.
  2. I wanted to push myself to model the React application as a Redux-without-Redux style application, just using hooks, specifically useContext() and useReducer(), but to also use them with "thunks".

    • There are cases where you want to do a state change in a reducer and then follow up with a new dispatch. Technically, this is an anti-pattern. If you think in terms of functional programming and the goal of doing functional patterns in Redux, you will know that any function that would do something other than take an input and always return the same output (a pure function) can introduce side-effects. Dispatching is a side-effect; it couples a system-level mechanism to a function that should only transition a state.
      • The purpose of a thunk is to abstract the away that second dispatch so that reducer functions can maintain their status as pure functions. In essence, it is like a Haskell monad; we are encapsulating and abstracting the side-effect away from the pure function.
      • With React hooks, it is possible to do this:
        • Make a new state in a useContext
        • Create a StateProvider that wraps components with something that can get both the state and the reducer functions
        • Write thunk-style action creators as usual.
      • The thing was though is that I had to use this: https://github.com/intergalacticspacehighway/use-reducer-thunk. I tried to write it myself, but I spent a better part of a day trying to figure out a complex type puzzle in TypeScript. It didn't feel like progress to fight the type system. The only problem with the aforementioned project was that, while it was written in TypeScript, it doesn't expose the types to make it useful. So I brought it into my own project, and exposed what was necessary. Once I got over that hump, I implemented a basic reducer/action creator and it worked as expected.
  3. For the purpose of my project, reactive-style programming was not necessary. I decided to go back to just using setState() for a primary component, and use functional components for the rest of the system. I learned in the process and it was iterated that components as classes are not really preferred anymore. If I feel like it, I would write my base component with the state management in that component to use useState().

In the end, I created a nice, useful little thing for myself.

Java

I have touched Java on-and-off most of my career, except from work the past couple of years. But, it is something I would like to work in. So, I spun up a new non-project.. I think I want to use Java for an idea I have for a toy project: create a P2P network to send messages through in an encrypted fashion.

FWIW, this is also influenced by some future work opportunities. ;)

Here is the Git repository on Gitlab: https://gitlab.com/bolsen80/javaexperiments.

So far, I tried:

Picking out understanding from Roaring bitmaps

I haven't read the paper yet on it, but I read this: https://vikramoberoi.com/a-primer-on-roaring-bitmaps-what-they-are-and-how-they-work/.

I wanted to understand a bit on how to make a set of integers just using a bitmap. This is used as an approach to densely pack integers into an array. There are however interesting properties that become constantly clearer. In Chapter 1 of "Programming Bitcoin" by Jimmy Song https://github.com/jimmysong/programmingbitcoin, he described as part of understanding elliptic curve cryptography: "If it helps, you can think of modulo arithmetic as "wraparound" or "clock" math." With that image in my head, this logic can apply to handling the type of expectations when packing a single integer (in the above link they used 32-bit integers as an example) to: 1. Find the top 16-bits of the integer 2. Find the bit that can be set to 1 to add an integer to an array. 3. Use the same logic to find the bit in an array.

Seeing just this, it is possible to write set-logic over a single 32-bit integer (or 64-bit for a larger space) using basically O(1) algorithms that run in just a couple of CPU instructions. Even in high level languages, this is really tempting to tinker with (however, in high-level languages, integers tend to be owned by the runtime. I remember like Lua giving a 31-bit integer. I think it was becuase it was just handled the signed-ness of an integer.

However, for now, here is what I started to use to understand this a little better: A function that takes a number, and returns a list of 0's or 1's to give me a full view of a "32-bit" integer:

function tobits(num) {
  res = [];
  for (var i = 0; i < 31; i++) {
    res.push(num & 1);
    num = num >> 1;
  }
  return res.reverse();
}

Doing bit manipulation is not my strongest skill, but I was happy to just figure out how to view each bit. The pattern of (N & 1) >> k is pretty useful for a number of circumstances, (like parameter flags, if you then & a flag, where the flag is something like 2**k.)

New battery for my laptop

I got a new laptop battery for my computer. The old ones are fine (I have a ThinkPad with hot-swappable batteries - remember those? :D) I got from the same company on AliExpress, called KingSener, in the past and thought that they are quality batteries after some extended use : (https://kingsener.aliexpress.com/store/1898372)[https://kingsener.aliexpress.com/store/1898372]. They use cells from Japanese companies (Sanyo, LG, etc.) and are very good in terms of quality and price. This time around, the shipment came much faster from China than before, coming in under a month.

I am running the battery through the initial calibration process. New laptops come with batteries pre-calibrated. Calibration involves getting the battery chips familiar with what the batteries are capable of so it can estimate better the actual capability of the battery. So this involves a 2-3 day cycle of charging it all the way and discharging close to 0.

Initially, it seems that the kernel is getting wrong signals from the battery controller as it tends to get wrong capacity measurements, but I expect this is not going to be a problem.

For some reason, I can't use TLP's recalibration feature (TLP is a power management utility in Linux. For ThinkPads only: if you plug it in and run tlp recalibrate, it will discharge to 0 and then keep the computer on while it recharges the battery.) So, I just have to do it the old-fashioned way. Regardless, I run sudo watch tlp-stat -b to keep an eye on all the parameters.

2022 in retrospective

I have always tried to be perfect on things, but it hasn't worked out well. I decided late last year to just read and do things and see what comes out. So far, it's been interesting. I might have a very vague goal for something, but in reality, not being too goal-oriented and just focus on doing things has helped a bit. I figure that if I do something, I will learn something new.

This is also the first year after a long time that I left my long-term job to get back into application development. However, the past few years at work has been fruitful, at least to really identify where I think I could be valuable going forward. I'll follow up in next month's 'What am I doing' post in terms of that.

Conclusion

It seems like I am all over the place this month. I think at some point it will settle a bit more into the aforementioned toy project or whatever comes next. :)

I also have some other ideas: 1. This site has no analytics. I will NOT install Google Analytics, but instead find a Nginx log parser, or write one, that I can push into a tool like https://munin-monitoring.org/ and maybe a Nginx Lua module to feed data into to get real-time counts. This sounds all hacky sounding, but bear with me this one: it's just another toy :) 2. I am interested in learning D2, which looks to be a better PlantUML: https://d2lang.com/tour/intro/. 3. Going to maybe write a presentation on some Java topic, acting though as a complete non-expert, maybe on the topic of Netty.

In: programming what_i_am_doing