minor correction: JavaScript has Array.reduce, not fold.
Also, it seems to me that it would be helpful to explain mapAccumR, since that is doing the real heavy lifting here. Presumably you could implement that in Java (or maybe there is a library that does it already) and the solutions would become more or less the same.
The nice thing is, mapAccumR is not doing much either. It relies completely on the power of Traversable the class that the compiler generated for us. If you would change the type signature of solve to allow for other Traversables than Tree, the code would work without any changes also for lists or arrays for example. All thanks to the power of fundamental abstractions.
I've added a short paragraph that shows that mapAccumR is not a lot of code.
minor correction: JavaScript has
Array.reduce
, notfold
.Also, it seems to me that it would be helpful to explain
mapAccumR
, since that is doing the real heavy lifting here. Presumably you could implement that in Java (or maybe there is a library that does it already) and the solutions would become more or less the same.Yes, fixed the
Array.reduce
The nice thing is,
mapAccumR
is not doing much either. It relies completely on the power ofTraversable
the class that the compiler generated for us. If you would change the type signature ofsolve
to allow for otherTraversable
s thanTree
, the code would work without any changes also for lists or arrays for example. All thanks to the power of fundamental abstractions.I've added a short paragraph that shows that
mapAccumR
is not a lot of code.Maybe it would help clear the confusion by mentioning that
deriving
creates functions with certain rules (map, fold, etc.) that operate on the types?