Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript: Get a list of all paths from root to leaves (rose tree)
// 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
You can’t perform that action at this time.