Unleashing the Power of Recursion
Recursion is a very powerful technique that allows us to represent complex algorithms in an elegant and concise way. If we look at the definition of recursion, we see that it’s a way of solving a problem by splitting it into smaller versions of itself until they become trivial to solve. A less elegant but more pragmatic definition is solving a problem with a function that calls itself.
For our problem, we would like to have a function that can accept a temporary variable called accumulator (in our case, the number of bytes read so far) and an operation (in our case, the call to read from the stream) and return a new accumulator. The accumulator is so called because it “accumulates” the partial results of the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access