huge rewrite, split to base and yandex-specific parts

Hmm, as I see, it's actually itertools function. Why do not use it from lib?