Last active
May 2, 2020 15:20
-
-
Save Lucifier129/cce5b10cb8258005f09a8270a5292993 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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