ProcGen Fun 0: Introduction

18 December 2024

Introductory post to a series combining procedural generation and functional programming.

Here begins a series of blog posts combining two concepts I've long since been interested in: procedural generation and functional programming.

Procedural generation (procgen) is the technique of creating data algorithmically instead of manually. It's typically powered by randomness operating within certain constraints. This allows you to generate many different instances of a structure which all follow a coherent pattern.

I'm utterly fascinated by the idea of giving a computer a set of rules and a random number generator and it giving you something beautiful in return! For example, the beautiful village map below was generated procedurally using Oleg Dolya's village generator. I love seeing what people come up with on the r/proceduralgeneration subreddit.

Procedurally generated village

Functional programming (FP) is a programming paradigm which centres on the idea of composing functions. It's often considered in contrast to imperative programming, where successive statements update the state of the program. As much as possible, functions in FP should be pure, meaning:

In my experience, applying principles from functional programming tends to make your code much easier to read and reason about. For example, avoiding side-effects means that you can focus on just the inputs and outputs to a called function.

At first glance it might seem odd to combine procedural generation and functional programming. Randomness is an inherently impure concept; by definition, you don't always get the same answer when you ask for a random number! However, there are ways of expressing randomness in a functional way (as we shall see), and I'm intrigued to see whether we're able to reap the benefits of functional principles within an intrinsically random system.

In this series I'll be using C#. This might seem an odd choice, as F# is the functional-first language within the .NET ecosystem. However, C# has embraced many functional principles in recent years, and it's much more familiar to me (and probably my readers). Perhaps a post later on in this series could explore migrating the functional core to F#.

The source code for this series can be found on GitHub; each post will have its own branch so you can follow along if you like.

List of posts

Here's a list of posts in the series so far; I'll update this as new posts are added!