This little exercise in javascript was heavily inspired by "Reversing the technical interview", which is pretty much the same except done in common-lisp. i implemented mine in JS, partly to have more esoteric code to scare colleagues with :3 This entire exercise starts with the following assertion, directly quoted from the article linked above. “What else are lists,” you reply, your eyes flashing, “But alternatives?” My cell type could indeed stay a boring struct, or a tuple, or anything in-between, but let's not really "store" any of that data. Instead, we'll let the runtime deal with it. const cell = (v, next) => (x) => x ? v : next; This is our container as well as our init fn, and it can be used in a pretty standard way. const empty = null // [] const single = cell(5, null) // [5] const triple = cell(1, cell(2, cell(3, null))) // [1, 2, 3] Accessing data is also pretty simple, calling the cell with something truthy will return the value, and falsy will return the next cell (or…
No comments yet. Log in to reply on the Fediverse. Comments will appear here.