Allow stateful function in dataset.map
@lhoestq Added a test. If you can come up with a better stateful callable, I'm all ears 😄.   Sorry I said earlier that it was good to have it inside the loop, my mistake ! @lhoestq Okay, did some refactoring and now the "cache" part comes before the for loop. Thanks for the guidance.

Think this is ready for the final review.
Removes the "test type" section in Dataset.map which would modify the state of the stateful function. Now, the return type of the map function is inferred after processing the first example.

Fixes #1940 

@lhoestq Not very happy with the usage of `nonlocal`. Would like to hear your opinion on this.