R solution without closures

Here's a solution that does not make a closure to keep track of an `o` count.