Functional programming in R