Skip to content

Instantly share code, notes, and snippets.

@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 / 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
public static final class Ratio extends Number {
/* .. */
}
public static final class Addition {
public static Number add(Integer x, Integer y) {
/* .. */
}
@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:") #\
(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))))
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)
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
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, _, _}) ->
@patrickgombert
patrickgombert / gist:4f4e3b51ea6ee81e76b6
Last active August 29, 2015 14:09
Find difference in public vars
(let [next-public (ns-publics 'clojure.next)
core-public (ns-publics 'clojure.core)
[compl not-compl] (let [s (set (keys next-public))]
(loop [ks (keys core-public)
compl '()
not-compl '()]
(if (empty? ks)
[compl not-compl]
(if (contains? s (first ks))
(recur (rest ks) (conj compl (first ks)) not-compl)
@patrickgombert
patrickgombert / gist:278b9ea067ebf772683a
Last active August 29, 2015 14:10
allow def'd classes to be used in try...catch
; try is a special form that seems to not allow def'd classes to be used
; for example:
;
; (def illegal-argument IllegalArgumentException)
;
; (try (catch illegal-argument _))
;
; will raise the following exception:
; Unable to resolve classname: illegal-argument