Learning Functional Programming, Part One

Part One: Lambdas? In my Python?

Over the past few months, I’ve decided to take a stab at learning some functional programming. I’d been doing python for a few years (and completely falling in love with it), and so I’d been exposed to a few functional concepts it offers – primarily higher-order functions and list comprehensions, both of which allow for very clear, concise and powerful code. Since that’s where I started my journey, that’s where my post will begin as well.

Functions are objects, too

Having graduated to python from PHP and C/C++, perhaps the biggest new thing to wrap my head around (besides readable code, whitespace-as-syntax, programming being fun again, and all that), is that in python, functions (and classes!) are objects, just like anything else. They can still be defined in the usual way, but they can also be assigned, passed as arguments, even modified and replaced like any other value or object in your program.

def do_a():
    print "Doing something"
 
do_b = do_a
 
do_b()
 
# Prints "Doing something"

Functions themselves no longer require formal definitions, either, they can be created anonymously:

my_send = lambda person, thing: send(person.upper(), thing, subject="Check this out!")
ucase_people = map(lambda name: name.upper(), ["Joe", "Mary", "Zach"])

Abstracting behaviour

You’ll find you can now start abstracting away common idioms. For example, you probably very often find yourself looping over some list of items, performing some set of actions on them, or passing them to some other function or method:

people = ["Joe", "Chris", "Matt", "Jennifer"]
for person in people:
    u_person = person.upper()
    send(person, super_fun_thing)

Instead of that, you could have a function that takes a list as one argument, and a function to apply to each item in it as another:

def dostuff(action, things):
    result = []
    for thing in things:
        result.append(action(thing))
    return result
 
dostuff(send, people)

The above example is actually just a simple definition of one of the most common higher-order functions, map, which python already provides for you. Another particularly useful higher-order function is filter which, given a function that returns true of false if its criteria are met by the passed item, will return the subset of the passed list that satisfy the filtering function:

stuff = ["My notes.txt", "Matt's notes.txt",  "My music.pls"]
my_stuff = filter(lambda s: s.startswith("My "), stuff)
 
# my_stuff = ["My notes.txt", "My music.pls"]

List comprehensions provide a cleaner, easier to read way to perform mapping and/or filtering on a list:

stuff = ["My notes.txt", "Matt's notes.txt",  "My music.pls"]
 
my_stuff = [file for file in stuff if file.startswith("My ")]
# ["My notes.txt", "My music.pls"]
 
upper_stuff = [file.upper() for file in stuff]
# ["MY NOTES.TXT", "MATT'S NOTES.TXT", "MY MUSIC.PLS"]
 
music = [file.upper() for file in stuff if file.endswith(".pls")]
# ["MY MUSIC.PLS"]

Tip of the iceberg

This is just a very small taste of functional programming concepts. Later, I’ll introduce a couple of functional languages, and explain what sets them apart from object-oriented and imperative programming languages.

Transmission, RSS and XBMC

I’m a huge fan of XBMC. My pc (currently running Ubuntu 10.04) has taken root in my living room, piping all my movies and tv shows straight to my HDTV.

While my pc is set up as a DVR using MythTV to record shows off my FIOS box, it tends to be a little unreliable, which can suck when it’s time to catch up on Daily Show and Colbert episodes. I’ve had Transmission set up for a while for all my torrenting needs, and I’ve even written an XBMC script to manage torrents, so I got to looking for tools to track tv show torrent rss feeds.

Read more »

Potatoes and Portal Guns

Got my hands on Portal 2 and finished a run through the single player campaign. Was a lot of fun, the characters were bursting with humor and personality. Just like the first game, it was hard to stop playing. Unlike the first game, it’s got some length, so I stayed up late a couple nights with my eyes glued to the television. I already want to play through it again to find any little things I my tired eyes may have missed.

I’m itching to give co-op a try, so if you happen to have it on xbox or care to drop by, let me know.

Update: Played some co-op with Jen, had fun navigating puzzles together :)

Meh.php

<?php
if (!defined('meh')) define('meh', null);
 
class Meh {
  public function __set($name, $value) {
  }
  public function __get($name) {
    return meh;
  }
  public function __isset($name) {
    return true || false;
  }
  public function __unset($name) {
  }
  public function __call($name, $arguments) {
    return meh;
  }
  public function __callStatic($name, $arguments) {
    return meh;
  }
}
 
$bwuh = new Meh();
$bwuh->give_a_shit();
echo $bwuh->concerns;
 
 
class SuperDuperBillingProcessor extends Meh {}
 
$p = new SuperDuperBillingProcessor();
$p->calculateEverything();
$p->profit();