# Extending Base functions for a tree

**URL:** <https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722>\
**Category:** General Usage\
**Created:** [March 8, 2020, 4:49pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722 "2020-03-08T16:49:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [March 8, 2020, 4:49pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/1 "2020-03-08T16:49:52Z")

</div>

As I recall,

- `Base.length` is used to return types with a single dimension
- `Base.size` is used to return types with more than one dimension

I have build a tree type and I want to return the total number of nodes in the tree. Should I extend `length`, `size` or neither?

Further, each node of the tree carry a “weight”. So I am thinking about extending `Base.sum` for the total weight.

Any thoughts welcome. Thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 8, 2020, 5:13pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/2 "2020-03-08T17:13:21Z")

</div>

`size` should probably be consistent with `length` if implemented. Assuming the type support iteration implementing `length` will be very fair.

For `sum`, again, if it is iterable it’ll be strange if the sum is not summing the item in iteration. If iteration returns the weight it’ll be fair but otherwise I think you’ll be better off with a different function

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [March 8, 2020, 5:42pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/3 "2020-03-08T17:42:37Z")

</div>

When you say consistent, do you mean that `size` should return a tuple and `length` should return a single number? I was struggling due to the shape of the tree because it is neither linear nor multi-dimensional 🙂

The type supports iteration in the sense that it can be traversed depth-first or breath-first (currently depth-first only). The nodes have composite type so I am implementing `sum(f, itr)` interface and expects function `f` to return a numeric type.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 8, 2020, 5:46pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/4 "2020-03-08T17:46:31Z")

</div>

> [@tk3369](#):
>
> When you say consistent, do you mean that `size` should return a tuple and `length` should return a single number? I was struggling due to the shape of the tree because it is neither linear nor multi-dimensional 🙂

Yes. And although I’m not sure right now what the official rule would be, my rule of thumb will be that if you support indexing then it has an interface that looks like an array that you can have `size`. If you don’t then don’t bother.

Put it another way, follow `Set` or `Dict`.

> [@tk3369](#):
>
> The nodes have composite type so I am implementing `sum(f, itr)` interface and expects function `f` to return a numeric type.

In that case I’d say if the iteration returns nodes that doesn’t support arithmetic then you probably should still not special case `sum` even though it does’t really conflict with anything. (But sincne there’s no conflict you can also do what you feel convinient without causing much trouble). sth like `sum(node_weight, iter)` shouldn’t be too hard to write either though…

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [March 8, 2020, 6:01pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/5 "2020-03-08T18:01:11Z")

</div>

Actually, there could be a couple of summable fields besides weight. Should I define something like this?

```julia
Base.sum(::Tree) = error("Please use `sum(f, ::Tree)` instead.")

```

Mmm… if I do that then it seems to be a hassle because I would have to do the same thing for other statistical functions like `mean`, `std`, `var`, etc.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 8, 2020, 6:06pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/6 "2020-03-08T18:06:50Z")

</div>

Just leave them unimplemented should be fine? You are not extending all the functions that you are not planning to implement are you?

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [March 8, 2020, 6:11pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/7 "2020-03-08T18:11:45Z")

</div>

Sure, I can leave them unimplemented. I was just thinking about being a good “citizen” for implementing the full interface. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 8, 2020, 6:21pm UTC](https://discourse.julialang.org/t/extending-base-functions-for-a-tree/35722/8 "2020-03-08T18:21:50Z")

</div>

> [@tk3369](#):
>
> I have build a tree type and I want to return the total number of nodes in the tree. Should I extend `length` , `size` or neither?

This depends on whether you want to support any of the [interfaces](https://docs.julialang.org/en/v1/manual/interfaces/).

I am not sure that the array interface, which has `size`, makes much sense for a tree. But if you want to support iteration, you should define `Base.length` and the relevant traits if applicable. And, crucially, `Base.iterate`.

If you do not want to support either interface, you are free to do what you like. Some types in packages define `length` without supporting iteration.
