Create flatten method using only higher order functions