Thursday, November 15, 2012

Oh wow, Functional Programming in Scala is over just like that!

On the last assignment of Progfun, I got sidetracked by some assertions in the discussion group about implementation details that weren't generally true, and hovered very close to the solution for far too long — long enough to start worrying about how I would finish by the deadline and make meaningful progress on some work for hire and catch up on another class that started last week when I was in Germany.

This a.m. after a slight change I decided to submit it to the grader (while at the time some testcases that only served to check someone else's implementation details were failing), and only a trivial style nit and handling of a simple edge case needed to be fixed. That was quick work to resolve, and now it is all over.

Overall I had one var (i.e., one bit of obviously imperative code) across six assignments for a demerit of 0.02 out of 60.00. If only...

Wednesday, November 7, 2012

Not a fanbois, but...

I bought an iPad mini on the first day. I like it. I don't care how bad everyone says it is.

I quickly discovered an app for the iPad called Textastic, which makes it easy to edit files accessible via Dropbox (or a couple of other mechanisms). I particularly like the cursor navigation wheel and the special symbol and number keys above the normal keyboard which give easy access to 45 or so more keys without cycling through keyboard layouts. It is a steal at $8.99. My Thinkpad has neither the battery life nor the size to allow for practical editing of files on the plane for more than a short time, so this app was a lifesaver on a recent trip.

My AC2012 EU slides

Monday, November 5, 2012

App App App App


function get_key($pi) {
    $terms = strlen($pi) != 0 ? $pi : 'Monday';
    $key = end(explode('/', $terms));
    return $key;

$key = get_key($_SERVER['PATH_INFO']);
$mckey = 'demowebapp.' . $key;

$mc = new Memcache;
$mc->connect('', 11211);
$val = $mc->get($mckey);
if (!$val) {
    $pgconn = pg_connect("host= dbname=demowebapp");
    $res = pg_query($pgconn, "SELECT * from demowebapp_x WHERE id = '$key';");
    $row = pg_fetch_row($res);
    $val = $row[1];
    $mc->set($mckey, $val, 0, 1);

print "$val\n";

use strict;
use DBI;
use Cache::Memcached;

sub get_key {
    my $pi = shift;
    my @terms = split(/\//, $pi || "Monday");
    return $terms[-1];

my $app = sub {
    my $env = shift;

    my $key = get_key($env->{'PATH_INFO'});
    my $mckey = 'demowebapp.' . $key;

    my $mc = new Cache::Memcached({'servers' => ['']});
    my $val = $mc->get($mckey);
    if (!$val) {
        my $dbh = DBI->connect('DBI:Pg:dbname=demowebapp;host=');
        my $sth = $dbh->prepare("SELECT * FROM demowebapp_x WHERE id = '$key';");
        ($key, $val) = $sth->fetchrow_array();
        $mc->set($mckey, $val, 1);

    return ['200', ['Content-Type' => 'text/html'], [$val]];

import psycopg2
import memcache

def get_key(pi):
    terms = [token for token in pi.split('/') if token != '']
    if terms:
        return terms[-1]
    return 'Monday'

def application(environ, start_response):
    start_response('200 OK', [('Content-type', 'text/html')])
    key = get_key(environ['PATH_INFO'])
    mckey = 'demowebapp.' + key
    mc = memcache.Client([''])
    val = mc.get(mckey)
    if not val:
        pg = psycopg2.connect(database='demowebapp', host='')
        csr = pg.cursor()
        csr.execute("SELECT * FROM demowebapp_x WHERE id = '%s';" % key)
        val = csr.fetchone()[1]
        mc.set(mckey, val, time=1)
    return [val]

class Lookup
    def get_key(pi)
        terms = pi != nil ? pi : 'Monday'

    def call env
        key = get_key(env['PATH_INFO'])
        mckey = 'demowebapp.' + key

        mc ='')
            val = mc.get mckey, false
            val = nil
        if not val
            pgconn = => 'demowebapp', :host => '')
            res = pgconn.exec("SELECT * from demowebapp_x WHERE id = '#{key}';")
            val = res[0]['content']
            mc.set mckey, val, 1, false

        [200, {'Content-Type' => 'text/html'}, [val]]