Use map, sum in Subtree::coount instead of fold

This commit is contained in:
Edwin Cheng 2019-04-22 17:37:27 +08:00
parent d6d9aa2003
commit ad1c3b5bd6

View File

@ -153,11 +153,15 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl Subtree {
/// Count the number of tokens recursively
pub fn count(&self) -> usize {
self.token_trees.iter().fold(self.token_trees.len(), |acc, c| {
acc + match c {
let children_count = self
.token_trees
.iter()
.map(|c| match c {
TokenTree::Subtree(c) => c.count(),
_ => 0,
}
})
})
.sum::<usize>();
self.token_trees.len() + children_count
}
}