Skip to content

Instantly share code, notes, and snippets.

@arjan
arjan / multi.ex
Last active March 22, 2017 14:20
defmodule Example.Utils.Multi do
@doc """
Intertwine multi calls with callbacks to a function to report progress information.
"""
def add_progress_calls(multi, fun) do
operations = multi.operations |> Enum.with_index()
size = Enum.count(operations)
new_operations =
Enum.reduce(operations, [], fn({op, index}, all) ->
@arjan
arjan / ev3.log
Created November 15, 2016 12:40
Kernel panic while booting Nerves EV3 example
Booting Linux on physical CPU 0x0
Linux version 3.16.7-ckt21-9-ev3dev-ev3 (travis@testing-gce-4a64bf49-6596-4092-8f3f-bbd884cce8e4) (gcc version 4.9.3 (crosstool-NG v0.7.1) ) #1 PREEMPT Wed Sep 21 17:01:00 UTC 2016
CPU: ARM926EJ-S [41069265] revision 5 (ARMv5TEJ), cr=0005317f
CPU: VIVT data cache, VIVT instruction cache
Machine: LEGO MINDSTORMS EV3 Programmable Brick
Memory policy: Data cache writeback
DaVinci da850/omap-l138/am18x variant 0x1
Built 1 zonelists in Zone order, mobility grouping on. Total pages: 16256
Kernel command line: mem=64M console=ttyS1,115200n8 root=/dev/mmcblk0p2 rw rootwait ip=dhcp lpj=747520
PID hash table entries: 256 (order: -2, 1024 bytes)
defmodule TTest do
use ExUnit.Case
defmodule Macro do
defmacro @({name, _, args}) do
IO.puts("defining #{name} #{inspect args}")
end
end
defmodule Foo do
@arjan
arjan / decorator_test.ex
Created October 20, 2016 11:10
Elixir function decorator example
defmodule DecoratorTest do
defmacro decorate() do
Module.put_attribute(__CALLER__.module, :decorated, :true)
end
defmacro def(fn_call_ast, fn_opts_ast \\ nil) do
mod = __CALLER__.module
@arjan
arjan / docker.conf
Created August 12, 2016 15:14
Getting dokku to work on Scaleway servers; put in `/etc/systemd/system/docker.service.d/docker.conf`
[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon -H fd:// --storage-driver=overlay
defmodule Util do
def clean_struct(input = %{__struct__: _}) do
input
|> Map.delete(:__struct__)
|> clean_struct
end
def clean_struct(input = %{}) do
input
|> Enum.map(fn({k, v}) -> {k, &clean_struct/1} end)
|> Enum.into(%{})
@arjan
arjan / webkit.c
Last active May 24, 2016 20:53
Load webkit url from stdin (note: does not compile, meant as example code)
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
#include <string.h>
#define nullptr NULL
GMainLoop* loop;
@arjan
arjan / datetime.ex
Created April 25, 2016 12:58
Serialize Timex.DateTime to JSON automatically
defimpl Poison.Encoder, for: Timex.DateTime do
use Timex
def encode(d, _options) do
fmt = DateFormat.format!(d, "{ISO}")
"\"#{fmt}\""
end
end
@arjan
arjan / genserver_interface.ex
Created February 16, 2016 15:53
GenServer macros
defmodule GenServer.Interface do
defmacro defcall(function, arity) do
args = for n <- :lists.seq(1,arity), do: {"arg" <> Integer.to_string(n) |> String.to_atom, [], Elixir}
quote do
def unquote(function)(server, unquote_splicing(args)) do
GenServer.call(server, {unquote(function), unquote_splicing(args)})
end
end
end
@arjan
arjan / timex_date_json.ex
Created November 6, 2015 01:45
Automatically encode Timex datetimes as ISO in JSON
defimpl Poison.Encoder, for: Timex.DateTime do
use Timex
def encode(d, _options) do
fmt = DateFormat.format!(d, "{ISO}")
"\"#{fmt}\""
end
end