This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, _, _}) -> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:") #\ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static final class Ratio extends Number { | |
/* .. */ | |
} | |
public static final class Addition { | |
public static Number add(Integer x, Integer y) { | |
/* .. */ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defns test.ns.exaple | |
(refer-clojure ..) | |
(refer ..) | |
(import ..) | |
.. namespace items ...) | |
; which leads to | |
(def test-suite | |
(quote |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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), |