Sunday, February 24, 2013

Anonymous Functions

There's a certain joy that comes of figuring out something yourself that you don't really get when someone else teaches you (although the best teachers teach in such a way that you're constantly figuring things out yourself).

I've never taken a computer science class in programming, so most of what I've learned I've figured out for myself. There are many little milestones along the way, but there are some big ones that stand out as large leaps forward in understanding. The day I suddenly understood classes in Python, for instance. I'd read the documentation on them several times before, but had never been able to make heads or tails of it, until the day when I really needed to use which point it all suddenly clicked, and I reread the documentation and understood everything. It was an exhilarating, empowering feeling. My abilities as a programmer grew a bit that day, and I've used classes often since to write better code than I otherwise could have.

Last Friday I had a similar experience, suddenly understanding lambda functions in Python (known more generally as anonymous functions). (I also understood two specific functions last week, but that's not quite as interesting or exciting as understanding a whole new class of object.)

Lambda (or anonymous) functions are functions that you create when you don't need or want to go to the bother of defining a full-fledged function. Sometimes all you need is a simple function for one particular part of your code that you don't intend to use anywhere else, for instance if you need to square each item in a list of numbers. It's such a trivial thing that it isn't worth the trouble of writing a separate function for, especially if that's the only place you're going to need it.

Instead, you can write a simple lambda function to do the work for you. For example, given a list of numbers:

someList = [1, 2, 3, 4]
for number in map(lambda x: x * x, someList):
This code would then print out 1, 4, 9, 16. (Incidentally, the map function is one of the other functions I understood last week, as it works closely in conjunction with lambda functions.)

Basically, the map function takes all the items in an list (such as someList) and applies another function to them. The "lambda" there signifies that I'm defining a function, and the "x" means that it's only got one variable; the part after the colon, "x * x" means to take the input of the function and multiply it by itself (squaring it). So, "take each item in someList, and multiply it by itself". The print(number) part simply tells the computer to display the results of the operation on screen; it has nothing to do with printing on paper.

Anyway, it's really wonderful to be exercising the creative aspects of my brain in my job, and learning new things as I go along. A hui hou!

No comments:

Post a Comment

Think I said something interesting or insightful? Let me know what you thought! Or even just drop in and say "hi" once in a while - I always enjoy reading comments.