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 / gist.rb
Created December 11, 2015 22:35
Enumerable Weirdness
class Test
include Enumerable
def each
yield "Hello"
yield "World"
end
end
enum = Test.new
array = ["Hello", "World"]
@sionide21
sionide21 / railsconf_schedule.rb
Last active August 29, 2015 14:19
Make the Schedule Items at http://railsconf.com/schedule clickable to add to your Google Calendar
$("<script/>", {type: "text/javascript", src: "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.min.js"}).appendTo(document.body);
$(".session-content a").on("click", function() { event.stopPropagation(); });
$(".session-content").on("click", function() {
var session = $(this),
title = session.find(".session-title"),
details = title.find("a")[0].href,
speaker = session.find(".speaker-name"),
room = $("th").eq(session.parent().index()),
text = function(el) { return el.text().trim(); },
@sionide21
sionide21 / outlook_proxy.rb
Created December 15, 2014 20:55
A proxy to the office365 IMAP server to figure out what a client tis up to.
require 'openssl'
require 'socket'
Thread.abort_on_exception = true
server = TCPServer.new(1993)
n = 0
loop do
client = server.accept
n += 1