Skip to content

Instantly share code, notes, and snippets.

View laheadle's full-sized avatar

Lyn Headley laheadle

View GitHub Profile
@linearregression
linearregression / heaps.erl
Created November 3, 2015 06:06 — forked from larsmans/heaps.erl
Priority queues (pairing heaps) in Erlang
% Copyright (c) 2010-2014, Lars Buitinck
% May be used, redistributed and modified under the terms of the
% GNU Lesser General Public License (LGPL), version 2.1 or later
% Heaps/priority queues in Erlang
% Heaps are data structures that return the entries inserted into them in
% sorted order. This makes them the data structure of choice for implementing
% priority queues, a central element of algorithms such as best-first/A*
% search and Kruskal's minimum-spanning-tree algorithm.
@angrycub
angrycub / GetInfoByModule.erl
Last active February 20, 2018 11:58
Get Info and Status by Module
GetInfoAndStatusByModule = fun (Module) ->
IsProcessInModule = fun (Module, ProcessInfo) ->
case proc_lib:translate_initial_call(ProcessInfo) of
{ICMod, _ICFun, _ICArity} when Module =:= ICMod -> true;
_ -> false
end
end,
ProcessTable = erlang:processes(),
CurriedPredicate = fun (P) -> IsProcessInModule(Module, P) end,
Pids = lists:filter(CurriedPredicate, ProcessTable),
@kaeluka
kaeluka / future.erl
Last active August 29, 2015 14:05
Futures in erlang
-module(future).
-export([async/1, fulfill/2, block/1, test/0]).
async(Fun) ->
F = mk(),
spawn(fun() ->
fulfill(F, Fun()) end),
F.
@shino
shino / gist:5539404
Created May 8, 2013 09:40
Print supervisor tree (snippet to use in shell)
f(C),
C = fun(Id, C2) -> {Id, [case S of
{CId, _, worker, _} -> CId;
{CId, _, supervisor, _} -> C2(CId, C2)
end || S <- supervisor:which_children(Id)]} end.
C(riak_core_sup,C).
@kevsmith
kevsmith / Example
Created December 10, 2012 20:48
Find all processes executing code from a given module
12> F = fun(P, {current_function, {Mod, _, _}}) -> {P, Mod} end,
12> C = fun(Mod, Prefixes) -> lists:sum([string:str(Mod, Prefix) || Prefix <- Prefixes]) > 0 end,
12> Target = fun(Prefixes) -> Candidates = [F(P, erlang:process_info(P, current_function)) || P <- erlang:processes()],
12> [{Pid, Mod} || {Pid, Mod} <- Candidates,
12> C(atom_to_list(Mod), Prefixes)] end.
#Fun<erl_eval.6.82930912>
13> Target(["erl"]).
[{<0.3.0>,erl_prim_loader},{<0.33.0>,erl_eval}]
14>
@roboshoes
roboshoes / touchmouse.js
Created April 13, 2012 10:43
This snippet maps mouse events and touch events onto one single event. This makes it easier in the code since you have to listen to only one event regardles whether it's desktop or mobile.
(function() {
/* == GLOBAL DECLERATIONS == */
TouchMouseEvent = {
DOWN: "touchmousedown",
UP: "touchmouseup",
MOVE: "touchmousemove"
}
/* == EVENT LISTENERS == */
@cooldaemon
cooldaemon / keep_state.erl
Created April 7, 2009 16:00
Restore state on supervisor restart.
-module(keep_state).
-author('cooldaemon@gmail.com').
-export([test/0]).
test() ->
keep_state_sup:start_link(),
{} = keep_state_server:get_state(),
keep_state_server:put_state(foo),
foo = keep_state_server:get_state(),
@ngerakines
ngerakines / memoize.erl
Created September 27, 2008 17:55
A small module to provide memoize functions in Erlang.
%% Copyright (c) 2008 Nick Gerakines <nick@gerakines.net>
%%
%% 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: