Last active
August 9, 2017 18:07
-
-
Save kurtmilam/458f23c788e391abaa89c2aa84012600 to your computer and use it in GitHub Desktop.
JavaScript: Get a list of all paths from root to leaves (rose tree)
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
// The rose tree is a data structure that's used to represent hierarchical | |
// data like XML / HTML documents or filesystems (directory and file structures). | |
// See this in action on the Ramda REPL: https://goo.gl/XeToZt | |
// R = Ramda :: http://ramdajs.com/docs/ | |
// much cleaner solution, thanks to @syaiful6 in Ramda's Gitter channel ( https://gitter.im/ramda/ramda ) | |
const listPathsFromRoseTree = | |
tree => | |
!is( Array, tree[ 1 ] ) | |
|| isEmpty( tree[ 1 ] ) | |
? [ [ tree[ 0 ] ] ] | |
: map( prepend( tree[ 0 ] ) ) | |
( chain( listPathsFromRoseTree ) | |
( tree[ 1 ] ) | |
) | |
listPathsFromRoseTree( roseTree ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment