Archive for the ‘Programming Ducks’ Category

Drop all databases matching prefix

Shell script to drop all MySQL databases with the prefix ‘rupture’:

mysql -u user_name -e 'show databases' |
grep '^rupture' |
xargs -I "@@" echo mysql -u user_name -e '"DROP DATABASE @@"' > temp

chmod +x temp
./temp


Erlang MySQL Stored Procedure Support

github.com/JacobVorreuter/erlang_mysql

If you’re getting the following exception trying to call a stored procedure or prepared statement with the Erlang MySQL library you must apply this patch to mysql_auth.erl inorder to add multi-results and multi-statements to the caps list:

{error,{mysql_result,[],[],0,<<"#0A000PROCEDURE
dbname.procname can't return a result set in the given context">>}}



@@ -27,6 +27,8 @@
 -define(LONG_PASSWORD, 1).
 -define(LONG_FLAG, 4).
 -define(PROTOCOL_41, 512).
+-define(CLIENT_MULTI_STATEMENTS, 65536).
+-define(CLIENT_MULTI_RESULTS, 131072).
 -define(TRANSACTIONS, 8192).
 -define(SECURE_CONNECTION, 32768).
 -define(CONNECT_WITH_DB, 8).
@@ -121,7 +123,8 @@ make_new_auth(User, Password, Database) ->
          ?CONNECT_WITH_DB
        end,
     Caps = ?LONG_PASSWORD bor ?LONG_FLAG bor ?TRANSACTIONS bor
-	?PROTOCOL_41 bor ?SECURE_CONNECTION bor DBCaps,
+	?PROTOCOL_41 bor ?SECURE_CONNECTION bor
+	?CLIENT_MULTI_STATEMENTS bor ?CLIENT_MULTI_RESULTS bor DBCaps,
     Maxsize = ?MAX_PACKET_SIZE,
     UserB = list_to_binary(User),
     PasswordL = size(Password),

Erlang Documentation Shell Command

I spend a lot of time looking things up in the man pages on erlang.org. Here’s how to setup a nice shortcut if you’re a mac user:


$> echo -e '#!/bin/sh\nopen http://erlang.org/doc/man/$1.html\n' > erld
$> chmod +x erld
$> sudo mv erld /usr/local/bin/

Now you can do:

$> erld lists

This runs “open http://erlang.org/doc/man/lists.html”

Enjoy!

Project Euler - Solution 6


-module(problem6).
-export([solution/0]).

solution() -> square_of_sums(100) - sum_of_squares(100).

square_of_sums(Max) -> square_of_sums(1, 0, Max).
square_of_sums(Num, Acc, Max) when Num > Max -> Acc * Acc;
square_of_sums(Num, Acc, Max) -> square_of_sums(Num+1, Acc+Num, Max).

sum_of_squares(Max) -> sum_of_squares(1, 0, Max).
sum_of_squares(Num, Acc, Max) when Num > Max -> Acc;
sum_of_squares(Num, Acc, Max) -> sum_of_squares(Num+1, (Num*Num)+Acc, Max).



Project Euler - Solution 5


-module(problem5).
-export([solution/0]).

-define(Set, [20,19,18,17,16,15,14,13,12,11]).

solution() -> run(1).

run(Count) ->
    case is_divisible(Count, ?Set) of
        true -> Count;
        false -> run(Count+1)
    end.

is_divisible(_, []) -> true;
is_divisible(Num, [First|Tail]) when (Num rem First =:= 0) -> is_divisible(Num, Tail);
is_divisible(_, _) -> false.
 



Project Euler - Solutions 3 & 4

Solution 3:

-module(problem3).
-export([solution/0]).

-define(Target, 600851475143).

%% What is the largest prime factor of the number 600851475143?
solution() -> pfac(1, 1, ?Target, math:sqrt(?Target)).

pfac(Count, Prime, _Target, Cutoff) when Count > Cutoff -> Prime;
pfac(Count, Prime, Target, Cutoff) when Target rem Count =:= 0 ->
    Factor =
        case is_prime(Target div Count) of
            0 ->
                case is_prime(Count) of
                    0 -> Prime;
                    _ -> Count
                end;
            Prime1 -> Prime1
        end,
    pfac(Count+1, Factor, Target, Cutoff);
pfac(Count, Prime, Target, Cutoff) ->
    pfac(Count+1, Prime, Target, Cutoff).

is_prime(Num) -> is_prime(2, Num, math:sqrt(Num)).
is_prime(Count, Target, Cutoff) when Count > Cutoff -> Target;
is_prime(Count, Target, _Cutoff) when Target rem Count =:= 0 -> 0;
is_prime(Count, Target, Cutoff) -> is_prime(Count+1, Target, Cutoff).



Solution 4:

-module(problem4).
-compile(export_all).

-define(Start, 100).
-define(Stop, 1000).

%% Find the largest palindrome made from the product of two 3-digit numbers.
solution() -> outer(?Start, 0).

outer(?Stop, Val) -> Val;
outer(Num1, Val) ->
    outer(Num1+1, inner(Num1, ?Start, Val)).

inner(_, ?Stop, Val) -> Val;
inner(Num1, Num2, Val) ->
    inner(Num1, Num2+1, pal(Num1, Num2, Val)).

pal(Num1, Num2, Val) ->
    Num3 = Num1 * Num2,
    case is_palindrome(Num3) of
        true when (Num3 > Val) -> Num3;
        _ -> Val
    end.

is_palindrome(Num) ->
    List = integer_to_list(Num),
    case lists:reverse(List) of
        List -> true;
        _ -> false
    end.

Project Euler - Solutions 1 & 2

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

Solution 1:

-module(problem1).
-export([solution/0]).

solution() -> sum(0, 3).

sum(Sum, 1000) -> Sum;
sum(Sum, Counter) ->
    Sum1 =
        case ((Counter rem 5) * (Counter rem 3)) of
            0 -> Sum + Counter;
            _ -> Sum
        end,
    sum(Sum1, Counter+1).



Solution 2:

-module(problem2).
-export([solution/0]).

solution() -> fib(1, 2, 0, 4000000).

fib(A, B, Sum, Cutoff) when B < Cutoff ->
    Sum1 =
        case B rem 2 of
            0 -> Sum + B;
            _ -> Sum
        end,
    fib(B, A+B, Sum1, Cutoff);
fib(_, _, Sum, _) -> Sum.



Hope for a Better Wiki: It’s all about the data

It seems as though everyone has taken a shot at making a wiki. Look at the list on wikimatrix.org. There has to be over a hundred on there. My complaint is that, outside of a few attempts at semantic wikis, they all do the same stuff. I hope that everyone agrees that the quota for “corporate wiki solutions” has been met.

Right now, it’s about the data. It’s about pushing it, pulling it, mashing it and spreading it. Information exchange is transforming again as services like Twitter offer new ways to satisfy our hunger for content. OpenID and the data portability movement will provide a set of tools that will allow sites like Twitter to offer better functionality and to get up and running in a fraction of the time it took to actually reach critical mass.

I was just reading Andrew Chen’s post about this process and the reason I believe that data portability will change all of this is as follows: Reaching critical mass is about enough users joining and contributing to your site. The death of many sites arises from users’ hesitation to invest any amount of time defining themselves in a new system. This leads to insufficient data and a bad user experience for everyone. By logging into a site for the first time with your OpenID, you’re bringing the data that you’ve built up on other sites with you (and so is everyone else). This eliminates redundant data and consolidates everything into one network and one social graph.

For example, I joined Twitter as a blank slate. I knew little about it and it knew nothing about me. It was my responsibility to search for and follow users whom I thought would be interesting. It’s not to say that I’m connected to those people on Facebook or MySpace, but I have enough portable preference info on those sites to determine where I fit in Twitter’s social graph. Basically, I want to be able to login to Twitter for the first time and have a tailored list of users suggested to me. Then it becomes very easy for me to adopt the site and decide who I want to follow.

Where do wikis fit in to all of this?

Well, wikis are great as collaborative websites, but the underlying technology of editing content in real-time is bigger than that. I want a web portal into my social graph, my collection of bookmarks and notes, my address book, my conversations and the multimedia that my friends and I have posted around the internet. I want my wiki to be my point of management. I want to use it like a database terminal, to query and manipulate my data.

The key here, is decoupling the on-the-fly editing functionality from the rigid page model. It would be really cool if I could login with my OpenID to some site and use a sleek, elegant wiki syntax to throw together mashups of data from any of my other OpenID sites. For instance, say I want to create a view that grabs all my connections from LinkedIn, retrieves their latest Twitter posts and then spits it out as an RSS feed. I go over to my Google homepage and point it to my new feed and I’m instantly up-to-date on what my peers and coworkers are up to.