/ ruby
Friday, April 17
Permalink
Comments (View)
Sunday, April 05
Tuesday, March 31
Tuesday, March 24
Thursday, October 30
Permalink

Deleting Duplicates with Unique Keys in MySQL with Ruby

posted 1 year ago

bambinos:

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


Reblogged from bambinos
Comments (View)
Saturday, April 26
Permalink

I have the power

posted 1 year ago

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.

Comments (View)
Permalink

Animals

posted 1 year ago

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).

Comments (View)
Monday, April 21
Friday, November 09
Permalink
Classy web-development dressed in a DSL
Comments (View)