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


<?php

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('192.168.11.199', 11211);
$val = $mc->get($mckey);
if (!$val) {
    $pgconn = pg_connect("host=192.168.11.199 dbname=demowebapp");
    $res = pg_query($pgconn, "SELECT * from demowebapp_x WHERE id = '$key';");
    $row = pg_fetch_row($res);
    $val = $row[1];
    pg_free_result($res);
    pg_close($pgconn);
    $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' => ['192.168.11.199:11211']});
    my $val = $mc->get($mckey);
    if (!$val) {
        my $dbh = DBI->connect('DBI:Pg:dbname=demowebapp;host=192.168.11.199');
        my $sth = $dbh->prepare("SELECT * FROM demowebapp_x WHERE id = '$key';");
        $sth->execute();
        ($key, $val) = $sth->fetchrow_array();
        $sth->finish();
        $dbh->disconnect();
        $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(['192.168.11.199:11211'])
    val = mc.get(mckey)
    if not val:
        pg = psycopg2.connect(database='demowebapp', host='192.168.11.199')
        csr = pg.cursor()
        csr.execute("SELECT * FROM demowebapp_x WHERE id = '%s';" % key)
        val = csr.fetchone()[1]
        csr.close()
        pg.close()
        mc.set(mckey, val, time=1)
    return [val]

class Lookup
    def get_key(pi)
        terms = pi != nil ? pi : 'Monday'
        terms.split('/')[-1]
    end

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

        mc = Memcached.new('192.168.11.199:11211')
        begin
            val = mc.get mckey, false
        rescue
            val = nil
        end
        if not val
            pgconn = PGconn.open(:dbname => 'demowebapp', :host => '192.168.11.199')
            res = pgconn.exec("SELECT * from demowebapp_x WHERE id = '#{key}';")
            val = res[0]['content']
            res.clear
            pgconn.finish
            mc.set mckey, val, 1, false
        end

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