Skip to content

Instantly share code, notes, and snippets.

defmacro defguard({name, _, args}, [do: body]) do
quote bind_quoted: [args: args, body: body, name: name] do
defmacro unquote(name)(unquote_splicing(args)) do
if Macro.Env.in_guard?(__CALLER__) do
quote do
unquote(body)
end
else
eager_bindings = Enum.map args, fn({name, _, _}) ->
iex(1)> a = << 0 :: size(10) >>
<<0, 0::size(2)>>
iex(2)> b = << 0 :: size(10) >>
<<0, 0::size(2)>>
iex(3)> << a :: size(10), b :: size(10) >>
** (ArgumentError) argument error
iex(3)> << a :: binary, b :: binary >>
** (ArgumentError) argument error
iex(3)> a <> b
** (ArgumentError) argument error
defmodule Curried do
defmacro defc({name, _, args}, [do: body]) do
curried_args = Enum.map(Enum.with_index(args), fn({_, index}) ->
Enum.take(args, index + 1)
end)
for a <- curried_args do
if a == Enum.at(curried_args, Enum.count(curried_args) - 1) do
quote do
def unquote(name)(unquote_splicing(a)) do
unquote(body)
(defmacro with-defaults [defaults definition]
(let [function-name (first definition)
arguments (second definition)
argument-subset (vec (nthrest arguments (count defaults)))
body (rest (rest definition))]
`(defn ~function-name
(~argument-subset
(~function-name ~@(reverse (concat argument-subset defaults))))
(~arguments
~@definition))))
@patrickgombert
patrickgombert / gist:9443008
Last active August 29, 2015 13:57
cat Gemfile.lock | sed -n '/specs\:/,/^$/p' | sed 1d | awk '{print $1}' | sort | uniq | xargs gem contents | wc -l
GFILE = "Gemfile.lock" #\
list = File.read(GFILE) #\
#\
gems = [] #> cat Gemfile.lock |
#/
list.each_line do |line| #/
line = line.strip #/
break if line.include?("PLATFORMS") #\
next if line.include?("GEM") #\
next if line.include?("remote:") #\
public static final class Ratio extends Number {
/* .. */
}
public static final class Addition {
public static Number add(Integer x, Integer y) {
/* .. */
}
@patrickgombert
patrickgombert / ideal namespaces
Created January 12, 2014 18:46
ideal namespaces
(defns test.ns.exaple
(refer-clojure ..)
(refer ..)
(import ..)
.. namespace items ...)
; which leads to
(def test-suite
(quote
@patrickgombert
patrickgombert / gist:8260490
Created January 4, 2014 20:45
Test atomicity
(defn swap-items-in [atm v1 v2 i1 i2]
(swap! atm
(fn [integer-vecs]
(let [first-vec (integer-vecs v1)
second-vec (integer-vecs v2)
first-item (first-vec i1)
second-item (second-vec i2)
next-first-vec (assoc first-vec i1 second-item)
next-second-vec (assoc second-vec i2 first-item)]
(assoc
@patrickgombert
patrickgombert / gist:6393923
Created August 30, 2013 20:23
Dynamo 404 Question
post "/:something" do
conn = conn.resp_content_type("text/plain")
conn = conn.resp_body("This does not exist")
conn = conn.status(404)
conn
end
test "it returns a 404" do
conn = post("/not-real")
@patrickgombert
patrickgombert / gist:6205156
Created August 11, 2013 14:33
Indestructable Erlang REPL
-module(shell).
-export([loop/0, eval_loop/0, eval/2]).
read() ->
io:get_line("> ").
eval(String, Bindings) ->
{ok, Tokens, _} = erl_scan:string(String),
{ok, Parsed} = erl_parse:parse_exprs(Tokens),