I hope that in this year to come, you make mistakes. Because if you are making...– Neil Gaiman (via drinkyourjuice)
Functional 12 Days of Christmas - Shorter Version
so, I still wanted to leave me previous example up, since I thought it gave a simple example of how to use the ever-so-handy reduce() function, but I just found out about python’s sum function, which does allow for a much shorter version: sum(map(sum,map(lambda x: range(1, x+1),range(1,13)))) And, after having a flashback to algebra class, I did remember that you can calculate the sum of...
The functional 12 days of Christmas
I was asked what the total of number of items in the 12 days of Christmas song was, so I decided to see if I could come up with a functional solution in python. I came up with this one-liner: reduce(lambda x,y: x+y,reduce(lambda x, y: x+y, map(lambda x: range(1,1+x), range(1,13)))) which I thought was a nice solution, and proivdes a good example of functional programming practices and basic...