A notch above a monkey

Dreaming about arithmetic mean

I had a crazy dream, where I was back at University, but this time studying computer science. We were writing a function to calculate an arithmetic mean of an array of numbers. A task obviously too simple for college course, but with dreams you get what you are given.

Discussion started with this function:

  1. function average(arr) {
  2. var i, sum = 0;
  3. for(i=0;i<arr.length;i++) {
  4. sum += arr[i];
  5. }
  6. return sum/i;
  7. }
  8. Download this code: /code/dreamjs.txt

Dream me, who by the way is significantly more bitchy than I ever am, wasn’t pleased and thought he could save few bytes by storing counter and sum inside of the array:

  1. function average(arr) {
  2. // Store counter in array[0] and sum in array[1]
  3. if (arr.length < 3) {
  4. return arr.length == 2 ? arr[0]+arr[1] : arr[0];
  5. } else {
  6. arr[1] = arr[0]+arr[1];
  7. arr[0] = 2;
  8. for(;arr[0]<arr.length;arr[0]++) {
  9. arr[1] += arr[arr[0]];
  10. }
  11. }
  12. return arr[1]/arr[0];
  13. }
  14. Download this code: /code/dreamjs2.txt

This looked ugly, but what bothered him (me?) was more that it also had a bug. Integer in Javascript is limited to 2 53 , which is a lot, but sum can still overflow or underflow it. Since mean can never be smaller than smallest or bigger than biggest number in a list, I could fix it by writing:

  1. function average(arr) {
  2. var n = arr.length, sum=0;
  3. while (arr.length) {
  4. sum += arr.pop()/n;
  5. }
  6. return sum;
  7. }
  8. Download this code: /code/dreamjs3.txt

End of dream. Everything edited for sanity and brevity.

I don’t have much to say about second program except that I would never write something so ugly just to save 16 bytes in a function like this. I wouldn’t even use Javascript if memory was that important.

I am more intrigued by third program. Bug in second is definitely there, even though few of us operate with numbers big enough to encounter it.

It’s interesting, because if I was awake, I would probably never think of it. I used to worry about bugs like this all the time, when I was coding in C. It was an unavoidable consequence of the language.

I guess what I am getting at is that it is nice to code in a high-level language and for the most part not think about implementation details like this, but only as long as you actually know them so your brain gets triggered when they matter. Speaking of details, I bet those divisions can cause rounding errors.

Books I read in 2009

Last year I read Aaron Swartz’s review of books and thought it was a great idea that I should copy. I don’t know why exactly. Maybe to better track how much I actually read. Or using a goal to promote more reading when I felt I haven’t read enough. Maybe it was just to learn what topics drew me particular year.

Who knows? Probably all of these reasons played a role.

My private goal was to read a book every fortnight, so about 26 in total. I read 37, some of which admittedly very light in content or girth.

Time to present the books, listed in chronological order. I linked those I liked and set in bold those I recommend. Unlinked were either bad or intended for someone I can’t relate to.

