New movie rental service

I'd like to start posting code on my blog so I'm testing out a syntax highlighter plugin. This post will likely go through several iterations as I see what it can do and how to do it.

Here's some ruby:

RUBY:
  1. def addPages(urls)
  2.     return false unless urls
  3.     urls.each{ |u| @queue <<PageFactory.newPage(u) }
  4.     @queue.uniq! # Get rid of dupes.
  5. end

Here's some Perl:

PERL:
  1. my @text = ( 'here', 'are', 'some', 'words' );
  2. my $offset = 0;
  3. print join(' ', map { choppedCase($_) } @text );
  4.  
  5. # Convert a string to "ChOpPeD cAsE"
  6. sub choppedCase
  7. {
  8.     my $word = shift;
  9.     my @chars = split('', $word);
  10.     my $char_count = scalar(@chars);
  11.  
  12.  
  13.     for (my $i = $offset; $i <$char_count; $i+=2)
  14.     {
  15.         $chars[$i] = uc($chars[$i]);
  16.     }
  17.  
  18.     # Track odd length words so we constantly alternate in a string
  19.     if ($char_count % 2)
  20.     {
  21.         $offset = $offset == 0 ? 1 : 0;
  22.     }
  23.     return join('', @chars);
  24. }

Here's some PHP

PHP:
  1. // Pattern to match the directory path that a file is in
  2. $dir_pattern = "@(/.*)/[^/]*$@";
  3.  
  4. // Take a path that's (possibly) relative to the directory that the script is executing in
  5. // and convert it to an absolute path.
  6. function absolutize($path)
  7. {
  8.     global $dir_pattern;
  9.  
  10.     // Only operate on paths that are relative
  11.     if (substr($path, 0, 1) == '.' && preg_match($dir_pattern, $_SERVER['PHP_SELF'], $matches))
  12.         $path = implode('', array($matches[1], '/', $path));
  13.  
  14.     $path_bits = explode('/', $path);
  15.     $elms = array();
  16.     foreach ($path_bits as $bit)
  17.     {
  18.         if ($bit == '' || $bit == '.')
  19.             continue;
  20.         if ($bit == '..') {
  21.             array_pop($elms);
  22.             continue;
  23.         }
  24.         $elms[]= $bit;
  25.     }
  26.     // Push an empty string on top so that we get a leading / when we implode
  27.     array_unshift($elms, '');
  28.     return implode('/', $elms);
  29. }

I just realized that I've coded in all three of these languages this week - plus JavaScript (can't find a good sample to post).

Update
JavaScript

JAVASCRIPT:
  1. // Add commas to a long number to make it more readable.
  2. // Currently only supports whole numbers.
  3. var addCommas = function(aNumber)
  4. {
  5.   // Convert our number to an array and reverse it so that
  6.   // we can easily work from most significant digits down
  7.   // to least significant
  8.   var bitsR = (aNumber.toString().split('')).reverse();
  9.   for (var i = bitsR.length - 1; i>2; i--)
  10.     if (i % 3 == 0)
  11.       bitsR.splice(i, 0, ',');
  12.   // Return our array with commas added back to the proper order
  13.   // for display on the screen.
  14.   return bitsR.reverse().join('');
  15. };

Comments are closed.