I was implementing this thing that took a minimum spanning tree and divided it into what I called "limbs". (The colorful thick lines in the image are limbs.)
First, I have to explain what I call "junctions": They're nodes at which three or more edges come together. So, a node that just joins two lines is not a junction here. A node that joins four is.
A "limb" is a sequence of edges between nodes that has no junctions in its middle. So, if a sequence of edges goes Node A, Node B, Node C, Node D, neither B nor C can be junctions. A or D can be junctions (they can also be dead ends that only connect to one edge).
(I'm doing this because I have a module that won't work on sequences with junctions in them.)
I was really confused while implementing this because I kept getting limbs that 1) did have junctions in the middle, 2) had edges that weren't even part of the minimum spanning tree it was searching, and 3) were fairly long.
It turned out the problem was this:
var limbs = junctionNode.links
.map(curry(followLinkToFillLimb)([ junctionNode ]))
`followLinkToFillLimb` is a function that successively (and recursively) follows a links from nodes, stopping when it reaches a junction or dead end (or a cycle). Along the way, it appends to an array of nodes, which is ultimately what it returns. Its parameters are the initial array and the next node to follow.
Currying is a technique in which you "pre-load" a function with some of the parameters, and then when the rest of the parameters are given to the function, it executes. Above, I was preloading the initial array so that I could use it to map over the junction's links.
What I did not realize was that the code above creates the initial array once and
passes that same array to every invocation of the function. And so, followLinkToFillLimb appended every node to the same limb.
This is the code that did what I intended:
var limbs = [];
for (var i = 0; i < junctionNode.links.length; ++i) {
limbs.push(
wrapInLimbObject(
followLinkToFillLimb([junctionNode], junctionNode.links[i])
)
);
}