Proposal: improve the Data.Tree API

Ivan Lazar Miljenovic ivan.miljenovic at gmail.com
Mon Feb 24 10:50:49 UTC 2014


On 24 February 2014 21:39, João Cristóvão <jmacristovao at gmail.com> wrote:
> Hello,
>
> The Data.Tree API seems rather poor. Some research on hackage shows some
> additional functions being defined in very unrelated packages:
>
> http://hackage.haskell.org/package/debian-3.81/docs/Debian-Apt-Dependencies.html#g:3
>
> http://hackage.haskell.org/package/hledger-lib-0.22.1/docs/Hledger-Utils.html#v:subtreeat
>
> I propose the addition of the following functions, that seem rather straigh
> forward to me:
>
> -- | get the sub-tree rooted at the first (left-most, depth-first)
> occurrence
> -- of the specified node value
> lookupTree :: Eq a => a -> Tree a -> Maybe (Tree a)
>
> -- | get the sub-tree rooted at the first (left-most, depth-first) value
> that
> -- matches the provided condition
> findTree :: (a -> Bool) -> Tree a -> Maybe (Tree a)
>
> -- | keep only the elements that match the provided condition
> filter :: (a -> Bool) -> Tree a -> Tree a
>
> The 'Tree' is appended in the name, to distinguish from the Foldable
> instances, that return the value, and not a sub-tree.
>
> Additionally, the following two functions might also be useful (even if the
> implementation is very simple in the second case):
> -- | get the sub-tree for the specified node value in the first tree in
> -- forest in which it occurs.
> lookupTreeInForest :: Eq a => a -> [Tree a] -> Maybe (Tree a)
>
> -- | Length of the tree
> length :: Tree a -> Int

What is the length of a tree?  The number of sub-trees of that
particular root node?  The overall width of the tree?  The number of
nodes?

>
> There are probably more useful functions to add, so suggestions are
> welcomed.
>
> Previous discussion:
> https://github.com/haskell/containers/issues/39#issuecomment-35799628
>
> Discussion period: 2 weeks
>
> Cheers,
> João
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>



-- 
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
http://IvanMiljenovic.wordpress.com


More information about the Libraries mailing list