The other day, in JavaScript, I wrote some code that required three nested for-loops in order to locate data for consumption. The JavaScript code looked something like this:
screens.forEach(
( screen ) => {
screen.conversations.forEach(
( conversation ) => {
conversation.comments.forEach(
( comment ) => {
doSomethingWith( screen, conversation, comment );
}
);
}
);
}
);
view rawsnippet-1.js hosted with ❤ by GitHub
This is hella ugly; and, on some level, “feels wrong” in a way that I can’t fully articulate. As such, I thought it would be a fun to try and figure out a way to encapsulate this nested looping using some sort of a “Visitor” Function. Meaning, create an algorithm that takes an object graph and an operator and then have it invoke said operator for each desired leaf-node within the graph.
#coldfusion #cfml #function