• Home
  • About Me
  • Contact

kanorben.net - blog

My personal blog on technology, programming, life, and the random

 

October 2008
M T W T F S S
« Aug   Nov »
 12345
6789101112
13141516171819
20212223242526
2728293031  

Blogroll

  • Boing Boing
  • BorjaNet
  • Brian Mayer
  • Dean Armstrong’s Blog
  • Ellen Smith’s blog
  • Faraocious
  • Gross or Awesome?
  • Marcus Westin’s Blog
  • Nightmares of David Bowie’s Package
  • Paul Mantz’s Blog
  • Slashdot
  • Tomorrow with Alex Beinstein
  • Valleywag

Personal Sites

  • DOIT Fortune Database
  • My bookmark’s on del.icio.us
  • My CS account page
  • My Facebook Profile
  • My LinkedIn Page
  • My Picasa Albums
  • My Twitter
  • pyXSD
  • The SUCCESS Blog
  • UofC ACM Site

webcomics

  • Questionable Content
  • Saturday Morning Breakfast Cereal
  • The Perry Bible Fellowship
  • Welcome To The Future
  • xkcd

Meta

  • Register
  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org
Add to Google Add to My Yahoo! Subscribe with Bloglines
Bloggers' Rights at EFF

Twitter Updates

    RSS My Del.icio.us

    • Developers Aren't Gonna Read It | Agile Software Development
    • Code Like a Pythonista: Idiomatic Python
    • Samuel Adams Utopias | Larsblog
    • BASTILLE-LINUX
    • from Vim to Emacs - part 1
    • OpenMacGrid | MacResearch
    • Tutorial: Backups with Launchd | MacResearch

    RSS My Facebook Posted Items

    • nomin' on ribs
    • Chomsky says pick the lesser of two evils
    • Elite Officer Recalls Bin Laden Hunt, Delta Force Commander Says The Best Plan To Kill The Al Qaeda
    • Language Fail
    • Safety Fail

    Fun with One-liners

    October 23rd, 2008 by knorby

    I have always enjoyed putting as much work as possible into a line, especially in higher level languages. In the crazy javascript (speaking of which, MochiKit 1.4 was finally release!) system I wrote (still need to put the final touches on that….), I was thrilled when I was able to combine all of the parts of my system into one one line. Since I mainly program in python, list comprehensions and generator expressions make it pretty easy to use one liners a lot, and at least with generators, it often means that is efficent too. Basically, what I am trying to say is that I love the one liner. Last night, while I was bored while doing my discrete homework, I came up with a memoized factorial function one liner. The standard, niave version is straightforward:

    fac = lambda n: int(n==0) or n*fac(n-1)

    Since I was bored, I wanted to see if I could memoize that expression, and still keep it in one line. Here is what I came up with:

    fac_dict, fac = {}, lambda x: ((x in fac_dict or fac_dict.update({x:(lambda n: int(n==0) or fac(n-1)*n)(x)})) and False) or fac_dict[x]

    which can be simplified slightly to:

    fac_dict, fac = {0:1}, lambda x: ((x in fac_dict or fac_dict.update({x:(lambda n: fac(n-1)*n)(x)})) and False) or fac_dict[x]

    There are still issues with recursion depth for large values, but a helper function could probably solve that.

    I needed to write something for my blog, as I have gotten out of the habit, and this thing seemed as good as anything.

    Posted in Python, coding, javascript | No Comments

     
    Add to Technorati Favorites - Creative Commons License - © 2007 Karl Norby