Friday, November 27, 2009

What do Interpreted Programming Languages have in Common? Part I

Update, December 4, 2009: I took the two comments I received an incorporated the appropriate changes. If anyone else sees corrections that need to be made, please (politely) let me know. I've been really busy lately, but I'll try to write and publish Part II soon. Thanks.

Foreword: I'm writing this particular blog as much for my own education as for yours. I'm sure I've made a mistake or two here and there, and if so, I don't mind you pointing them out. I would appreciate it however, if you at least tried to be civil if not polite when making corrections. I want this to be a friendly atmosphere where education is encouraged, not fear of mistakes. Thanks.

One of the frustrating things about learning how to program is that you have to start somewhere. I know there's no way of avoiding this, but when you learn your first programming language, you are also learning the basic structure of how to program in general. It's easy to get mixed up at this stage and lost in the details of a particular language, losing sight of the overall goal. I've tried, but there are few, if any, guides available online that present the general structure of an interpreted language (as opposed to a compiled language which we won't be discussing here), without referencing one specific language. With that in mind, I thought I'd try to put together an outline of what interpreted languages seem to have in common as far as a basic structure. My specific examples will be JavaScript, Python, and Ruby, so you can see how different languages are expressed within this structure. But first, some basic definitions:

The Purpose of a Program: In general, any computer program is supposed to take a large problem, and solve it by breaking it down into small, bite-sized bits or steps.
Syntax: This is the rules about how to write in a particular programming language. While interpreted languages in general use the same "parts" or structure, how each language is expressed within that structure varies.

Statements: A statement in a program is like any other statement, such as a person making a verbal statement or writing a sentence in an email. In a program, a statement is s step that defines a particular action that's taken in the program. Statements are written, regardless of the language, in a way that the computer, but not necessarily people, can understand.

Statement one
Statement two

JavaScript Sample Code:

Python Sample Code:
bunnies = 3
while bunnies > 0:
    print bunnies
    bunnies -= 1

Ruby Sample Code:
input = ''
while input != 'bye'
   puts input
   input = gets.chomp
end
The statements in the previous examples don't quite do the same thing, but you can get the idea of how statements are similar and different across three separate languages. Comments: Since statements are always understandable by human beings, comments are often written in human-readable language so that people can understand what a statement is supposed to say and do. It's not only a note from to programmer to himself or herself, but to any other programmer who may later need to understand and perhaps modify the code. Using a while statement as an example, a statement in general looks like this:
JavaScript Sample Code:
Statement one; // This is the first statement
Python Sample Code:
Statement one #this is the first statement
In Python, you can also create a comment that spans multiple lines like this:
Statement one """this is a multi line comment
about the first statement"""
Ruby Sample Code:
Statement one #this is the first statement
There's also a way to make a comment that spans multiple lines in Ruby:
Statement one =begin
this is a multi line comment
about the first statement
=end
Variables: In a program, variables are used to store information, however as the name implies, the value of the information stored isn't static. It can be changed depending on the requirements of the program. Usually variables are assigned values as in the following examples.
JavaScript Sample Code:
var color = "blue";
Python Sample Code:
color = "blue"
Ruby Sample Code:
color = "blue"
Variables Types: Variables can be used to store different types of information. Depending on the programming language involved, the variable types may be either strictly typed, which means you must tell the program specifically, what type of data is to be stored, or loosely typed, which means the language doesn't really care what is contained in the variables. That said, you as the programmer must know what type of data you want stored in a variable. Variable types include:
  • Numbers
  • Strings
  • Arrays
  • Booleans
Numbers: Numbers are what you imagine them to be: 1, -2.2 3.14159265, and so on. Actually, there are two different ways of expressing a number within a program: Integers and Floats. Both Integers and Floats can be expressed as positive or negative values.
  • An Integer or int is a whole number such as 1, 5, -123, 345698, and so on.
  • A Float or Floating point number is a number expressed with a decimal such as 1.3, -33.7, 8.5678906556, and so on.
  • Mathematical Operations are what you might have learned as "signs" as a child when studying basic math. The most commonly used mathematical operators are addition (+), subtraction (-), multiplication (*) and division (/).
