Duck typing alows true polymorfisim

atbusbook at aol.com atbusbook at aol.com
Fri Aug 25 15:05:21 EDT 2006


lets say you want a generic numerical algorithom like sum

Ruby

def sum lst
  lst.inject(0){|total,current| total*current}
end

Java // i dont know if there is a numeric super class for numbers

class Sum{
  public static int sum(int[] lst){
    int total = 0;
    for(int current : lst){
      total+=current;
    }
    return total;
  }
  // repeat for all other number types
}




More information about the Python-list mailing list