Skip to content

Instantly share code, notes, and snippets.

>>> if True:
... for i in [1, 2]:
... print(i)
... else:
... print("else")
...
1
2
else
>>> def foobar():
@patrickgombert
patrickgombert / clj.py
Last active January 12, 2017 18:37
concepts from clj in py
# lazyseq.py
class LazySeq(object):
def __init__(self, f):
self.f = f
self.seq = []
def next(self):
res = self.f(self.seq)
self.seq.append(res)
user=> (#{} 1 "not found")
ArityException Wrong number of args (2) passed to: PersistentHashSet clojure.lang.AFn.throwArity (AFn.java:429)
user=> ((transient #{}) 1 "not found")
"not found"
@patrickgombert
patrickgombert / deftype.clj
Last active October 20, 2015 17:45
deftype with class names as vars and macroexpansion
(defn- symbol->Class [resolved-symbol original-symbol]
(if (class? resolved-symbol)
(-> (str resolved-symbol)
(split #"\.")
last
symbol)
original-symbol))
(defn- do-resolve [sym]
(let [r (resolve sym)]
(defprotocol Log
(info [args]))
; We wanted to do this
(deftype TaggedLogger [metadata]
Log
(info [args] (apply clojure.tools.logging/info (conj metadata args))))
; but info et al. are macros so we can't use apply here
; Then I wrote this macro in order to wrangle the problem
;The following fails, it seems to be related to using try..finally and returning this
(defprotocol Foo
(-bar [this]))
(deftype Baz [^:volatile-mutable -oof]
Foo
(-bar [this]
(try
(set! -oof "something")
def coords({x1, y1}, {x2, y2}) when x1 == x2 or y1 == y2 or abs(x1 - x2) == abs(y1 - y2) do
do_coords({x1, y1}, {x2, y2}, [])
end
def coords(_, _), do: raise "invalid"
defp do_coords({fromx, fromy}, {tox, toy}, acc) when fromx == tox and fromy == toy, do: tl(acc)
defp do_coords({fromx, fromy}, {tox, toy}, acc) do
newx = gen_next(fromx, tox)
@patrickgombert
patrickgombert / gist:bc6bf7e4dfc702b888a4
Created December 18, 2014 17:56
Trim trailing whitespace
" Trip trailing whitespace
function! Trim()
exe "normal mz"
%s/\s*$//
exe "normal `z"
exe "normal zz"
endfunction
command! -nargs=0 Trim :call Trim()
nnoremap <silent> <Leader>tw :Trim<CR>
@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
@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)