I had a few minutes, so I ported the simplest function, and gave it some tests;