The joy of recursion, immutable data, & pure functions: Making mazes with JS

jrsinclair.com

87 points by jrsinclair 3 days ago


tikhonj - a day ago

If you enjoyed that, I have a blog post on generating mazes in Haskell (from over a decade ago!). The algorithm is very similar, but the code is written using "inductive graphs" and the post is really more of an intro to working with graphs in a purely functional style.

https://jelv.is/blog/Generating-Mazes-with-Inductive-Graphs

wk_end - a day ago

Kind of amusing and maybe telling that this article about implementing an algorithm functionally begins by explaining it in an iterative, mutational fashion.

hombre_fatal - a day ago

Nice animation on the maze building algo.

I remember trying to use Immutable.js back in the day. It's probably pretty great with Typescript these days, but I remember it was kinda hell with vanilla JS back then since I'd accidentally do things like assign `thing.foo = 42` instead of `thing.set('foo', 42)` and I'd comb through the code to see why it wouldn't work, and I remember not knowing when I had a JS object or a Record. All things fixed by Typescript, of course.

hyperhello - 15 hours ago

Once you have the maze you need to solve it. JavaScript is a great environment for tinkering. If you would like to try, I made an online maze game for myself this year.

https://hypervariety.com/Amaze/

joshfarrant - a day ago

The author has also written a book which I’ve read a couple of times and enjoyed.

A Skeptic’s Guide To Functional Programming With JavaScript

https://jrsinclair.com/skeptics-guide

I’d recommend it for functional-curious JS devs like myself.

sillysaurusx - a day ago

I’m still sad that JS doesn’t have tail call optimization. I’ve always wondered why. Is it hard to implement?

jefecoon - 3 days ago

Nice read, and beautiful website btw.

vismit2000 - a day ago

Related: http://www.mazesforprogrammers.com/

prakashrj - a day ago

[dead]

chowells - a day ago

I'm slightly horrified by the memory leak that's casually introduced without even a remark as to the potential to cause a problem. I can't tell if I'm more horrified by the cavalier attitude or the fact that JavaScript makes having a global registry the only easy way to use an object of an arbitrary type as a key to Map.

But at the very least, if you're going to memoize immutable values, please do it in a way that allows garbage collection. JavaScript has WeakRef and FinalizationRegistry. (Why it doesn't provide the obvious WeakCache built on those is a mystery, though.)

The issues won't be visible on a toy example like making mazes a few hundred elements across, but if you use these techniques on real problems, you absolutely need to cooperate with the garbage collector.