Skip to content

Instantly share code, notes, and snippets.

View dergraf's full-sized avatar

Andre Graf dergraf

  • Basel, Switzerland
View GitHub Profile
@dergraf
dergraf / gist:2216802
Created March 27, 2012 15:15
pretty print timestamps in erlang
%% pretty print timestamp from lager/src/lager_utils.erl
localtime_ms() ->
{_, _, Micro} = Now = os:timestamp(),
{Date, {Hours, Minutes, Seconds}} = calendar:now_to_local_time(Now),
{Date, {Hours, Minutes, Seconds, Micro div 1000 rem 1000}}.
format_time() ->
format_time(localtime_ms()).
@dergraf
dergraf / gist:2659414
Created May 11, 2012 12:48
Simple tambur usage
var connection = new tambur.Connection(MY_API_KEY, MY_APP_ID);
connection.ready = function() {
var stream = connection.get_stream("mystream");
stream.onmessage = function(msg) {
/* do something fancy with the msg */
};
};
Tambur = tambur_client:new(MY_API_KEY, MY_APP_ID, SECRET).
Tambur:publish("test_stream", "some message").
-module(dbis_sample).
-export([i_am_a_function/1, pattern/1]).
%% this is a comment
i_am_a_function(Param) ->
Number = 10,
Atom = atom,
String = "string",
EmptyList = [],
List = [Param, Number, Atom, String, EmptyList],
-module(continuation_test).
-export([test/0]).
-define(SIZE, 100).
test() ->
ets:new(test, [named_table]),
[ets:insert(test, {I, I}) || I<- lists:seq(1,?SIZE)],
{Res, Cont} = ets:match_object(test, {'_', '_'}, 10),
L = [p(R) || R <- Res],
c(Cont, L).
@dergraf
dergraf / kdtree.erl
Last active December 19, 2015 23:59
incomplete kdtree implementation in erlang.
-module(kdtree).
-export([new/1, search/2, distance/2, test/0, test/3]).
new([]) -> [];
new(PointList) ->
K = size(lists:nth(1, PointList)),
kdtree(K, PointList, 0).
kdtree(_, [], _) -> [];
kdtree(K, PointList, Depth) ->
@dergraf
dergraf / hilbert.erl
Created July 19, 2013 12:19
space filling hilbert curve that maps two-dimensional space to a one-dimensional space
-module(hilbert).
-export([curve/1, curve/7, point_to_hilbert/3]).
-define(HILBERTMAP, [
{a, [{{0,0},{0,d}}, {{0,1},{1,a}}, {{1,0},{3,b}}, {{1,1},{2,a}}]},
{b, [{{0,0},{2,b}}, {{0,1},{1,b}}, {{1,0},{3,a}}, {{1,1},{0,c}}]},
{c, [{{0,0},{2,c}}, {{0,1},{3,d}}, {{1,0},{1,c}}, {{1,1},{0,b}}]},
{d, [{{0,0},{0,a}}, {{0,1},{3,c}}, {{1,0},{1,d}}, {{1,1},{2,d}}]}
]).
@dergraf
dergraf / gist:b3aa9d495110ff30a010
Created October 10, 2014 13:24
set up a function call in its own process
make_request(Fun, Args) when is_function(Fun), is_list(Args) ->
Ref = make_ref(),
Caller = {self(), Ref},
ReqF = fun() ->
exit({Ref, apply(Fun, [Caller|Args])})
end,
try spawn_monitor(ReqF) of
{_, MRef} ->
receive
Ref ->
@dergraf
dergraf / vernemq_grafana.json
Created October 16, 2015 14:38
VerneMQ Grafana Single Node Dashboard
{
"id": 1,
"title": "VerneMQ",
"originalTitle": "VerneMQ",
"tags": [],
"style": "dark",
"timezone": "browser",
"editable": true,
"hideControls": false,
"sharedCrosshair": false,
@dergraf
dergraf / convert_retain_plugin.erl
Last active October 21, 2015 15:44
A simple VerneMQ auth_on_publish hook to disable retaining messages.
-module(convert_retain_plugin).
-behaviour(auth_on_publish_hook).
-export([auth_on_publish/6]).
auth_on_publish(_UserName, _SubscriberId, _QoS, _Topic, _Payload, _IsRetain) ->
{ok, [{retain, false}]}.