scala implementation of shifting word