code from the book functional programming in scala