Skip to content

Instantly share code, notes, and snippets.

@Lucifier129
Last active May 2, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lucifier129/cce5b10cb8258005f09a8270a5292993 to your computer and use it in GitHub Desktop.
Save Lucifier129/cce5b10cb8258005f09a8270a5292993 to your computer and use it in GitHub Desktop.
let identity = x => x
let createBF = (getChildren) => {
let handler = (stack = [], results = [], f) => {
if (stack.length === 0) return results;
let [head, ...tail] = stack;
let nextStack = tail.concat(getChildren(head));
return handler(nextStack, [...results, f(head)], f);
};
return (tree, f = identity) => handler([tree], [], f);
};
let tree = [
0,
[1,
[2],
[3],
[4,
[5]
]
],
[6],
[7]
];
let bfForTree = createBF((tree) => tree.slice(1));
let bfForDOM = createBF(tree => Array.from(tree.children))
console.log('bfForTree', bfForTree(tree, node => node[0]));
console.log('bfForDOM', bfForDOM(document.documentElement, node => node.tagName.toLowerCase()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment