Go Language and Functional Programming

There is a cool new language on the block, coming from The Google courtesy of (among others) Rob Pike and Ken Thompson. The language is called Go (and the debugger is called Ogle, so put ‘em together for a secret message) and it attempts to be both a low-level, type-safe, compiled language, like C, while being garbage-collected, concise, with high-level structures and idioms like Python and Javascript. There are a lot of things to like in it, and I am exploring what it can and cannot do. My first question was whether you could use it for functional programming, i.e., are functions first-class objects that can be passed to other functions, returned from functions, stored in variables, etc. This wasn’t in the FAQ or the Language Design FAQ, but there were some hints of it in the description of channels, so I went ahead and wrote my own tests to see if it would work.


package main

import fmt "fmt"

type Stringy func() string

func foo() string{
return "Stringy function"
}

func takesAFunction(foo Stringy){
fmt.Printf("takesAFunction: %v\n", foo())
}

func returnsAFunction()Stringy{
return func()string{
fmt.Printf("Inner stringy function\n");
return "bar" // have to return a string to be stringy
}
}

func main(){
takesAFunction(foo);
var f Stringy = returnsAFunction();
f();
var baz Stringy = func()string{
return "anonymous stringy\n"
};
fmt.Printf(baz());
}

This compiled and ran, giving the right output, so it looks to me like functional programming is indeed possible in Go, and on the Effective Go page we learn, “In Go, function literals are closures: the implementation makes sure the variables referred to by the function survive as long as they are active.” So that’s kind of cool.

Go also has rich, high-level concurrency support similar to Erlang, several features that prevent random memory from being read or written, and both strings and maps (dictionaries, hash tables) are built-in datatypes. It has a small, but powerful standard library.

One puzzle down, several to go. I still want to explore how I can talk to Cocoa from Go (for GUIs), how to wrap libraries such as libxml2 and cairo for Go, and basically how to map more Python/Javascript library goodness into Go’s world.

I have smaller questions too: can I add methods to existing types? How do I load the contents of a URL? It’s looking good so far! Tune in next time for more exploration.

Moped Syndrome

[This is an essay I wrote in 2002, found in an old journal]

What do you get when you cross a clock and a computer: a computer. An
airplane and computer: computer. A VCR and computer: computer. [This argument I believe was my summary of something Donald Norman said in The Design of Everyday Things.]

Of course, computer here is used as a code word for Something which is
perverse and complicated, which most computer *are*, but they don’t have to
be. It doesn’t imply that you can program your clock radio with Python, for
instance.

These are examples of moped compromises. I owned a moped once. It was too
heavy to pedal and too underpowered to go up hills. It combined all the
worst features of bicycles and motorcycles, with few of the advantages of
either.

These bad compromises are everywhere. When I studied to be an EMT, we learned
some Greek and Latin, a little CPR, but mostly we learned how to cover our
asses in case we were sued. By becoming professionals we would no longer be
covered by Good Samaritan laws, and the nature of the job (trying like hell to
save somebody’s life, often when it’s too late) meant lots of times the
patients died or lost a limb or whatever. Whenever the outcome is less than
optimal, the American knee-jerk reaction is to sue. So we inadvertently
became lawyers.

What do you get when you cross a paramedic with a lawyer: A paralegal.

Now I’m in computing and it seems that just writing code, pretty simple and
harmless code, not viruses or anything like that, can get you thrown in jail
or sued in all kinds of remarkable and unpredictable ways. In fact, you can
break the law without even knowing it, because what you’ve created is already
a secret. Add that to the awesome array of software licenses and we’re
inadvertently becoming lawyers again (or outlaws).

What do you get when you cross a computer scientist with a lawyer: A lawyer.

Aspect-oriented Python

A friend asked me for an example of AOP in Python. I started to write up my response, then realized it might be worth sharing more widely.

Briefly, AOP is about separating out Aspects which are are interspersed throughout your code (in AOP lingo, cross-cutting). The canonical example is logging, but there are others.

For Python, as long as you’re content with before/after aspects, the situation is good. In Python 2.5 and up there are two main tactics: decorators and context managers. Decorators use the @ syntax and context managers are used with the “with” keyword.

Decorators

Decorators are only usable on functions and methods in 2.4 and 2.5, but in 2.6, 3.0 and beyond they can be used on classes as well. Essentially they are callables (functions, methods, objects) that accept a function object and return a function object. They are called when the function is defined, so they get a chance to have their way with it: annotate it, replace it, or wrap it. The common case is to wrap it.

Quick example:

import logging
def before(fn):
    def wrapped(*args, **kws):
        logging.warn('about to call function %s' % fn.func_name)
        return fn(*args, **kws)
    return wrapped

def after(fn):
    def wrapped(*args, **kws):
        retVal = fn(*args, **kws)
        logging.warn('just returned from function %s' % fn.func_name)
        return retVal
    return wrapped

OK, those are three basic wrappers, you can use them like so:

@before
def foo():
    logging.warn('inside foo')

@after
def bar():
    logging.warn('inside bar')

@before
@after
def baz():
    logging.warn('inside baz')

foo()
bar()
baz()

This will result in the following output:

WARNING:root:about to call function foo
WARNING:root:inside foo
WARNING:root:inside bar
WARNING:root:just returned from function bar
WARNING:root:about to call function wrapped
WARNING:root:inside baz
WARNING:root:just returned from function baz

You will note that when we use two decorators on baz, the name of the function called by before is “wrapped.” This is because what before is called on is the result of after. The functools.update_wrapper function is useful in this case to make a wrapped function look more like the original function.

For more, please see PEP 318 Decorators for Functions and Methods: Examples and PEP 3129 Class Decorators. For convenience when creating new decorators, see the standard library functions functools.update_wrapper and functools.wraps.

Context Managers

Context Managers are used with the with statement, and are handy for resource aquisition and release. In Python 2.5 you have to “from __future__ import with_statement” to use them, but they are built-in in Python versions later than that. Also, objects such as files and locks are context managers now, so you can use patterns like

with open('example.txt') as example:
    for line in example:
        do_something(line)

This will automatically close the file when leaving the with block. And for locks the pattern is similar:

with myThreadingLock:
    do_something_threadsafely()

It is important to note that the lock will be release properly, or the file closed, even if an exception is thrown inside the with block.

If you want to create your own context managers, you can add two methods to your objects: __enter__(self) and __exit__(self, exception_type, exception_value, traceback). The return value from __enter__ will be passed to the optional as variable (seen in the file example). The __exit__ method will be called with exception info if there was an exception. If no exception is raised in the with block, then all three arguements will be None. If __exit__ returns True then any exception will be “swallowed”, otherwise the exception will be re-raised after any cleanup.

For more info, see PEP 343 The “with” Statement, especially the examples section, and also the helper functions in the standard library contextlib.

AOP and 80/20

Full-on aspect-oriented programming is beyond the scope of this post and involves join-points, code weaving, and other such arcanery. There are multiple Python libraries which target aspect-oriented coding styles, but for my money, the simplicity of the methods in the standard library, coupled with my impression that they cover at least 80% of the uses of AOP, make me favour these built-in techniques over any of the special purpose tools.

Balding for Dollars Update

balding_after.jpg

Well, my Balding for Dollars event was a success. I have raised $717.37 for Children’s Hospital (there is still time to help bring it to my goal of $1000 if you’d like to donate). Thanks to everyone who helped with donations, words of support, and especially to Frank for his photography, to Berenice for helping with the cutting, to my wife Daniela for her encouragement and help, and to our kids for their patience. I couldn’t have done it without all of you.

There has been one change of plans. I had wanted to donate the hair to a Canadian foundation making wigs for kids with cancer, but couldn’t find it. Daniela’s google-fu skills were better and tomorrow my hair will go off to the Eva and Co. hair donation program. And to clarify, my daughter didn’t shave her head, we’re sending a ponytail from when she went from a long hairstyle to a short one.

Thanks also, to everyone who offered me a toque, but I’m well covered.

Balding for dollars

balding_profile2.jpg

I usually use this site to try to give back to the community, with Python and Javascript tutorials, software, and the occasional joke. Today I’m going to ask for something in return. On Saturday, February 14th, 2009 (Valentine’s Day, at least in the US and Canada) I will be shaving my head to raise money for Vancouver Children’s Hospital as part of their Balding for Dollars drive.

The picture above was taken today by my friend Frank Lee. As you can see, my hair is quite long. I’ll be sending my ponytail to Locks of Love, who make a wigs for kids who have lost their hair due to medical conditions. This is my second balding for dollars and will be the fourth ponytail I’ve donated, and my daughter is also donating her ponytail. As I get older, my hair isn’t growing in like it used to. This may be my last good ponytail to give, so with your help, I’d like to make a difference.

You can make donations on the Balding for Dollars site here. Saturday is only two days away, so be my valentine and help some kids!

I appreciate any help you can give. Check back on Monday for before/after photos. Thank you!

« Previous entries Next Page » Next Page »

google

google

asus