DISCLAIMER: Most links point to Amazon and include my affiliate ID meaning if you buy them after following these links, I get few cents that might eventually lead to purchase of another book.

  1. Mind Performance Hacks by Ron Hale-Evans. Surprised to see how many of them I discovered independently through years.
  2. The Cold War by John Lewis Gaddis . Fantastic short history of cold war.
  3. The Non-Designer’s Design Book by Robin Williams
  4. The Non-Designer’s Type Book by Robin Williams. Like previous one probably useful only if you know absolutely nothing about design.
  5. Business Cards 2 More Ways of Saying Hello by Michael Dorrian, Liz Farrelly. Worth a look before you make your own.
  6. Here Comes Everybody by Clay Shirky. Astute and interesting, but might be redundant if you already follow Clay’s writing.
  7. Guns, Germs and Steel by Jared Diamond.
  8. Sticky Graphics: Create Memorable Graphic Design Using Mnemonics and Visual Hooks by Jessica Glaser, Carolyn Knight. WTF?
  9. Thursday Next: First Among Sequels by Jasper Fforde. Fantasy series for book lovers. Enjoyed more if you’ve read a lot.
  10. Designing Web Interfaces by Bill Scott, Theresa Neil. Components of modern web applications. A book that should be on the shelf of every web interaction designer.
  11. Reader by Bernhard Schlink . Excellent book, haven’t seen the movie.
  12. The Wall Jumper by Peter Schneider . Timeless portrait of German separation.
  13. Everyware: The Dawning Age of Ubiquitous Computing by Adam Greenfield. Really late to the party with this one, but still a good and fairly unbiased treatise on intelligent tools and environments and problems related to them.
  14. Always Building: The Programmable Environment by Jim Long, Jennifer Magnolfi, Lois Maassen. Optimistic thinking and examples of adaptable environments by people from Herman Miller.
  15. Javascript: The Good Parts by Douglas Crockford. Concise but informative description of good parts of Javascript useful for most programmers.
  16. Stardust by Neil Gaiman.
  17. Ender’s Game by Orson Scott Card . As good as people say.
  18. Small Gods by Terry Pratchett . In my opinion still the best book by Terry.
  19. A Theory of Fun for Game Design by Raph Koster. Worth reading if you want to know what makes games fun and what to expect in future.
  20. Aid and Other Dirty Business: How Good Intentions Have Failed the World’s Poor by Giles Bolton . Excellent when looking at facts of aid “business” (first 4/5 of the book). Weak when it comes to solutions with seemingly unlimited faith in globalism and power of a consumer.
  21. The Caryatids by Bruce Sterling. Interesting and occasionally excellent, but a bit of a let down compared to some of his other work.
  22. Sketching User Experiences: Getting the Design Right and the Right Design by Bill Buxton. Excellent book about sketching and its role in design. A must read for practitioners.
  23. The Buddha: A Very Short Introduction by Michael Carrithers. Buddism seen through life of the Buddha.
  24. Archaeology: A Very Short Introduction by Paul Bahn. Fun.
  25. The Four Steps to Epiphany by Steven Gary Blank . I can’t recommend this book highly enough to anyone starting a new venture. Absolutely a must read.
  26. The First World War: A Very Short Introduction by Michael Howard. Short, but good.
  27. Everyman by Philip Roth. Facing old age and death, a book I liked more than my wife.
  28. Contemporary Art: A Very Short Introduction by Julian Stallabrass. How contemporary art (world) functions and less what it is.
  29. Judaism: A Very Short Introduction by Norman Solomon.
  30. By Night in Chile by Roberto Bolano . Discovered by method of pulling random books from shelves in book store. Absolutely superb.
  31. Against the Gods: The Remarkable Story of Risk by Peter L. Bernstein . A book about history of risk management made only more relevant by last financial crisis. Fun and very informative.
  32. Forget Kathmandu: An Elegy for Democracy by Manjushree Thapa. Modern history of Nepal told through personal experience. Recommended if you are interested in Nepal.
  33. The Life and Opinions of Tristram Shandy, Gentleman by Lawrence Sterne . Hard to believe that 250 years old novel can feel so modern.
  34. Unseen Academicals by Terry Pratchett . I might be biased, but I would put it among his best novels.
  35. Prototyping: A Practitioner’s Guide by Todd Zaki Warfel. I never got around writing review of this book. Definitely worth a read for practitioners.
  36. Getting Started with Arduino by Massimo Banzi. Aimed at designers, but learned few new things anyway.
  37. The City: A Global History by Joel Kotkin. Sort of a short introduction to history of cities with everything good and bad that format entails. Excellent starting point for those interested in its subject.

I wish I read as much as Aaron ( latest review ), who seems to devour about hundred books per year, but I don’t think I’ll ever manage that. My goal for this year is going to be about same, 25 books before bells toll midnight on 31st of December. It still is an ambitious goal, since these days I seem to have a penchant for buying tomes at least 600 page thick and want to read more of other people’s code.

2009 review

Comment (1)

  1. [...] this is not a travel post. A year ago I wrote my yearly review and it’s time to write a new one. Looking back at this year I feel disappointed. It [...]

Sorry, the comment form is closed at this time.