I’m not sure what would be acceptable output for a code generation tool if rewriting the examples isn’t ok and reimplementing something that performs the same function still isn’t ok. Are we automatically granting de-facto code patents on all published code now?
I understand why these might feel different to you, but textbooks and stack overflow are also proprietary, licensed pieces of work. I don’t see why there would be much of a legal distinction.