• Dynamic Syntax Highlighting with React.js

Let’s say you’re building a modern web app. You’re using react, you’re set up with webpack code splitting, you render stuff on the server-side, everything is awesome. Even better, your dependencies are really lean-and-mean: you fetch stuff as you go, so the initial bundle is very small!

You might run into a problem, though, if you need to render user-generated Markdown. You’re already using the excellent react-markdown library, so that’s all working fine, but the recommended way to do syntax highlighting has the rather large problem that you need to manually require the languages you want to use. Worse, they’re all bundled together.

You don’t want this. You want to be able to deliver no highlighting code, then allow the user to dynamically fetch the required highlighting files, like how the rest of your app works. Ideally, you even want it to render a non-highlighted version first, then switch to the highlighted one when its ready. How might this get done?

• Templates, Index Sequence, and Optimization

Recently, I’ve been working on a 2D game. Full-time, in fact: I recently graduated early, and I figured I might as well take a year and a half to work on this project before getting a “real” job. I initially started creating a game in Unity, however, I quickly realized that I didn’t actually like the engine. I actually disliked it fairly heavily, and it was hurting my productivity. So, naturally, my next thought was to code the game from scratch.

The natural choice for this was C++. I messed around with making a 2D engine before and enjoyed it, even if I didn’t actually get very far. I took a bit of a different approach this time, intentionally trying to make a game instead of an engine. However, that doesn’t mean that I don’t occasionally encounter engine-dev-related problems. One interesting recent one: optimizing a component of my ECS system.

• Luck and Analysis

Over the last few years, I’ve become progressively more interested in tabletop games. As a result, I’ve been playing more of them—and thus rolling more dice. Now, as with any game of chance, occasionally luck isn’t in my favor and and I’ll consistently do much lower damage than I feel like I should. When you do 5 damage on an attack that averages 16, it feels pretty awful. I, however, wanted to know more. I wanted to know exaclty how unlucky I was getting. Being a programmer, my solution, naturally, was to write some code to do so.

• value_ptr

In today’s blog post, we’re going to discuss the idea of a value_ptr. Basically, the idea is that it’s pointer with value semantics. Why would you want this? Well, polymorphism for one. For another, you could conceivably want to allocate a very large number of some very large objects on the stack (perhaps recursively), and you don’t want to cause an overflow.

This has been implemented before, but I didn’t actually read the source for that. Instead, I’ll write it myself.

• An alternative way to Memoize in Ruby

I was screwing around with Ruby Coroutines, and the concept of coroutines in general, when I came across this page. It talks about how you can use coroutines to memoize functions. Ruby has native coroutines. You can guess what happened next.

Here’s what I came up with:

class Object
def self.fiber_memoize(method_name)
meth = self.instance_method(method_name)
self.send(:define_method, method_name) do
f = Fiber.new do |s|
result = meth.bind(self).call
loop do
Fiber.yield(result)
end
end
self.send(:define_singleton_method, method_name) do
f.resume
end
f.resume
end
end
end


This works on an instance-level, which a niave implementation wouldn’t. It also works by re-defining methods on a per-instance basis when you call them, which is just another example of how much Ruby rocks.

Here’s how to use it:

class Factorial
def initialize(num)
@num = num
end

def result
puts "This run is not memoized"
(1..@num).inject(:*) || 1
end

fiber_memoize :result
end

f = Factorial.new(100)
puts f.result #=> Prints the not memoized message, then the result
puts f.result #=> Prints the (now memoized) result


Interesting, this is also a way to do memoization in ruby without any conditionals, which is pretty neat.

Please never use this in any kind of production environment. I’m begging you.