Thursday, February 19, 2009

Source Code Highlighting - In Blogger!

There are a great many posts on how to post syntax highlighted code in blogger. I can't really understand why google doesn't have a ready made solution in place.

I think I've found a solution I'm comfortable with:
  1. Convert the code to html (":TOhtml" in vi)
  2. In the Compose tab, type: <pre> </pre>
  3. Paste you code between the tags
  4. Publish
That's it - no installation, not too much hassle.

For extra credit, you can modify the css for <pre> sections.

If you want line numbers:
  1. ":set nu" - show line numbers in vi
  2. ":TOhtml" - convert to html
  3. ":set nonu" - remove line numbers for easy copy & paste (or, use Ctrl-V)
1  #include <stdio.h>
2
3 int main( int argc, char *argv[] ) {
4 printf("Hello World!\n");
5 return 0;
6 }
All that's left is to find a good color scheme for vi.

edit: Ars Pythonica has added that in your .vimrc file, you can add: "let html_use_css = 1" to include css in the output.

Thanks to:
  1. Eli Bendersky - for the pre trick
  2. FluidBlog - For the vi TOHtml trick

Project Euler

I've just stumbled upon Project Euler. It's a nice little site with mathematical puzzles to be solved using a computer. For example, this first problem is to find the sum of all the natural numbers who are multiples of either 3 or 5, and are below 1000.

There are hundreds of such puzzles, most of them more challenging, so I'll allow myself to post the answer to this first problem:
#!/usr/local/bin/python
sum = 0
for number in range(1, 1000):
if number % 3 == 0 or number % 5 == 0:
sum += number
print sum

And what is great is that once you solve a problem, you get access to a pdf file with an overview of the problem and efficient solutions. The above solution I wrote, which is quite naive, can be computed in O(1), had I first used a pencil and paper as my IDE, and not Vi. Can you see how?

Tuesday, February 17, 2009

Programming with Vi

This page will serve as a place to store my notes on using vi as a programming editor. And when I mean vi, I really mean vim, but these days I think it's pretty safe to assume vi=vim. I'm placing these notes on the web in the hopes that it may help other programmers new to vi, as I currently am. I will update this page with information I find useful.

The Two Modes
The most important concept to understand about vi is that it has two separate modes - command mode and insertion mode. This originally struck me as rather cumbersome, and I found my self working nearly completely in insertion mode. You can get along like this. You can also write C++ code in Microsoft Word. There's a really good reason for this separation - because command mode is separate, the commands are much simpler. For example, navigating around the document is really easy:

Navigation
j - Move one row up
k - Move one row down
h - Move one column left
l - Move one column right
w - Move to the start of the next word
b - Move to the start of the previous word
e - Move to the end of the next word
0 - Move to the beginning of the line
$ - Move to the end of the line
1G - Move to the beginning of the file
G - Move to the end of the file
% - Move cursor to matching bracket

The jkhl commands may seem quite stupid. There are arrow keys after all. But if you know how to touch type, you'll find that this really improves your productivity. It takes a bit getting used to, but it really pays off, in my opinion.

Like many commands, you can add a number before a navigation, so that 4j moves you four rows down.

Undo & Redo

u - undo last change
Ctrl+R - redo

Copy & Paste

v - enter visual selection mode. Moving the cursor will select text for later actions, such as:
y - yanks the selected text. aka, copy
yy - yanks the current line
p - paste text

Multiple Windows
:split filename - split a window and open filename
:vsplit filename - split a window and open filename

Running Shell Commands
:!make - executes a shell command, such as make
:r !ls - executes a shell command and pastes the output into the file

Search & Replace
/expression - Searches for expression
:%s/expression/newtext/g - Searches for expression and replaces it with newtext

Configuring The Environment
:set nu - shows the line numbers
:set nonu - don't show the line numbers
:colorscheme - changes the color scheme
:set sw=4 - sets the tab size to be 4 spaces

All these commands can be placed in ~/.vimrc

References
  1. Yolinux is always a good source
  2. Run vimtutor for a good hands-on tutorial

Saturday, February 14, 2009

Current Reading List

This page contains books I'm currently reading and the books I recommend, though I will probably not update it too diligently (last update-30'th of December, 2011). I have also stopped buying the dead-tree variety of books, and read on my iPad. Finally, note that reading is not enough, you also have to do things.

Current Reading List
Release It / Michael T. Nygard


Books I Recommend

I still can't relay say that I finished this book. I still need to find a quite afternoon to finish it. But, it is an excellent book, there is much Zen in it.

I think it's a good book. I haven't read many self-help book, and I generally dislike the entire genre. The 7 habits, however, is a serious book that doesn't promise any shortcuts - it doesn't promise easy solutions to anything, and I like that. I disagree with a couple of things, but it's a good read.

This is book is recommended by many people, and now by myself as well. It's a great book about managing people who get payed to think. It's a great read even for regular programmers like myself. It's a small book with plenty of fun stories. Highly recommended.

