Skip to content

Instantly share code, notes, and snippets.

View ferd's full-sized avatar
☢️
This place is not a place of honor… no highly esteemed deed is commemorated here

Fred Hebert ferd

☢️
This place is not a place of honor… no highly esteemed deed is commemorated here
View GitHub Profile
-spec gc_count(non_neg_integer(), binary()) -> non_neg_integer().
gc_count(PreviousCounter, Bin) ->
case byte_size(Bin) of
N when N >= 64 -> % refc binary
Count = N + PreviousCounter
case Count >= ?THRESHOLD of
true ->
erlang:garbage_collect(),
0;
false ->
Originally from: http://erlang.org/pipermail/erlang-questions/2017-August/093170.html
For a safe and fast Erlang SSL server, there's a few
configuration values you might want by default:
[{ciphers, CipherList}, % see below
{honor_cipher_order, true}, % pick the server-defined order of ciphers
{secure_renegotiate, true}, % prevent renegotiation hijacks
{client_renegotiation, false}, % prevent clients DoSing w/ renegs
{versions, ['tlsv1.2', 'tlsv1.1']}, % add tlsv1 if you must
@ferd
ferd / day12.erl
Last active December 12, 2021 16:26
-module(day12).
-export([main/1]).
-mode(compile).
main([File]) ->
{ok, Bin} = file:read_file(File),
Lines = [binary:split(Line, <<"-">>)
|| Line <- binary:split(Bin, <<"\n">>, [global, trim])],
G = lists:foldl(fun([A,B], G) -> add(A,B,add(B,A,G)) end, #{}, Lines),
io:format("part 1: ~p~npart 2: ~p~n",
-module(day10).
-export([main/1]).
-mode(compile).
main([File]) ->
{ok, Bin} = file:read_file(File),
Lines = re:split(Bin, "\n", [multiline, trim, {return, list}]),
io:format("~p~n", [run(Lines)]).
run(Lines) ->
@ferd
ferd / app_deps.erl
Created September 23, 2012 05:33
Quick escript to generate a visualization of app dependencies in Erlang/OTP.
%%% Run with 'escript app_deps.erl'
%%% Change the path in filelib:wildcard/1 as required to capture all
%%% your dependencies.
%%%
%%% Rectangular nodes will represent library apps (no processes involved)
%%% and the circular nodes will represent regular apps. An arrow going from
%%% 'A -> B' means 'A depends on B'.
%%%
%%% This script depends on graphviz being present on the system.
-module(app_deps).
@ferd
ferd / ferd-lite.zsh-theme
Created April 1, 2014 01:12
ZSH theme for light background
PROMPT='$(prompt_clr)λ %{$fg[grey]%}%1~ $(prompt_mode) $(git_prompt_info)$(hg_prompt_info)%{$reset_color%}'
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$fg[yellow]%} → %{$reset_color%}"
function hg_prompt_info {
hg prompt --angle-brackets "\
<%{$fg[blue]%}<branch>%{$reset_color%}> \
<%{$fg[blue]%}<status|modified|unknown>%{$reset_color%} >" 2>/dev/null
}
@ferd
ferd / refc_leak.erl
Created July 18, 2013 12:32
Find Erlang processes that may be leaking refc binaries
f(MostLeaky).
MostLeaky = fun(N) ->
lists:sublist(
lists:usort(
fun({K1,V1},{K2,V2}) -> {V1,K1} =< {V2,K2} end,
[try
{_,Pre} = erlang:process_info(Pid, binary),
erlang:garbage_collect(Pid),
{_,Post} = erlang:process_info(Pid, binary),
{Pid, length(Post)-length(Pre)}
@ferd
ferd / adtech.md
Created June 10, 2019 13:46
AdTech in a non-linear timeline nutshell

Essentially, there's a publisher side (the website displaying the ads), a supply side (the advertisers wanting to display ads), and the client (you). The advertisers might be individual corporations, but they could also be agencies that represent various advertisers or corporations as well.

Traditional advertising was either the publisher or supply side contacting each other to set up a campaign. They could strike a deal like "display my ads on your site for a week for $x" and off you'd go. A contract was signed, you added a banner, and things were fine. You could compare that format to what would be magazines and TV ads: the website was part of a target demographic and you could just buy time as-is. Eventually it evolved when sites got more general (say, news websites or dating websites) where a wide swath of the population could be on the same site. The idea became to target even tighter. The dating websites I used to work on had things like: age, gender, sexual orientation, interests, and so on. We could

@ferd
ferd / percentiles.erl
Created July 16, 2013 13:39
Calculate percentiles in the Erlang shell
f(Percentiles).
Percentiles = fun(Numbers) ->
Percentile = fun(List, Size, Perc) ->
Element = round(Perc * Size),
lists:nth(Element, List)
end,
Len = length(Numbers),
Sorted = lists:sort(Numbers),
[{trunc(Perc*100), Percentile(Sorted, Len, Perc)} ||
Perc <- [0.50, 0.75, 0.90, 0.95, 0.99, 0.999]]
@ferd
ferd / erlang-shell-search-history.patch
Created December 20, 2012 13:59
Add search to Erlang shell's history Search mode can be entered by pressing ctrl-r. Enter terms and press ctrl-r again to search backwards, or ctrl-s to then search forward (if you terminal doesn't eat up that one). Press enter to execute the line, or use tab, arrow keys, or other control sequences (^D, ^K, etc.) to exit search mode while remain…
From d98e6c32d44e00f954c3912b08c6ebf48d55729c Mon Sep 17 00:00:00 2001
From: Fred Hebert <mononcqc@ferd.ca>
Date: Thu, 20 Dec 2012 08:41:33 -0500
Subject: [PATCH] Add search to Erlang shell's history
Search mode can be entered by pressing ctrl-r. Enter terms and press
ctrl-r again to search backwards, or ctrl-s to then search forward (if
you terminal doesn't eat up that one). Press enter to execute the line,
or use tab, arrow keys, or other control sequences (^D, ^K, etc.) to
exit search mode while remaining on the last found line.