Numbers can be added, subtracted, multiplied, and divided. This is unlike the next variable type. Strings: A string is a series of characters that can be of any length, from zero (an empty string) to infinity (though you'd never finish writing an infinite string). Strings aren't just letters such as "dffvsdftgojpgbjdfe" but can be any type of character, including letters, numbers, and symbols. Usually a string is contained between two single or double-quotes to distinguish it from a part of the program. I also mentioned that numbers can be included in a string but a number in a string is not the same as a number in an integer or float. Strings cannot be added, subtracted, multiplied, or divided (except under certain circumstances).
String Operations: I mentioned that strings couldn't be added, subtracted, etc... but that's not entirely true. If 2 + 2 is expressed as integers, the result will be 4, but if "2" + "2" is expressed as strings, the result is "22". Confused? When you add two strings, performing "2" + "2" is no different than performing "a" + "8". The operation doesn't perform mathematical addition but rather concatenation. In this case, "a" + "8" concatenates to "a8".
Arrays: While the variable types presented thus far store individual pieces of information, arrays are designed to store groups of values, such as a list of names or a series of numbers. An array provides an ordered structure for storing a group of values. When my kids were in elementary school, as they entered their classroom from outside, they could store their backpacks in individual cubbyholes or "cubbies" in storage racks. This is the same principle as an array, with each "cubbyhole" representing a "slot" or element in the array. Each element position is represented by an index. Children always start counting with the number one (1) and go up from there. so the first cubby on the left on the top row would be "one", and next cubby to the right would be "two" and so on. In an array, the index of the first element is "zero" (0), the next is "one" (1) and so on. So an element's position in an array is represented by an index, but the element isn't very useful unless it contains a value. (example of how to write an array) So, for example, the element in this array with the index of "one" (1) contains the value "second". Arrays can contain numbers, strings, or a combination as follows:
JavaScript Sample Code:
var cubby = [];
cubby[0] = "First";
cubby[1] = "Second";
cubby[2] = "Third";
cubby[3] = "Fourth";
Python Sample Code:
cubby = ["First", "Second", "Third", Fourth"]
Ruby Sample Code:
cubby = ["First", "Second", "Third", Fourth"]
Associative Arrays: This is just like any other array except that the indexes are switched out for "keys". In a standard array, you need to remember that a particular index, such as 4, contains the value "Sailor Moon backpack". The computer doesn't have a problem with that, but you might. If you, for example, wanted to associate a child's name with a backpack type in an array, you could create that connection in an associative array as in the following:
JavaScript Sample Code:
var backpacks = [];
backpacks["Jamie"] = "Sailor Moon";
backpacks["David"] = "DragonballZ";
backpacks["Michael"] = "Batman";
backpacks["Jim"] = "Jonny Quest";
Python Sample Code:
backpacks = { "Jamie" : "Sailor Moon", "David" : "DragonballZ", "Michael" : "Batman", "Jim" : "Jonny Quest" }
Ruby Sample Code:
backpacks = { :Jamie => 'Sailor Moon', :David => 'DragonballZ', :Michael => 'Batman', :Jim => 'Jonny Quest' }
These are called key-value pairs with, for example "Jamie" being the key (formerly the index) and "Sailor Moon" being the value. So, going back to our elementary school example, we know that Jamie's cubby contains a Sailor Moon backpack (I know I'm dating myself, but it's been awhile since my kids were little). Booleans: Simply put, a boolean is a value that can contain one of two positions: true or false. There are only three operators to work with when using booleans: and, or, and not. Booleans are used to evaluate statements to determine if they're true or false. If true, then one thing happens. If false, then another thing happens. It gets more confusing if one thing happens if A and B are true, and another thing happens if A or B are true. Booleans work more or less the same way in all three of the languages. A common way to use a boolean in any of these languages is to evaluate a loop (we'll get into loops in Part II) in a while statement. While a condition is true, continue going through the loop. When the statement is false, exit the program. For instance, you may have created an array of products your business sells. A customer may be searching your site for products you have in stock. While the customer continues to enter the names of products you have in stock, the search returns a web page for the desired product. When the customer searches for a product name out of stock, the search returns the "not in stock" page. Long, drawn out blogs are a bore to read, even if the content isn't. I'm going to cut the tutorial off here. Part II will continue with the lesson, by explaining conditions, loops, functions, and objects. Stay tuned. Share/Bookmark

Tuesday, November 24, 2009

Review: Goggle Wave so far

I got this in my Gmail inbox the other day:

"Thank you for signing up to give us early feedback on Google Wave. We're happy to give you access to Google Wave and are enlisting your help to improve the product".

Since I'm all about making improvements, I clicked the link to accept my invitation to Google Wave. Once I signed in with my handy dandy Google account (OK, so I'm going a little overboard here), Google Wave opened in my Firefox browser.

I clicked on the first welcome message (there were two) and got this:

Google Wave is more fun when you have others to wave with, so please nominate people you would like to add. Keep in mind that this is a preview so it could be a bit rocky at times.

Invitations will not be sent immediately. We have a lot of stamps to lick.

Happy waving!


Gee. Who would I wave with? I took a look at my contacts and saw that four of them were already on Google Wave. Still, I should read the other welcome message before continuing (linear or anal, I don't know which).

The second message lead me to a YouTube video of "Doctor Wave". Volume was up full blast, so it almost blew my head off. Took me on a nice tour on the GUI, just to orient me (all of us who are using Wave so far). The video is a sparse 2 minutes, 12 seconds, just enough to point out all of the major areas of the UI but not enough to bury the audience in details. Below the playback, is a small group of links that lead to further content.

Now I felt free to check out my Contacts to see if anyone was around I could "wave" with. I clicked on a name and the YouTube video in the wave panel vanished and was replaced by a text editor. I created the message, clicked "Done" and waited. Nothing happened, but then, I didn't know what to expect. Also, all of the nifty links that were below the YouTube video on the wave panel vanished, and I had no idea how I can get them back.

Thought I'd try out the navigation panel and clicked from Inbox to All. More messages became available, including two unread ones; more introductory material. I clicked on the "Welcome to Wave" message again, and the YouTube video and the aforementioned links came back to the wave panel.

I must say, I'm probably the perfect person to try this out and review it. I'm experiencing Wave from a total "end user" perspective, having not read up on it previously, so the whole thing is new. Also, I'm still working on my morning coffee, so I can't promise my brain is as agile as it should be if it were fully caffeinated.

I sent out a cry on twitter for more Wave people and then continued to explore. One of the messages in the "All" folder (already read) listed Extension Settings, which I could uninstall or remove. Basically, these are Google Gadgets, like the ones I used to pimp out my Google home page. I looked around, but the "under construction" message from Google let me know that more Gadgets for Wave weren't available yet, so the pimping would have to be postponed. The noticed "Unfollowed" appeared by the message title. Apparently, you can follow and unfollow specific waves. I found an unfollowed wave that said Profile and decided to "edit" my Google Wave identity.

I was surprised that it wasn't imported from Google proper. My Google av was as I could see it in the Contacts box and by all of my waves, but it wasn't available in my actual profile. Nevertheless, I clicked Edit Profile and see what was what. I inserted the URL to my blog and updated my av photo and that was about it. There was a field for "status" but I have no idea what it's used for. Is it like updating one's "status" in twitter or Facebook?

Just for giggles, I followed the "unfollowed" waves in my All box, then continued to explore Navigation. The rest of the folders were not surprising. By Me is like a "sent" folder, Requests was empty and must contain waves relative to receiving invitation requests. Spam and Trash are self explanatory and Settings held the waves related to the customization of my Google Wave.

No responses to my message that I possess Google Wave invitations yet (has it become that passe' already?). Doctor Wave was right. Without someone to Wave with, this isn't much of a power surge. Of course, if a new message arrives, will I know? My Google page automatically refreshes, and so does Gmail, but I don't know about Wave. I did notice that the number of messages changed in my Inbox from time to time. I don't mean that new messages arrived, but that the messages disappeared and reappeared. Sometimes they all vanished and the all returned. Then, about half of them vanished. Of course, this is a beta, so you can expect the occasional glitch.

While waiting for anyone to respond or to care, I continued to nose around. Under Navigation, you can expand Searches and Folders. Folders just lets you make more you-know-whats and Searches is like searching Gmail, only with a few more options. You can also search your Contacts, though with only four registered in Wave, that's hardly a chore.

Within the Wave window itself, you can click Reply, Playback, Unfollow, Archive, Spam, and Read (which is only active for an unread Wave. It's nice to finally have the option to Unfollow an Gmail/Wave, since an unending conversation in Gmail can sometimes get annoying. Playback?

When you click Playback on a Wave that's not a video or audio file, you get the controls but they affect nothing. I found myself wondering if the Playback button shouldn't also be inactive on all Waves that don't actually play. A minor point, but Google is asking for suggestions.

While waiting, I decided to see if my Google Wave profile was able to work and play well with CredMe. I found out that not only it did, but there were a set of instructions (apparently written by a developer and not me) telling me how to insert the CredMe URL code. I've been playing with CredMe for awhile, so I didn't really need to RTFM. I quickly verified my Google Wave profile with CredMe and was back to the Wave.

Oh, I did get a reply from a friend of mine in the UK. His new job won't let him get online as freely as he could in the past, so I had to wait for the workday to end in that part of the world. Message arrived automatically. When I revisited Wave on a break from my own job, it was just there. Somebody please tell me how this is an "email killer". It's nice, it's smooth, it's slick...it looks like the next generation web-based email interface...but it's email. What makes it "Wave"?

To be fair, I've only been on Wave for a few hours and have only exchanged messages with one person. Probably not a fair test of Wave's abilities. While I'm going to post this article now, I'll be back with updates on Wave as (or if) they develop.

Oh. One more thing. You can reach me on Google Wave at james.pyles AT googlewave.com.

Share/Bookmark

Wednesday, November 18, 2009

Lunch in Boise: Sweetwater's Tropic Zone

I know...I don't usually review restaurants, but it's my blog and I figured, what the heck.

I probably wouldn't have eaten here, but my friend @ryancoates said he'd heard it was good. Actually, I was the one who invited him out, which is probably a surprise to most of you who know me. I don't "get out" much for lunch, but I had something specific I wanted to talk with Ryan about (which is another story...nothing bad, though).

I looked up the address online and according to Google Maps, it's an 18 minute walk from where I work. Beats looking for parking downtown, which I detest, so at about 11:40 a.m., I set out to hoof it to Sweetwater's. I have to say that the smell of barbecue as I walked past The Cottonwood Grille on 9th was just intoxicating, but I pressed on (I love the carbon monoxide smell of charcoal briquettes).

I encountered a few of those new "timed" electric crosswalk signals. Instead of just holding up a blinking red hand to indicate the traffic light is about to change against you, it presents a countdown from about 18 seconds (I think) down to zero, letting you know if you should walk or run or not attempt to cross the street. A minor distraction, but like I said, I don't get out much.

I love downtown Boise. Whenever I have a reason to, I really enjoy the area on foot. At least during a weekday and during daylight, it's kind of peaceful, old fashioned in a brickwork sort of way, and the way every city's downtown should be. If I had the whole afternoon to kill, I'd slow up my pace and just explore, but not today. I was on a mission.

I arrived at the restaurant just a minute or two before twelve and could see Ryan approaching. Our timing was right in synch so neither of us had to wait. The air had a bit of a snap to it when I was walking, but by the time I got to 205 N. 10th, I was warm enough. Ryan and I said our hellos and then followed the directions to enter via the lobby of the entrance next door.

Sweetwater's was very lightly attended, which is good for service but not necessarily good for business. We were seated right away and attended to in every little detail. We were given the lunch menu, plus a beverage and some other menu (I didn't look at it) to review. I'd already selected the Curried Avocado & Jasmine Rice salad from the online menu, and Ryan had some sort of blackened chicken (sorry, I didn't listen very well when he was ordering).

Service was swift, but I probably wouldn't have noticed that much, since Ryan and I were already deep in conversation (64-bit hardware at this point, but moving into Artificial Intelligence). My salad was excellent and a big plus, the portion was just right. I usually either come away from lunch stuffed or starving, but this sat with me just right. Our wait staff (and probably the manager) provided us with a bottle of hot sauce euphemistically called "The Inferno" and I tried a bit on my salad. I guess either the hot sensing part of my tongue died with age, or a lifetime of eating hot foods has made me immune, but I thought it rather tasty.

I only had water to drink, so can't testify to any of the beers they serve. I didn't have to wait, once I put out my credit card, to have it run and have the receipt for signature returned to me (waiting to settle the bill is a pet peeve of mine). Lunch customer activity had only marginally picked up by the time I was ready to pay the bill, and service was still lightning quick. It would have been interesting to have eaten there during a rush to see if this would have been affected at all.

I have to give Sweetwater's Tropic Zone top marks. Food was exotic without being overpowering and service was swift and straightforward. Exactly how I want it when I lunching with a friend and conversation turns to politics (both American and UK), religion, and driving on ice.

Sweetwater's Tropic Zone is located in downtown Boise at 205 N. 10th Street, between Bannock and Idaho. They're open Monday through Saturday, starting at 11:30 a.m. I couldn't find their closing time online. You can get to their website at http://www.sweetwaterstropiczone.com and follow them on twitter at @SweetwatersBOI.

Share/Bookmark

Sunday, November 1, 2009

Linux in a Nutshell, 6th Edition

Authors: Ellen Siever, Stephen Figgins, Robert love and Arnold Robbins
Format: Paperback, 942 pages
Publisher: O'Reilly Media; 6th edition (September 30, 2009)
ISBN-10: 0596154488
ISBN-13: 978-0596154486

I was going to swear that, with each successive edition of this book, the page count got larger and larger, but I checked, and it's stayed almost the same over the last three editions. O'Reilly says that the 4th edition was 944 pages long, but the 5th and 6th editions (the 6th being the latest) are both 942 pages. When I got my review copy in the mail and opened the box, the book seemed larger than I expected for some reason.

But if the book isn't getting larger and thus, filled with more recent and updated information about Linux, why publish subsequent editions? For that matter, can you call a book that's approaching 1000 pages a "Desktop Quick Reference?" To try and answer my first question, I took a look at the back cover and saw, "This updated edition offers a tighter focus on Linux system essentials, as well as more coverage of new capabilities..." Yes, the Linux kernel continues to evolve and thus, what you need to know about Linux system administration continues to change as well. While most of the common shell commands won't change, there are new ones that you'll need to know (and if you've bought this book or are intending to, you belong to that class of person who needs to know). Also, virtualization is huge these days and you can now manage Linux servers via Xen and VMware.

Linux in a Nutshell is considered a classic by anyone's standards, so it's expected to review well. In fact, the prior editions have reviewed extremely well so, in this case, turning in a bad review on the latest edition would mean that the authors and publisher must have completely rewritten the book and done a poor job of it. Fortunately, that's not the case here. Linux in a Nutshell, 6th Edition is a worthy successor to those editions that have come before it. You have three main reasons to buy it. First, you have the reason I've already mentioned; keeping up to date on the latest changes to Linux. Second, you've completely worn out your older copy of the book and need something that isn't hanging in rags. Third, you are new to Linux and need to buy a reference guide for the first time.

In a literary sense, some books are considered classics and almost legends. That may be a bit more rare in technical circles (is there such as thing as the equivalent of A Tale of Two Cites or To Kill a Mockingbird when it comes to Linux?), but it's not entirely unheard of. If the world of Linux has such a book, it's probably Linux in a Nutshell.

All that said, you will be disappointed in this book if you don't know what to expect, and I've read comments from people who were quite disappointed with this book. Hence writing reviews. Here's what not to expect. This is not a tutorial. It will not explain, step-by-step how to use Linux, particularly for the home or office desktop user (and with Ubuntu 9.10 (Karmic Koala) having just been released, a large number of people are thinking of the Linux desktop). If you try to read this book cover-to-cover, you won't find it amusing or entertaining. It would be like trying to read an encyclopedia set or dictionary from A to Z. Yes, it would be very informative, but organized in a very rigid manner, and it will not read like a narrative.

This book is a reference in the same manner as the aforementioned encyclopedia and dictionary. You look up only what you need to know. It presupposes that the reader have some familiarity with Linux, at least enough to understand what information they require and how to use the book to find it. The list of shell commands runs from page 33 to page 503, for instance and is in alphabetical order. On the other hand, chapters such as The Bash Shell do contain narrative and explanatory components, so you do get more than raw shell commands and arguments. Still, it is not a good book as your first exposure to Linux documentation or as an introduction to topics like the bash shell, the vi text editor, or the gawk programming language. It would be like trying to learn English by reading a dictionary. A dictionary is better utilized once you know at least the basics of the language and want to pick up something specific.

Both Git and Subversion version control system commands are well covered in the later chapters, but as I said, you'd better know the basics first, rather than expect to learn them for the first time here. As promised, Virtualization Command-Line Tools sits in the last chapter of the book, giving you fingertip access to the commands and options involving KVM, VMware, and Xen virtualization tools.

This book's 6th Edition is indeed a worthy inheritor to those that have come before it and carries on the tradition of providing A Desktop Quick Reference for Linux shell commands and utilities. If you've owned a prior edition, this update is now available as a replacement. If you are learning Linux administration for the first time and have the basics down, you're ready to buy this edition as your first experience to Linux in a Nutshell.

Share/Bookmark