some scala code copy from some book