Skip to content

Instantly share code, notes, and snippets.

View ngerakines's full-sized avatar
🏠
Working from home

Nick Gerakines ngerakines

🏠
Working from home
View GitHub Profile
@ngerakines
ngerakines / clear.erl
Created July 23, 2008 18:37
A small erlang snippet to clear ipwcore cache files.
Now = calendar:datetime_to_gregorian_seconds(erlang:universaltime()) - 14400,
DelFun = fun(File, {ok, [{{time, Time}, _}]}) when Time < Now -> file:delete(File); (_, _) -> ok end,
[DelFun(File, file:consult(File)) || File <- filelib:wildcard("cache/*")].
<style>
div.row {
clear: both;
padding-top: 10px;
}
div.row span.label {
float: left;
width: 100px;
text-align: right;
Str = "2008-10-01 10:00:01",
[YY, MM, DD, Hh, Mm, Ss] = [list_to_integer(X) || X <- string:tokens(Str, " -:")],
{{YY, MM, DD}, {Hh, Mm, Ss}}.
Eshell V5.6.2 (abort with ^G)
1> c(sleeperl).
{ok,sleeperl}
2> sleeperl:testb().
Receiver spawned at {1219,775147,552727}
Spawn start at {1219,775147,552864}
Spawn done at {1219,775147,553079}
ok
3>
receiver got '{done,<0.38.0>}' at {1219,775150,571689}
@ngerakines
ngerakines / memoize.erl
Created September 27, 2008 17:55
A small module to provide memoize functions in Erlang.
%% Copyright (c) 2008 Nick Gerakines <[email protected]>
%%
%% Permission is hereby granted, free of charge, to any person
%% obtaining a copy of this software and associated documentation
%% files (the "Software"), to deal in the Software without
%% restriction, including without limitation the rights to use,
%% copy, modify, merge, publish, distribute, sublicense, and/or sell
%% copies of the Software, and to permit persons to whom the
%% Software is furnished to do so, subject to the following
%% conditions:
@ngerakines
ngerakines / lrulist.erl
Created October 5, 2008 00:52
A small LRU list based on gb_trees.
%% @doc A small LRU list container.
%% @todo Add better documentation.
%% @todo Find edge case bugs in purging.
-module(lrulist).
-author("Nick Gerakines <[email protected]>").
-export([new/0, new/1, get/2, insert/3, remove/2, purge/1, keys/1]).
-define(EXPIRE_RULES, [expire, slidingexpire]).
@ngerakines
ngerakines / binlog.erl
Created October 17, 2008 19:37
Determine the next binlog file for a given binlog filename.
-module(binlog).
-compile(export_all).
current_log() -> "db-000004.bin".
%% binlog:next_log(binlog:current_log()).
%% binlog:next_log("db-000004.bin").
next_log("db-" ++ TmpA) ->
"nib." ++ TmpB = lists:reverse(TmpA),
TmpC = list_to_integer(lists:reverse(TmpB)) + 1,
Backpack = {
setUser:function(val){
Application.prefs.setValue("backpack_user", val);
},
setDomain:function(val){
Application.prefs.setValue("backpack_domain", val);
},
setToken:function(val){
Application.prefs.setValue("backpack_token", val);
@ngerakines
ngerakines / bottles.erl
Created December 8, 2008 04:49
A simple erlang server demo.
-module(bottles).
-export([start/0, start/1, server_loop/1, take/0, info/0, stop/0]).
start() ->
start(99).
start(State) ->
Pid = spawn(bottles, server_loop, [State]),
pg2:create(bottles_server),
pg2:join(bottles_server, Pid),
@ngerakines
ngerakines / mtdump.erl
Created December 20, 2008 10:23
A module used to dump MT blog entries to Jekyll files.
-module (mtdump).
-export([start/0]).
start() ->
mysql:start_link(mt, "localhost", "user", "password", "mt_database"),
mysql:prepare(posts, <<"SELECT entry_id, entry_basename, entry_text, entry_text_more, entry_created_on, entry_title FROM mt_entry where entry_blog_id = 3">>),
Posts = case mysql:execute(mt, posts, [], infinity) of
{data, MySQLRes} ->
Fields = [ F || {_, F, _, _} <- mysql:get_result_field_info(MySQLRes)],
Rows = mysql:get_result_rows(MySQLRes),