Don't Make Me Think: A Common Sense Approach to Web Usability / Steve Krug
This is a book about designing web sites to be usable. It's a very quick read - large fonts, plenty of pictures. Nonetheless, I think it's worth the money. A day after finishing the book I happened to show my mom the blog of our honeymoon, and I immediately noticed the many flaws I made when designing the blog.

Kafka on the Shore / Haruki Murakami
This is the second book by Murakami-san I've read. Like the first one (The Wind-Up Bird Chronicle), it is quite surrealistic. I can say that I really liked it, even though I disagreed with many of the overly-simplistic pacifistic messages Murakami tried to convey. Next I need to read Norwegian Wood (done).

Tuesday, February 3, 2009

Learning Japanese

I started studying Japanese a couple of months ago. I decided to register for an intensive course at the local university. There were other places to go to, such as the Japanese embassy, private tutors, specialized companies or just learn it by myself. I'm not too sure why I chose to study in a university.

Ages ago (2003), when I was contemplating what to study, I narrowed it down to three options - Physics, Computer-Science and Japanese (as part of a degree in Far-East Studies). And then I decided the choice was too hard, so I'd take two of the three. The final choice was to study Physics and CS, with the hope of squeezing a course in Japanese somewhere in between. I can't say I'm sorry, even though I never did manage to take that Japanese course.

After finishing my degree, it was only logical that I finally study Japanese. And I guess I decided to do that in the university because that was my frame of mind back then. I did consider the other options, but either they were too expensive, or they were not rigorous enough for my taste. In any case, now that the semester is nearly over, I can say I'm quite please with my choice.

I must say that the learning Japanese is quite different from learning Physics. The entire atmosphere is different. For example, classes are much more interactive, but that may be due to the fact that few people want to study Japanese, but a great many want to study Computer Science (for $ome $trange rea$on). In any case, the biggest difference is that there's a lot of memorizing required when learning a new language. Except some courses in mathematics, I really didn't have to do much memorizing during my studies in the exact sciences.

I've always had a love-hate relationship with memorization. My teachers in high-school kept on saying that it's important to understand concepts, and not to memorize them, and I've spent most of my life agreeing with them. I no longer do. I think that memorization is an important step in the path to understanding. It is also important in itself. For example, any physicist should remember what Planck's constant equals, as well as the value of Boltzmann's constant. Remembering important equations is also very important, because it allows you to solve various problems without breaking your chain of thought by opening a book (or Wikipedia).

My father knows pretty much the entire Hebrew bible by heart. He's not religious, far from it. It's just that his teachers didn't have any problems with forcing people to memorize passages. And I think his teachers had a good point. My teachers also had a good point, but that's how life is.

So, back to Japanese. I've spent the last couple of months having to memorize stuff for the first time in my life (at least to such an extent). I wasn't prepared for it. I tried various techniques. At first, I wrote each new word in a tiny notebook which I intended to take with me everywhere. That didn't workout, mostly because I didn't take it with me everywhere I went (it had a spiral binding, quite uncomfortable in the pocket). I still think it's a good enough idea in most cases, and it certainly works out quite well for my brother, who gave me the idea. My current invention goes something like this:
  1. Buy a bunch of cards
  2. Write the English/Hebrew term on one side.
  3. Write the Japanese translation on the other side.
  4. Add some useful information (for verbs, for example, if it's いちだん or ごだん)
Yes, it may very well be the breakthrough in language study that millions of people have been waiting for. Oh, wait. Someone thought of it before me. Damn.

Both of the methods I tried are okay, but that's pretty much all I can say about them. Of course, I don't rely solely on them. I still think that the best way to learn is by doing, and, as a corollary, the best way to learn to read is by writing. So I write a lot, which is important in Japanese because their alphabet is a mess. A beautiful mess, but a mess none-the-less. The bottom line, however, is that all these methods fall under the category of "memorize by repetition". It's a good, proven method. It just takes up a lot of time, time which I simply don't have (when am I supposed to write that game in Python?).

There has to be a better way. I've started looking into the various tricks of memorization. In high-school, we once got a visit from some sales-person for a private course in improving learning skills, or something like it. She taught us how to remember all the different offices in the government in a couple of minutes. She did this by telling us a story, and I still remember it more than 10 years later. It's based on a simple trick, that is the basis of most memorization techniques.

The basic concept is that we have an associative memory, and we remember the extraordinary. So, to remember a list of things, you just make up some bizarre story that connects the various items. The more extraordinary, the better, especially if it involves as many senses as possible. There are many methods that take this basic idea much further, but I don't know enough about them right now. It's one of the things I've decided I want to learn (yes, yet another one).

And yet, I'm not sure these techniques are good enough for Japanese. There are thousands of "letters", each with several different pronunciations, depending on the context. Most letters have a dozen or more strokes to remember. I just don't know of a better way to remember them than by endless repetition.

So, although it's been a fun post, it's one of the longer ones I've written, and I have to go memorize some kanjis (料利 is especially annoying).

I also have to improve my English writing skills sometime.

Further Reading
  1. Learning Japanese - Memorization: includes many new lessons I've learned since writing this article.