I'm trying to learn scala. It's hard.
I currently have a tree in the form
class Node(children:List[Node], value:Int){
}
I want to calculate a total cost defined by value + the sum of the totalcost of the children. My java background made me do this:
def totalCost() {
var total = value
for (child <- children){
total = total+child.totalCost
}
return total
}
now I know I should be folding or reducing, but it's not coming out. Could some of you frendly helpers give me a hand here?
totalCost
method, which works fine, but isn't very idiomatic. \$\endgroup\$