Skip to content

Instantly share code, notes, and snippets.

View jlouis's full-sized avatar

Jesper Louis Andersen jlouis

View GitHub Profile
@jlouis
jlouis / gist:661272
Created November 3, 2010 16:03
Make a text message into a brainfuck program
%%%-------------------------------------------------------------------
%%% File : bfize.erl
%%% Author : Jesper Louis Andersen <jesper.louis.andersen@gmail.com>
%%% Description : Brainfuckize a text message
%%%
%%% Created : 27 Oct 2010 by Jesper Louis Andersen <jesper.louis.andersen@gmail.com>
%%%-------------------------------------------------------------------
-module(bfize).
%% API, the single export from the module, taking one parameter
-module(moddeps).
-compile(export_all).
write_to(Filename) ->
xref:start(s),
xref:add_directory(s, "ebin"),
{ok, Calls} = xref:q(s, "XC"),
Mods = [{From, To} || {{From,_,_}, {To,_,_}} <- Calls],
@jlouis
jlouis / reltool.config example
Created January 3, 2011 23:07
Stolen from etorrent :)
{sys, [
%%% This says where to look for applications
{lib_dirs, ["../deps", "../apps"]},
%%% Name and version of the release.
%%% The list is the list of applications to include and start when the embedded system is booted
{rel, "etorrent", "1.2.0",
[
kernel,
stdlib,
crypto,
@jlouis
jlouis / sort.erl
Created January 6, 2011 14:55
An alternative to the built-in sort routine
-module(sort).
-export([bu_merge/1]).
-export([bench/0]).
-ifdef(TEST).
-include_lib("eqc/include/eqc.hrl").
-include_lib("eunit/include/eunit.hrl").
-endif.
@jlouis
jlouis / Output example from the error cluster code
Created January 8, 2011 08:20
Here is an example from the cluster code, simple but shows the current situation.
(etorrent@127.0.0.1)2> rerb:analyze().
ok
(etorrent@127.0.0.1)4> rerb:list({clusters, 0.5}).
Cluster:
Size: 89 -- Representatives: [16,4492,32]
Cluster:
Size: 90 -- Representatives: [4438,4570,4464]
ok
(etorrent@127.0.0.1)5> rerb:list({clusters_full, 0.5}).
Cluster:
@jlouis
jlouis / Generating a proplist froma a record
Created January 21, 2011 17:34
Generating a proplist from a record
-module(foo).
-compile(export_all).
-record(foo, {a,b,c}).
foo_to_proplist(#foo{} = F) ->
[{K, element(I, F)} ||
{K, I} <- lists:zip(record_info(fields, foo),
lists:seq(2,record_info(size, foo)))].
@jlouis
jlouis / Puzzle :)
Created January 21, 2011 22:47
Creating a tree of size N in time O(lg N)
-module(foo).
-compile(export_all).
%% Create a binary tree in O(depth)
build(0, T) -> T;
build(K, T) -> build(K-1, {T, T}).
%% What is the size of such a tree
sz(X) when is_atom(X) -> 1;
@jlouis
jlouis / item.erl
Created January 21, 2011 23:28
RPG/MMORPG/MUD Item handling ideas
-module(item).
-export([gen_shield/0]).
-export([transmogrify/2, as_item/1]).
-export([weapon_damage/1, look/1]).
-record(item, {
%% Rather arbitrary identity
id :: term(),
%% Proplist of items properties
master(State = #ms{socket = Socket}) ->
receive
%% Loop cases
{tcp_error, Socket, Reason} ->
io:format("Socket error [~w]: ~s~n", [Socket, Reason]),
master(State);
{announce, Text} ->
send(Socket, "PRIVMSG " ++ State#ms.channel ++ " :" ++ Text),
send(File, State#ms.channel ++ " :" ++ Text),
master(State);
@jlouis
jlouis / z.erl
Created January 23, 2011 23:39
binary_to_integer/1
-module(z).
-compile(export_all).
pow(0, 0) -> 0;
pow(0, _N) -> 1;
pow(K, N) -> N * pow(K-1, N).
binary_to_integer(B) when is_binary(B) ->
{_, N} = binary_to_integer1(B),
N.