Skip to content

Instantly share code, notes, and snippets.

@kurtmilam
Last active August 9, 2017 18:07
Show Gist options
  • Save kurtmilam/458f23c788e391abaa89c2aa84012600 to your computer and use it in GitHub Desktop.
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)
// 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