Skip to content

Instantly share code, notes, and snippets.

defmodule Debug do
defmacro debug_case(ast) do
ast
|> Macro.postwalk(fn
{:->, ctx, [[clause], expr]} ->
debug = quote do
IO.puts("Hit clause: #{unquote(Macro.to_string(clause))}")
end
{:->, ctx, [[clause], {:__block__, [], [debug, expr]}]}
@sionide21
sionide21 / counter.ex
Created February 10, 2018 16:27
Counter CRDT
defmodule Counter do
use GenServer
@announce_interval 5_000
def start_link do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end
def increment() do
GenServer.cast(__MODULE__, :increment)
FROM elixir:1.5.1
RUN mkdir /app
WORKDIR /app
EXPOSE 7777
ENV PORT=7777
ENV MIX_ENV=prod
RUN mix local.hex --force && mix local.rebar --force

Install Erlang Dependencies

Note: This assumes you are using GTK, if not, choose a different version of the libwx or you'll end up installing all of GTK. If you aren't using a window manager, you don't need WX at all, but you'll miss out on some cool built in GUIs.

sudo apt-get install build-essential libncurses5-dev libssl-dev libwxgtk3.0-dev

Download and compile Erlang

defmodule Polish do
defmacro reverse_polish_notation(code) do
code
|> do_reverse_polish()
|> List.flatten()
|> Enum.join(" ")
end
defmacro sigil_p({:<<>>, _, [code]}, '') do
build_code(code)
SELECT idstat.relname AS table_name,
indexrelname AS index_name,
CASE WHEN indexdef ~* 'unique' THEN 'UNIQUE' END,
pg_size_pretty(pg_relation_size(indexrelname::text)) AS index_size,
idstat.idx_scan AS times_used,
n_tup_upd + n_tup_ins + n_tup_del as num_writes,
indexdef
FROM pg_stat_user_indexes AS idstat JOIN pg_indexes ON indexrelname = indexname
JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname
ORDER BY num_writes desc, times_used ASC;
@sionide21
sionide21 / 0-problem.md
Last active July 20, 2016 13:09
Weird socket behavior

For some reason, I can't seem to serve more than a bit under 2^14 tcp requests without elixer hanging.

This happens consistently in multiple projects I have set up. Below is a boiled down version that exhibits the behavior.

@sionide21
sionide21 / sqrtSums.hs
Created December 6, 2013 13:52
sqrtSums | sum of the square root of the first natural numbers sqrtSums < x
sqrtSums :: Double -> Int
sqrtSums x = length (takeWhile (<x) (scanl1 (+) (map sqrt [1..]))) + 1
@sionide21
sionide21 / gist:7807702
Last active December 30, 2015 09:09
This `ls` took over 3 hours to complete. I have too many files.
$ time ls -lh > ~/files.txt
real 186m14.030s
user 0m9.361s
sys 43m37.939s
ve() {
local curdir ve
curdir="$PWD"
[ -z "$1" ] && ve="ve" || ve="$1"
while ! ls -d "$ve" >/dev/null 2>&1; do
if [ "$PWD" == "/" ]; then
echo "Cannot find a '$ve' directory on the current path" >&2
cd $curdir
return -1
fi