# Proposal: improve the Data.Tree API

João Cristóvão jmacristovao at gmail.com
Thu Feb 27 11:34:23 UTC 2014

```Hey,
So, proposal 2.0, with the received feedback:

-- | 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)

-- | 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)

-- | keep only the elements that match the provided condition
filter :: (a -> Bool) -> Tree a -> Tree a

-- | Size of the (flattened) tree
size :: Tree a -> Int
size = getSum . F.foldMap (const \$ Sum 1)

-- | Maximum depth of tree
maxDepth :: Tree a -> Int

-- | Remove all nodes past a certain depth
prune :: Int -> Tree a -> Tree a

-- | Take the mirror-image of a tree
mirror :: Tree a -> Tree a
mirror (Node a ts) = Node a . reverse \$ map mirror ts

Thanks,
João

2014-02-24 12:55 GMT+00:00 João Cristóvão <jmacristovao at gmail.com>:
> Hi Ivan,
>
>
>>> then what about the arguably better name `size`?
>
> We do?
> If there is consensus I would then add `size` with the arguably more
> efficient implementation:
> size :: Tree a -> Int
> size = getSum . F.foldMap (const \$ Sum 1)
>
>
>> * An Ord instance (achievable via standalone deriving, though this isn't
>> ideal)
>
> Agreed.
>
>> * Functions to take/drop so many levels of the tree (take is
>> relatively easy; drop would result in a Forest).
>
> Similar to treeprune in
> ?
>
>
>> mirror :: Tree a -> Tree a
>> mirror (Node a ts) = Node a . reverse \$ map mirror ts
>
> useful, why not...
>
> João
>
>
> 2014-02-24 11:38 GMT+00:00 Ivan Lazar Miljenovic
> <ivan.miljenovic at gmail.com>:
>
>> On 24 February 2014 22:17, Tobias Florek <haskell at ibotty.net> wrote:
>> > hi,
>> >
>> >> to drop this particular function (length) from the proposal, if there
>> >> is
>> >> no consensus.
>> >
>> > then what about the arguably better name `size`?
>>
>>
>> Some things I missed when I last used Data.Tree:
>>
>> * An Ord instance (achievable via standalone deriving, though this isn't
>> ideal)
>>
>> * A function to take the mirror-image of a tree (name not that important):
>>
>> mirror :: Tree a -> Tree a
>> mirror (Node a ts) = Node a . reverse \$ map mirror ts
>>
>> * Functions to take/drop so many levels of the tree (take is
>> relatively easy; drop would result in a Forest).
>>
>>
>> >
>> > cheers,
>> >  tobias florek
>> >
>> >
>> > _______________________________________________
>> > Libraries mailing list
>>
>>
>>
>> --
>> Ivan Lazar Miljenovic
>> Ivan.Miljenovic at gmail.com
>> http://IvanMiljenovic.wordpress.com
>> _______________________________________________
>> Libraries mailing list