/ ruby
Check out the live demo at http://sinitter.moocode.com/
Deleting Duplicates with Unique Keys in MySQL with Ruby
Trying to delete duplicate data is a pain. Especially when your table is bigger than your RAM available so you cannot just create a temp table can copy the unique rows back and forth.
So I’ve made available some code in this gist which will delete duplicate data, in the case where the ids are unique but the data is duplicated.
# USE: delete_dups_for(Comment, :post_id)
NB: code is at the bottom, the first 232 lines are the Ruby Progress Bar lib
I have the power
Ruby:
square = lambda {|x| puts x ** 2}
power = lambda {|x, y| puts x ** y}
square.call(2) # => 4
power.call(2, 4) # => 16
Erlang:
-module(power).
-compile(export_all).
sqaure(X) -> X * X.
pow(X, 0) -> 1;
pow(X, Y) -> X * pow(X, Y-1).
geometric_pow(_, 0, Acc) -> Acc;
% It’s interesting that if 1 and 2 aren’t “unrolled”,
% the linear implementation beats geometric_pow.
geometric_pow(Square, 1, Acc) -> Square*Acc;
geometric_pow(Square, 2, Acc) -> Square*Square*Acc;
geometric_pow(Square, 3, Acc) -> Square*Square*Square*Acc;
geometric_pow(Square, 4, Acc) -> Square*Square*Square*Square*Acc;
geometric_pow(Square, Exp, Acc) ->
geometric_pow(Square*Square, Exp div 2, ((Exp rem 2)*(Square-1)*Acc + Acc)).
geometric_pow(Base, Exponent) -> geometric_pow(Base,Exponent,1).
power:geometric_pow(2, 2). and power:pow(2,2) return the same, geometric_pow however is recursive.
Animals
In Ruby:
class Animal
def self.speak
"Grunt"
end
end
class Cat < Animal
def self.speak
"Meeaow"
end
end
class Dog < Animal
def self.speak
"Ruuf"
end
end
In Erlang
-module(animals).
-export([speak/1]).
-export([speak/0]).
speak() -> "Grunt".
speak(cat) -> "Meeaow";
speak(dog) -> "Ruuf".
export defines the function and it’s arity.
Ruby:
Animal.speak
Cat.speak
Erlang:
animals:speak.
animals:speak(cat).
