Monday, March 16, 2026

Javascript: Just use eval

So we want to show in the interface the number of online players, so we need to "bind" a div to the value nested in an object:

<span data-bind="network.players.length"></span>

So we implement a function that with a given path and a root/global object as a starting point we get the value. But actually we want the length + 1 because that list does not include the current player... now what?

Should implement arithmetic operations, should we create a getter for length_plus1, should we pre-calculate the value, should we do any of that and cache it? No.

We just use the best function that a interpreted language can offer: eval.

But eval has security problems you say, it has no security problems its just perfectly fine, the only security problem is the developer. But it could cause performance problems, it can but will make it a provider/producer function and it will be perfectly fine:

provider = eval("() => " + expression)  // once
element.innerText = provider()          // forever

And if you think about this is even faster than the property lookup path parser version because it has now been "compiled" into a function that does no parsing at all.



done_

Saturday, March 14, 2026

Regex: From state machine to code and back

You want to grep all the hrefs from a files.

You write a simple grep, then you realize you actually want the non greedy, so you add the perl flag, then you realize the you don't need it, you can just use the negated selection, but now you don't get the last ", but actually you dont want it, not even the prefix, you just need the value:

grep -o  'href=".*"'
grep -oP 'href=".*?"'
grep -o  'href="[^"]*'

And now the hardships really start, ok let put back the flag and lets try to group the value we want, but doesn't matter grep doesn't care at all, so let go all out: look ahead, look up if its lt equals or equals lt, so, should we just add the look behind so the osd is satisfied:

grep -oP 'href="([^"]*)'
grep -oP '(?<=href=")[^"]*'
grep -oP '(?<=href=").*?(?=")'

Wait a second, what about the \K, they said is the solution for everything, (it basically drops the so far matched pattern), so its seems we are back to very good level of verbosity, lets make some final touches and just remove any trailing slashes at the end, and we are back to the look ahead:

grep -oP 'href="\K[^"]*'
grep -oP 'href="\K.*?(?=/?")'

This process can't be the best we can have. There should be way to convey what we want without these constrains.

Let's try by increasing the number of available operations and also reduce the possible compositions that are possible. And by making the operations just named things like functions (and with auto complete) we avoid of the negatives of having more operations. Less composition means reduced efficiency, but we can just compile it back down to a state machine (even at compile time). So instead of writing a state machine, we will just write code that looks like code. Prototype:

(i) => {
  if (i.drop_start('href="')) {  // drop = not in the match
    if (i.match_until('"')) {
      i.drop_trailing('/')       // no if = optional
      return i
    }
  }
  return null
}

And as long the the function is pure all the call happening in belong to the implementation, it should be possible to compile to a state machine and then optimize it at that level, and even produce the equivalent regex. And now its possible to also debug the matching because, during development, we can just keep the code instead of the compiled version.

And of course if the above code can produce the state machine, the following code that is semantically equivalent will also be able to used:

(i) => {
  if (i.drop_start('href="') && i.match_until('"')) {
    i.drop_trailing('/')
    return i
  }
  return null
}

Or going all the way to other end:

Match.drop_start('href="').match_until('"').optionally.drop_trailing('/')

But, keep in mind this is not a regex builder, there should be no limitations as long as you use the function provided, and also a function provided could be match_xml_attribute_value. Now how is going to create this?



done_

Sunday, March 8, 2026

Code Math: Sine as pseudo 1D noise generator

Lets use sine as a randomness provider to generate a infinite sequence of random numbers that change smoothly with no state:

1. Step wave that grows from 0-1 and resets back to 0 again every 1:

step(x) = ((x % 1) + 1) % 1

2-3. The random number generator from the last post:

random(x) = sin(floor(x) ** 3)

4. Interpolate between the current random number and the next:

noise(x) = step(x) * random(x + 1) + (1 - step(x)) * random(x)

5. Combine multiple sequences of different frequencies and amplitudes:

noise(x) + noise(x * 2) / 2 + noise(x * 4) / 4 + noise(x * 8 ) / 8

Graph of each:



done_

Code Math: Sine as pseudo random number generator

Lets use sine as a randomness provider to generate a infinite sequence of random numbers with no state:

1. Sine wave: 

sin(x)

2. Sample the wave by an non linear expression in order to avoid the linear repeat pattern of the sine: 

sin(x ** 3)

3. Extract only the values at integer points that represent the indexes of the random numbers in the sequence: 

sin(floor(x) ** 3)

4. Map the optional seed to the 0-1 range and added multiplied by 2PI in the sample location: 

sin((floor(x) + seed * 2 * Math.PI) ** 3)

Graph of each:



done_

Wednesday, March 4, 2026

Clipboard: The copy lie

You select a piece of text, right-click and select Copy. As the name suggests, you have now have a copy of the text in the copy vault (or clipboard if you prefer) untill the computer dies.

Now the lie, if you do the same with a file, you dont get a copy in the copy vault. You get a copy to the file path and when you do a paste, then and only then a copy is created.

It the simple case, everything works, but if the "marked for copy in the future" file is deleted or the system has no longer access to it, instead of a copy you get an error on paste.

If you wanted to actual make a copy, you could make a hardlink if the files is on a physical filesystem, create an actuall copy in memory if the file is small, or just create an actual copy in worst case.

But the problem is not that fake copy is default, the problem is that there is not an option for the actual copy. Bellow the Copy option should be a 'Copy for real' option.



next_