Skip to content

The Evolution of a Programmer

by Iain on February 4th, 2010

The Original Python Version

In some ways, it shows the differences between the methods and reasons of different programmers.
Personally, I like

def fact(x):
    return x > 1 and x * fact(x - 1) or 1
print fact(6)

out of them all. (or would write).
The Lambda version sacrifices clarity in variable names, and understood logic.

The Ruby Version

# The evolution of a Ruby programmer
 
def sum(list)
  total = 0
  for i in 0..list.size-1
    total = total + list[i]
  end
  total
end
 
def sum(list)
  total = 0
  list.each do |item|
    total += item
  end
  total
end
 
def sum(list)
  total = 0
  list.each{|i| total += i}
  total
end
 
def sum(list)
  list.inject(0){|a,b| a+b}
end
 
class Array
  def sum
    inject{|a,b| a+b}
  end
end
 
def sum(list)
  list.reduce(:+)
end

And, of course, there’s always using StackOverflow to find a good technique and build upon it.
For ruby, also, the rubydocs are invaluable for refactoring. Lots of great built-in stuff.

VN:F [1.4.8_745]
Rating: 10.0/10 (1 vote cast)

From → Uncategorized

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS