Archives

February 2010 (1)
September 2009 (1)
May 2009 (1)
April 2009 (1)
March 2009 (4)
January 2009 (3)

November 2008 (2)
October 2008 (2)
September 2008 (1)
August 2008 (5)
July 2008 (3)
June 2008 (1)
May 2008 (5)
April 2008 (8)
March 2008 (3)
February 2008 (1)
January 2008 (2)

December 2007 (2)
November 2007 (4)
October 2007 (17)
September 2007 (9)

Using Firebug’s console without fear

Thursday, February 04 2010

I love Firebug, and I especially love sprinking console.log calls around the place, but I hate the way things break when you forget to remove them and your code hits a browser without Firebug installed.

    if (typeof console === 'undefined') {
        var console = {}; // For once not having block scoping is kind of handy
        console.log = function () {};
    };
    
    console.log("It's safe to use console.log");

I feel there ought to be a smarter way of doing this but I just can’t think of one.

no comments

Tags: javascript ~ firebug ~ catalyst

Becoming lazy, strict and functional again

Friday, September 11 2009

Functional programming is the new hotness: functions as first class citizens, closures, and map/filter/reduce & co seem to be infiltrating the imperative languages rapidly.

Of course FP has been the new hotness for a long time. For example, I remember being taught Gofer, a forerunner of today’s Haskell, in the mid-90s by the long-suffering Steve Reeves. My brain had already been corrupted by BASIC and Pascal, so I didn’t really appreciate what was so great about it. I do clearly remember memorising the mantras that side effects are evil, and that real programming can be done effectively with a pencil and paper.

Something must have stuck because I have over time adopted a style where I avoid side-effects, declare as few variables as possible, avoid re-assigning to variables, decompose big procedures into tiny little ones, and use vectors and dictionaries a lot – all habits supported by my favourite scripting languages. Perl, Python and Tcl all support an unsophisticated functional style and once you learn them well you yearn for features like map and filter in more imperative languages.

In the last couple of years I’ve become vaguely interested in Haskell, and bought some books, but never really did anything with it. But in the last month or so I have decided to really try and come to grips with it. Partly I think because I’m writing a lot of Perl, and its untyped slipperiness is troubling me; partly because I’m writing a lot of Python, and its Haskelly list comprehensions are lovely; and partly because we have a lump of useful Haskell code at work which I just don’t understand AT ALL, and I want to.

So yesterday I finished my first successful functional language program in about 15 years. It does something useful for me: it works out the optimal progression of weights in a weight training cycle. I have enjoyed the mental exercise considerably as I try to do things the Haskell way rather than just translating an imperative solution into the nearest equivalent. I doubt that I’ve succeeded in being Haskelly, but these things don’t happen overnight.

The only thing that’s really annoyed me so far is discovering that the primitive operation to join a list of strings, which every other language I know calls “join”, is known in Haskell as “intercalate”. (OK, intercalate is a more general function on lists of lists, but still).

import System.Environment (getArgs, getProgName)
import Data.List

main = do
    args <- getArgs
    progName <- getProgName
    case args of
        [goal, workouts, weights] -> putStrLn (message goal workouts weights)
        _ -> putStrLn (“Usage: ” ++ progName ++ ” goal workout weights”)
       
message goal workouts weights = show (workoutCycle g wo wg)
    where g  = read goal :: Float
          wo = read workouts :: Float
          wg = map read (words weights)::[Float]
       

– subsequences is not in Ubuntu Jaunty’s version of GHC, unfortunately.
subsequences xs = [] : subsequences’ xs
       where subsequences’ []     = []
             subsequences’ (x:xs) = [x] : concatMap (\ys -> [ys, x:ys])
                                                    (subsequences’ xs)

steps start end workouts = let step = ((end-start)/(workouts-1))
                           in [start, start + step .. end]

threshold workouts goal increment = start >= 0.60 && start <= 0.8
                         where start = (goal – increment * workouts) / goal

bestIncrement workouts goal increments =  case find (threshold workouts goal) increments of
                                          Just increment -> increment
                                          Nothing -> head increments

bestStart workouts goal increments = case find (threshold workouts goal) increments of
                                          Just increment -> goal – (workouts * increment)
                                          Nothing -> goal * 0.7

roundToIncrement d n = let raw   = n / d
                           low   = fromInteger(floor (raw) ) * d
                           high  = low + d
                           mid   = d / 2 + low
                       in if n <= mid
                          then low
                          else high

workoutCycle goal workouts weights = map (roundToIncrement increment) (steps start goal workouts)
                        where increment  = bestIncrement workouts goal increments
                              start      = bestStart  workouts goal increments
                              increments = tail (map (foldl (+) 0) (subsequences weights ))

no comments

Tags: haskell ~ musing ~ functional programming ~ catalyst

Helpful features that aren’t

Wednesday, March 25 2009

Dear WhitePages,

Your “suggested locations” dropdown and region searching are broken.

Eg, I want to find “City GPs” in Wellington. As I type “Wellington”, the first suggested location is “Wellington Central”. Aha! I think. They are indeed in Central Wellington.

However, this search yields no results. Neither does “Wellington City” or “Wellington CBD”, even though they are at the city end of Willis St and definitely would be in both those regions. Only plain “Wellington” gives me a result.

To add insult to injury, the suggestion when there are no results is to “refine my search”. But “Wellington CBD” IS more refined than “Wellington”.

At this point, your suggested locations feature is actually more useless and more annoying than if it didn’t exist at all. Either get rid of it, or make your subcategories work as expected.

Yours sincerely

Stephen

1 comment

Tags: usability ~ catalyst ~ misfeature

Physician, heal thyself

Sunday, March 22 2009

The other day I was reading Ryan Tomayko’s blog and I got inspired.

Ryan wrote the Kid templating library which drives this blog, and is quite the Python/Ruby hacker. He also has a very minimalist design. Its principles are outlined here.

With hypertext, the information itself is the interface. The content takes center stage while the chrome and tool areas are placed in the back-seat. This inversion of priorities has created as big a leap in interface innovation as the first graphical user interfaces did to the terminal based applications before them.

And yet, these fine attributes of hypertext are regularly subverted. Since the web’s inception and subsequent boom, people have been trying to get around hypertext’s “limitations” as an interface medium: first with Java Applets and Active X controls, later with Flash sites, and today with Rich Internet Application (RIA) platforms. There was a time when sites were authored with the goal of preventing the vertical scroll-bar from ever appearing! The goal is always the same: invert the web’s superior content-oriented interface back to the GUI era and allow for the types of administrative debris so common and accepted in desktop applications.

I have applied them over on my other channel. (I also made a bunch of other improvements, like per-tag RSS feeds, and better 404 handling.)

I often have rude things to say about other people’s usability, so it feels good to get my own house in order. I am interested though in whether there such a thing as best practice design for blogs. For example, are “recent comments” widgets useful? Should you have whole articles rather than excerpts on your home page, and if so, how many? I don’t know, but I’d like to.

Naturally, this blog is still untouched and looks like pus; in fact owing to changes made for the other channel, it’s worse than before. This will not be the case for long.

no comments

Tags: usability ~ burble ~ ryan tomayko ~ catalyst

Recent comments

Rendered at 2010-03-14 17:55:19