Today's News:
30 songs: $30? $4,000? $675,000?
Jury awards RIAA $675,000 from a grad student for DLing and sharing 30 songs. RIAA demanded $4k before trial. Grad student. Now he'll have to scrape up enough to pay a bankruptcy lawyer. RIAA won't get money, but it's got a hell of a legal precedent.
For the record, it took 42 seconds for me to find the file-sharing service my kids use.
Perl
First, Perl is a very kludgy language. The Perl programmer does a lot of work for the benefit of the interpreter. That is backwards. Scalar values' names must start with "$". Array names start with "@". If you have an array, @arr and want its first element you refer to $arr[0].
I assigned a task to myself: learn the three "P"s of the LAMP stack. That's Linux, the Apache server, the MySQL database and one of Perl, PHP or Python. I got far enough in Perl that I did not use it for CGI code. Too kludgy.
Consider this. Arguments are passed in the global variable @_. Suppose your second argument, $_[1], is a reference to a list. The reference is a scalar. The list is not. So you say:$ref = $_[1]; my @list = @$ref;. You now have a list variable, @list.
If that's not bad enough, consider this simpler substitute: my @list = @$_[1];. It looks identical, but it doesn't work. Ugh. I quit.
Regular Expressions
Perl did one thing well: regular expressions. It puts regular expression constants into the core syntax as most languages have string constants. You have "string constant" or /regular expression constant/.
A Perl statement has this format:
[optional statement part] [# optional comment part]
A regular expression to recognize this is /[~#]*#.*/. In English, that's any single character other than "#", repeated zero or more times, followed by "#" followed by zero or more characters of any kind. Add parentheses: /([~#]*)(#.*)/ and you can refer to the parts in your code.
For many text processing chores, regular expressions are so valuable that you forgive their unreadability. The programmer can partially solve this problem with extensive comments. Since Perl, most languages, including Python, feature regex constants delimited by forward slashes. Oddly, Ruby, designed to be a better Perl, does not. It features a regular expression class (JavaScript does this, also) which is somewhat less convenient.
Unfortunately, that sample regular expression won't handle itself as input. It would have /([~ as the statement part and #]*)(#.*)/ as the comment part.
The correct statement definition is:
Zero or more characters, including the "#" character if it is part of a string literal or a regex literal, optionally followed by a "#" character and zero or more comment characters.
There's a neat Perl program on my board that reads Perl source and emits html output. As the comments are output unprocessed, you can use HTML in the comments: lists, tables, images, ...
However, the processing that splits the Perl source into statement and comment does not use Perl's regex. It reads like a C program: got a quote character? Read forward until the matching close quote. Got a forward slash? Set the "in regex" flag. In regex, got a "["? Set the in character class flag. Got a "\"? The next char is escaped. And so it goes.
Challenge:
If you love your Perl and your Tim Toady, meet this challenge: write my "pl2html" program using Perl's regex. If you can get that done, I'll substantially revise this post.
Tuesday, August 11, 2009
Monday, August 10, 2009
The Big Gulp Method
Movie Review:
Julie and Julia
"Most strikingly, this is a Hollywood movie about women that is not about the desperate pursuit of men."
This blogger is NOT a moviegoer. For a movie about Julia Child, I make an exception. Mastering the Art of French Cooking was one of my bibles. Child's shows on PBS were my inspiration. Over the years my cooking has moved south: olive oil has replaced butter, seafood has replaced beef. Still, it all started with Julia.
The Big Gulp Method:
One of the Java-beginner articles on my website suggested that reading a file using Java's streamIO, as was universally suggested in every book I ever saw (my own excluded), was slow, foolish, and too much work for programmer and computer alike.
This was all that you needed:
RandomAccessFile raf = new RandomAccessFile
( "C:/mrwebsite/articles/raf.html", "r" );
bytes = new byte[ (int) raf.length() ];
raf.read( bytes );
raf.close();
In Python, that simplifies to:
file = open( pathname )
arr = file.read()
file.close()
In Python you can also use file.readlines() to populate an array with a text file, one line per array element. This feature is also available in Ruby with near-identical syntax and identical simplicity.
Of course, all of the above is simpler than file I/O in a finished application. Whatever the language, you have to try the file input (does the file exist?), catch errors and provide options for the user to recover from the errors.
Still, the big-gulp method of reading the file into a byte array (or line array, for text) is fast and easy.
Typical Tutorial:
So why do we still see this?
a = open("sample.txt", "r")
line = a.readline()
while line:
print line
line = a.readline()
a.close()
That bit happens to be from about.com, but it's typical of what is given to beginners. Beginners do as they are shown and we are doing file I/O in a way that made sense when RAM size was measured in kilobytes, not gigabytes. And so it goes.
Julie and Julia
"Most strikingly, this is a Hollywood movie about women that is not about the desperate pursuit of men."
This blogger is NOT a moviegoer. For a movie about Julia Child, I make an exception. Mastering the Art of French Cooking was one of my bibles. Child's shows on PBS were my inspiration. Over the years my cooking has moved south: olive oil has replaced butter, seafood has replaced beef. Still, it all started with Julia.
The Big Gulp Method:
One of the Java-beginner articles on my website suggested that reading a file using Java's streamIO, as was universally suggested in every book I ever saw (my own excluded), was slow, foolish, and too much work for programmer and computer alike.
This was all that you needed:
RandomAccessFile raf = new RandomAccessFile
( "C:/mrwebsite/articles/raf.html", "r" );
bytes = new byte[ (int) raf.length() ];
raf.read( bytes );
raf.close();
In Python, that simplifies to:
file = open( pathname )
arr = file.read()
file.close()
In Python you can also use file.readlines() to populate an array with a text file, one line per array element. This feature is also available in Ruby with near-identical syntax and identical simplicity.
Of course, all of the above is simpler than file I/O in a finished application. Whatever the language, you have to try the file input (does the file exist?), catch errors and provide options for the user to recover from the errors.
Still, the big-gulp method of reading the file into a byte array (or line array, for text) is fast and easy.
Typical Tutorial:
So why do we still see this?
a = open("sample.txt", "r")
line = a.readline()
while line:
print line
line = a.readline()
a.close()
That bit happens to be from about.com, but it's typical of what is given to beginners. Beginners do as they are shown and we are doing file I/O in a way that made sense when RAM size was measured in kilobytes, not gigabytes. And so it goes.
Friday, August 7, 2009
JavaScript, Again
Opinion in Fortune:
"... most dismissive report ever produced by a Wall Street analyst" Fortune's Apple-watcher summary of an analysis of Microsoft's plan to launch stores to compete with Apple stores.
JavaScript, Encore:
The function concept and the basic syntax goes back to Fortran, c. 1960. (Born in 1956, Fortran, now structured and object-oriented, is still alive and well where scientists need to do serious number crunching.) In Fortran-descendant C you would write:
function add (num1, num2) {
return num1 + num2;
}
(Actually, it's been a long time since someone wrote an "add" function, but you get the idea.)
That code will run, with only minor changes, through a wide range of compilers and interpreters, including Ruby and JavaScript. Unlike any of the others, here's JavaScript's functional programming alternative:
add = function( num1, num2 ) {
return num1 + num2;
}
With the code in variable "add" you can use "add" like any other variable. For example, you can pass it to another method:
foo = function( bar ) {
return bar(2, 2);
foo( add );
}
"foo" is a function with a parameter named "bar". "bar" is a method that foo will call with the arguments "2" and "2". This is certainly the hard way to get to four, but it has lots of powerful uses. Passing a comparison method to a sort routine, for instance.
This also means that in JavaScript, as in C, you can have an array of functions as easily as an array of strings (and without C's pointer mess). If you've ever needed an array of methods where you can't have one (Java comes to mind) you know how important this is.
Perhaps the best thing about this syntax is its pythonicness. Python itself has this capability but in a messy way with lambda functions. I do hope that the designers of future languages will learn from this innovation. (I doubt it, though. For functional programming you would study Lisp, as Python's designer Guido van Rossum did, and come up with lambda functions.)
"... most dismissive report ever produced by a Wall Street analyst" Fortune's Apple-watcher summary of an analysis of Microsoft's plan to launch stores to compete with Apple stores.
JavaScript, Encore:
The function concept and the basic syntax goes back to Fortran, c. 1960. (Born in 1956, Fortran, now structured and object-oriented, is still alive and well where scientists need to do serious number crunching.) In Fortran-descendant C you would write:
function add (num1, num2) {
return num1 + num2;
}
(Actually, it's been a long time since someone wrote an "add" function, but you get the idea.)
That code will run, with only minor changes, through a wide range of compilers and interpreters, including Ruby and JavaScript. Unlike any of the others, here's JavaScript's functional programming alternative:
add = function( num1, num2 ) {
return num1 + num2;
}
With the code in variable "add" you can use "add" like any other variable. For example, you can pass it to another method:
foo = function( bar ) {
return bar(2, 2);
foo( add );
}
"foo" is a function with a parameter named "bar". "bar" is a method that foo will call with the arguments "2" and "2". This is certainly the hard way to get to four, but it has lots of powerful uses. Passing a comparison method to a sort routine, for instance.
This also means that in JavaScript, as in C, you can have an array of functions as easily as an array of strings (and without C's pointer mess). If you've ever needed an array of methods where you can't have one (Java comes to mind) you know how important this is.
Perhaps the best thing about this syntax is its pythonicness. Python itself has this capability but in a messy way with lambda functions. I do hope that the designers of future languages will learn from this innovation. (I doubt it, though. For functional programming you would study Lisp, as Python's designer Guido van Rossum did, and come up with lambda functions.)
Thursday, August 6, 2009
JavaScript
First, All the News that Fits
NY Times, yesterday: Google’s Android Jumps to the Living Room
Earlier this summer: Microsoft Wants Gadgets to Run Windows
JavaScript
You have people writing JavaScript right now. You did not choose this language. You did not invent the Internet. (Al Gore did, remember?) Brendan Eich, then at Netscape, invented this small scripting language for the then-dominant Netscape browser. If you want to run code within your visitor's browser (something as simple as rotating product pictures) you can do this in any language you choose, provided you choose JavaScript.
Yesterday I was writing JavaScript. SketchUp Rubies, written in Ruby, run their user interface in browser windows. Half or more of a Ruby may be JavaScript, not Ruby.
Actually, I don't write JavaScript. I write the subset that Doug Crockford (Yahoo's JavaScript guru) identifies in his book JavaScript: the Good Parts. JavaScript has other parts, but you can ignore them without losing any functionality. Stick to the good parts. Here's one:
I just created an object named "python". It had two characteristics (data members, if you prefer), "species" and "diet". (No, python sebae is not a vegetarian species. This is my pet python. I don't want him to eat the kitten.) After creating the object, I gave it another attribute, "name". No, there is no class from which this object was instantiated. As yet, python has no methods.
More exactly, python is an Object. Object is the superclass of all objects. As a practical matter, this is important if you are creating a JavaScript interpreter. You can ignore it if you are writing JavaScript.
This simple syntax (I'd call it pythonic!) is the basis for JSON, JavaScript Object Notation. JSON, invented by Doug Crockford, is a simpler alternative to XML for storing and retrieving objects. (For security reasons JavaScript cannot do disk I/O except for cookies. JavaScript plus X is needed for generalized disk I/O. For SketchUp Rubies, X is Ruby and the SketchUp API.)
Where JavaScript gets most pythonic is in its functional programming, which is a thing of beauty. I'll see what I can come up with for that topic tomorrow.
NY Times, yesterday: Google’s Android Jumps to the Living Room
Earlier this summer: Microsoft Wants Gadgets to Run Windows
JavaScript
You have people writing JavaScript right now. You did not choose this language. You did not invent the Internet. (Al Gore did, remember?) Brendan Eich, then at Netscape, invented this small scripting language for the then-dominant Netscape browser. If you want to run code within your visitor's browser (something as simple as rotating product pictures) you can do this in any language you choose, provided you choose JavaScript.
Yesterday I was writing JavaScript. SketchUp Rubies, written in Ruby, run their user interface in browser windows. Half or more of a Ruby may be JavaScript, not Ruby.
Actually, I don't write JavaScript. I write the subset that Doug Crockford (Yahoo's JavaScript guru) identifies in his book JavaScript: the Good Parts. JavaScript has other parts, but you can ignore them without losing any functionality. Stick to the good parts. Here's one:
| python = {species:'python sebae', diet:'vegetarian'}; python.name = 'Monty'; |
I just created an object named "python". It had two characteristics (data members, if you prefer), "species" and "diet". (No, python sebae is not a vegetarian species. This is my pet python. I don't want him to eat the kitten.) After creating the object, I gave it another attribute, "name". No, there is no class from which this object was instantiated. As yet, python has no methods.
More exactly, python is an Object. Object is the superclass of all objects. As a practical matter, this is important if you are creating a JavaScript interpreter. You can ignore it if you are writing JavaScript.
This simple syntax (I'd call it pythonic!) is the basis for JSON, JavaScript Object Notation. JSON, invented by Doug Crockford, is a simpler alternative to XML for storing and retrieving objects. (For security reasons JavaScript cannot do disk I/O except for cookies. JavaScript plus X is needed for generalized disk I/O. For SketchUp Rubies, X is Ruby and the SketchUp API.)
Where JavaScript gets most pythonic is in its functional programming, which is a thing of beauty. I'll see what I can come up with for that topic tomorrow.
Wednesday, August 5, 2009
Not Well-Known for Marketing Prowess
In the News:
Business Week on Google Marketing Apps
Google apps (I've tried word processing and spreadsheet) are good and getting better. Now GOOG wants corporate America to buy premium versions at $50/seat/year. Enter a big billboard campaign backing a 400-person sales army.
Analysis:
8 persons per state. Let's blanket New York. We'll have 4 persons for the NYC metro area, one each in Albany, Binghamton, Buffalo, and Schenectady. We'll spread the metro folks one each in Westchester, Manhattan, the other boroughs and Long Island.
OK, that's not fair. New York will have twice the average, because it's a populous state. So in Manhattan we'll have one person covering finance, another covering advertising.
Stop! Too much sarcasm, Martin.
Reflection:
I've done marketing in Manhattan for a small time-sharing company. We had eight sales people backed by me and a half-dozen engineers.
Oracle, before the Sun acquisition, had just under 20,000 people in sales and marketing. That's an order of magnitude estimate of what you need to market to the world.
In the quarter ended June 30, 2009, MSFT spent 24% of revenue on sales and marketing. In the same quarter GOOG spent 9%. Clearly these are different business models. Let's ask Wall Street what it thinks of these two companies.
GOOG's revenue, latest quarter, was about one third of MSFT's revenue. GOOG's market cap was about two thirds of MSFT's market cap. Wall Street's opinion is that GOOG will grow its earnings a lot faster than MSFT will.
Summary:
So the question is this: should Google add a serious marketing effort to sell apps? If I were Google I would say no. I'd pick just one small target (e.g., drug companies) and look to convert that segment.
There's no real hurry about this. MS Office still dominates a market when the competition, Open Office, is free and superior in many ways.
Every fact in this post was extracted via Google searches. The spreadsheets (all .XLS) that I downloaded along the way were viewed and crunched with OO.
Business Week on Google Marketing Apps
Google apps (I've tried word processing and spreadsheet) are good and getting better. Now GOOG wants corporate America to buy premium versions at $50/seat/year. Enter a big billboard campaign backing a 400-person sales army.
Analysis:
8 persons per state. Let's blanket New York. We'll have 4 persons for the NYC metro area, one each in Albany, Binghamton, Buffalo, and Schenectady. We'll spread the metro folks one each in Westchester, Manhattan, the other boroughs and Long Island.
OK, that's not fair. New York will have twice the average, because it's a populous state. So in Manhattan we'll have one person covering finance, another covering advertising.
Stop! Too much sarcasm, Martin.
Reflection:
I've done marketing in Manhattan for a small time-sharing company. We had eight sales people backed by me and a half-dozen engineers.
Oracle, before the Sun acquisition, had just under 20,000 people in sales and marketing. That's an order of magnitude estimate of what you need to market to the world.
In the quarter ended June 30, 2009, MSFT spent 24% of revenue on sales and marketing. In the same quarter GOOG spent 9%. Clearly these are different business models. Let's ask Wall Street what it thinks of these two companies.
GOOG's revenue, latest quarter, was about one third of MSFT's revenue. GOOG's market cap was about two thirds of MSFT's market cap. Wall Street's opinion is that GOOG will grow its earnings a lot faster than MSFT will.
Summary:
So the question is this: should Google add a serious marketing effort to sell apps? If I were Google I would say no. I'd pick just one small target (e.g., drug companies) and look to convert that segment.
There's no real hurry about this. MS Office still dominates a market when the competition, Open Office, is free and superior in many ways.
Every fact in this post was extracted via Google searches. The spreadsheets (all .XLS) that I downloaded along the way were viewed and crunched with OO.
Tuesday, August 4, 2009
Python on Python
Yesterday my python slithered off, mad because I was writing Ruby. I feared he'd swallow the kitten. Since Python lets you create attributes on the fly, I solved the problem:
Actually, Python has nothing to do with snakes. The name comes from the British comedy troupe, Monty Python. My python is named Monty. Ruby gets its name from lists, such as wedding anniversaries, where Ruby follows Pearl.
When Python and Ruby engineers debate the merits of their respective languages, the discussion is generally learned and respectful. Monty, on the other hand, can really get going.
"Hey, Monty! Want to talk about Tim Toady?" (He never passes this one!)
"There you are. Tell me what you think of Tim."
"Martin, Tim's a fraud. There's maybe sixteen different ways to loop through an array in Ruby. You got some hotshot coder who never does it the same way twice. OK, boss. Who's going to maintain that crap? Some junior coder who just learned Ruby? I don't think so. You're stuck with hotshot. Hope you like paying his six-figure salary."
"Let's show a little real code." Monty dictates a little Ruby and Python.
Python
Ruby
"You could write that loop other ways in Python, but no one would. That's the one, obvious way to do it. Ruby dudes will write every loop differently to show off their Tim Toadiness. And no, I did not forget curly braces in Python. You indent and you get code that looks like, and is, blocked."
"I could go on, Martin, but I just got this sudden urge for some broccoli. Broccoli? I never ate broccoli. What's going on?"
Monty slithers off. Actually, in Ruby looping by the "each" method is the preferred way, in most instances. The simplicity of the Python illustrates the quality that Pythonistas call "pythonicness."
python.diet = 'vegetarian' |
Actually, Python has nothing to do with snakes. The name comes from the British comedy troupe, Monty Python. My python is named Monty. Ruby gets its name from lists, such as wedding anniversaries, where Ruby follows Pearl.
When Python and Ruby engineers debate the merits of their respective languages, the discussion is generally learned and respectful. Monty, on the other hand, can really get going.
"Hey, Monty! Want to talk about Tim Toady?" (He never passes this one!)
"There you are. Tell me what you think of Tim."
"Martin, Tim's a fraud. There's maybe sixteen different ways to loop through an array in Ruby. You got some hotshot coder who never does it the same way twice. OK, boss. Who's going to maintain that crap? Some junior coder who just learned Ruby? I don't think so. You're stuck with hotshot. Hope you like paying his six-figure salary."
"Let's show a little real code." Monty dictates a little Ruby and Python.
Python
| ids=[...] # array, listing division's employee ids for id in ids # code processing # each id |
Ruby
| ids=[...] # array, listing division's employee ids ids.each { |id| # code processing # each id } |
"You could write that loop other ways in Python, but no one would. That's the one, obvious way to do it. Ruby dudes will write every loop differently to show off their Tim Toadiness. And no, I did not forget curly braces in Python. You indent and you get code that looks like, and is, blocked."
"I could go on, Martin, but I just got this sudden urge for some broccoli. Broccoli? I never ate broccoli. What's going on?"
Monty slithers off. Actually, in Ruby looping by the "each" method is the preferred way, in most instances. The simplicity of the Python illustrates the quality that Pythonistas call "pythonicness."
Monday, August 3, 2009
Ruby, Python and Tim Toady
In the News:
Why Bing Is Gaining on Google
A triumph of creative headline writing. Bing soars from 7.2% to 8.2%. Google: 78%. Yo, Steve. When your competitor's name becomes a verb, they've solved the problem. If you don't want to take my word for it, google it.
Python v. Ruby:
Executive summary: two excellent modern, multi-paradigm languages with fiercely partisan fans who never tire of arguing for their personal choice. In the end, the one you prefer probably depends on where you stand on Tim Toady.
First, the basics. Both languages are interpreted, not compiled. Both are more than fast enough for business computing. Neither is strongly typed. Neither is more error prone than, for example, Java.
Both are object-oriented. Almost everything in Python is an object. Everything in Ruby is an object. (In Ruby, "2+2" is a notational convenience for "2.+(2)". That's an object "2" with a method "+" called with another "2" object as argument.)
Ruby's OO constructs are more Java-like. Python's object model struck me at first as chaos. You start with what in C++ or Java would be two instances of the "dog" class, "fido" and "rover" for example. If fido does tricks, you stick an array field to him: "fido.tricks = ["jump", "roll over", ...]. No need to extend any class, no problem that rover has no tricks.
Multi-paradigm means that unlike Java, you use OO coding when it works for you. You use other techniques and you don't force OO solutions on non-OO problems. For example, both support functional programming: the ability to pass code around and manipulate it as easily as other traditional languages handle strings.
Big difference: Rails. Ruby on Rails is the Ruby language with the Rails website framework. The Rails framework gets all the attention of the Ruby community. By contrast, Pythonistas are split among multiple frameworks, none enjoying the effort put into Rails. Rails JavaScript framework, Pythonistas argue, is based on Scriptaculous which is based on Prototype which has an enduring reputation for bad code.
Both languages work. Spectacular websites can be built successfully with either. Both will give you the same results as you could get with Java EE but with much less code, which will simplify maintenance. Would I recommend one or the other? For me, yes I would. For you? I would not. That's because I have a definite stand on Tim Toady.
Tim originated in Perl (which I recommend you avoid if at all possible). TMTOWTDI: There's More Than One Way To Do It. "Tim Toady" is the way it's pronounced. Ruby, which was designed as a better Perl, features Tim Toady. Pythonistas say, "There should be one, obvious way to do it."
I'd say more on this, but my Python just saw the Ruby code on my screen and slithered away looking very mad. I've got to find him before he swallows the kitten. (Why Ruby? SketchUp plugins can be written in the language of your choice, provided you choose Ruby. How can I explain that to my Python?)
Why Bing Is Gaining on Google
A triumph of creative headline writing. Bing soars from 7.2% to 8.2%. Google: 78%. Yo, Steve. When your competitor's name becomes a verb, they've solved the problem. If you don't want to take my word for it, google it.
Python v. Ruby:
Executive summary: two excellent modern, multi-paradigm languages with fiercely partisan fans who never tire of arguing for their personal choice. In the end, the one you prefer probably depends on where you stand on Tim Toady.
First, the basics. Both languages are interpreted, not compiled. Both are more than fast enough for business computing. Neither is strongly typed. Neither is more error prone than, for example, Java.
Both are object-oriented. Almost everything in Python is an object. Everything in Ruby is an object. (In Ruby, "2+2" is a notational convenience for "2.+(2)". That's an object "2" with a method "+" called with another "2" object as argument.)
Ruby's OO constructs are more Java-like. Python's object model struck me at first as chaos. You start with what in C++ or Java would be two instances of the "dog" class, "fido" and "rover" for example. If fido does tricks, you stick an array field to him: "fido.tricks = ["jump", "roll over", ...]. No need to extend any class, no problem that rover has no tricks.
Multi-paradigm means that unlike Java, you use OO coding when it works for you. You use other techniques and you don't force OO solutions on non-OO problems. For example, both support functional programming: the ability to pass code around and manipulate it as easily as other traditional languages handle strings.
Big difference: Rails. Ruby on Rails is the Ruby language with the Rails website framework. The Rails framework gets all the attention of the Ruby community. By contrast, Pythonistas are split among multiple frameworks, none enjoying the effort put into Rails. Rails JavaScript framework, Pythonistas argue, is based on Scriptaculous which is based on Prototype which has an enduring reputation for bad code.
Both languages work. Spectacular websites can be built successfully with either. Both will give you the same results as you could get with Java EE but with much less code, which will simplify maintenance. Would I recommend one or the other? For me, yes I would. For you? I would not. That's because I have a definite stand on Tim Toady.
Tim originated in Perl (which I recommend you avoid if at all possible). TMTOWTDI: There's More Than One Way To Do It. "Tim Toady" is the way it's pronounced. Ruby, which was designed as a better Perl, features Tim Toady. Pythonistas say, "There should be one, obvious way to do it."
I'd say more on this, but my Python just saw the Ruby code on my screen and slithered away looking very mad. I've got to find him before he swallows the kitten. (Why Ruby? SketchUp plugins can be written in the language of your choice, provided you choose Ruby. How can I explain that to my Python?)
Subscribe to:
Posts (Atom)
