Archive for November, 2007

Pretty colors: Syntax Highlighting in Mercurial

Sunday, November 18th, 2007

I recently switched my personal revision control system from subversion to mercurial. One of the great things about mercurial is the built-in web interface, but I missed the syntax highlighting that’s available in interfaces such as ViewVC.

I’ve written a mercurial extension that applies pygments code highlighting. This extension is now available in the main mercurial repo.

To enable it, install pygments and add the following entry to hgrc:

[extension]
hgext.highlight =

An example of the output.

Thanks to micha who wrote an initial patch. black jack betting strategyfree online video pokerbackgammon gambling,online backgammon gamblinghow to play video pokervideo poker practicefree baccarat game,baccarat game,baccarat casino gametournament backgammoninternet casino gamefull pay video pokerno deposit bonus online casinofree slots,free slots game,free on line slotsbaccarat rulefree black jack gamecraps online gamevirtual casino gamblingfree video poker downloadsblackjack gambling game,blackjack gambling,online blackjack gamblingblackjack casino gameno deposit free money casinocasino link online suggestreal money backgammonplay free casino slotshow to play backgammon,instructions to play backgammon,play backgammon online freevideo poker tournamentfree on line slotsblack jack downloadon line casino wageringfree cash casinoplay free online slotslearn to play crapsdouble bonus video pokerbest online casino bonusinternet casino gambling game,internet casino gambling,internet casino gambling ukonline casino gambling,gambling casino online bonus,online casino online gamblingfree backgammon downloaddownload casino gamevideo poker doublejacks or better video pokerno download video pokeronline black jack gameonline casino gambling sitebaccarat the internet casino game,casino baccarat,virtual online casino gambling baccaratfree online black jack gameonline baccarathow to win at roulettedeuces wild video pokerfind online casinole casinoblack jack regolenouveau casino bonus sans depot

Python λ Shorthand

Saturday, November 10th, 2007

In Python a lambda expression (anonymous function) is created with the lambda keyword:

map(lambda x: x+1, [1,2,3])

Some Scheme interpreters such as Dr. Scheme allow the λ symbol (U+03BB) as a shorthand for lambda. I started wondering what this would look like in Python. For example:

map(λ x: x+1, [1,2,3])

Or for a slightly more gratuitously complex example, a recursive factorial with the Y combinator:

Y = (λ X:
         (λ p:
              X(λ arg: p(p)(arg)))
     ((λ p:
           X(λ arg: p(p)(arg)))))

F = (λ f: (λ x: (x*f(x-1) if x > 0 else 1)))

fact = Y(F)
fact(5) => 120

The interpreter changes to make this work this are quite simple. All it requires is small change to the grammar, an update to the syntax checker, and a hack in the parser generator to treat characters with the high-bit set as though they were in the alpha class.

There are a few cases where the shorthand is beneficial. For example, compare:

[i for i in lst if i > 42]

to:

filter(λ x: x > 42, lst)

While list comprehensions are the standard python way to accomplish this, I do like how the predicate comes first in the latter version.

Also, pretty much all of the functions in the itertools module could work nicely with the shorthand form:

itertools.groupby(lst, λ x: x.somevalue)

Even so this is probably isn’t something that would be worth adding to the language. Lambda expressions are rarely useful in Python so a shorthand form is not going at add much benefit compared to the added language complexity. Not to mention the need for a UTF-8 aware editor and terminal.

Using It

Since λ probably doesn’t appear on your keyboard a certain amount of editor configuration is required. The easiest way in emacs is to define a key binding that inserts the character:

(global-set-key "\C-c\C-l" (lambda () (interactive) (insert "λ")) )

Alternatively, abbrev-mode can insert this character whenever the word lambda is typed:

(define-abbrev-table 'global-abbrev-table
  '(("lambda" "λ" nil 0)))

To build an interpreter with this enabled you’ll need this patch applied to a recentish checkout of python 3k. I don’t know if it would work on earlier versions.

Update: On reddit, gwern pointed out that it’s better to translate from lambda -> λ in the editor display rather than embed this symbol in the text. There is (of course) elisp to do this.

One more time: BK Trees in Haskell

Friday, November 2nd, 2007

After the Python BK-tree implementation I decided to try my hand at a Haskell version. Here it is.

videos of drunk girls peeing outside: free squirting video. free teen incest sex videos free incest hentai manga movies. free rape forced sex exploitation movie sites extreme rape porn movies.