beautiful scala code is also more efficient
merge my beautiful and efficient scala code