implemented takeWhile via foldRight.