Skip to content

Instantly share code, notes, and snippets.

View mbuczko's full-sized avatar

Michał Buczko mbuczko

View GitHub Profile
@mbuczko
mbuczko / .ctags
Last active June 26, 2019 15:31
ctags configuration
--langdef=clojure
--langmap=clojure:.clj.cljs.cljx
--regex-clojure=/\(fact[ \t]+\"([^\"]+)\"/\1/,fact/
--regex-clojure=/\(create-ns[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/n,namespace/
--regex-clojure=/\(def[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/d,definition/
--regex-clojure=/\(defn[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/f,function/
--regex-clojure=/\(defn-[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/p,private function/
--regex-clojure=/\(defmacro[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/m,macro/
--regex-clojure=/\(defroutes[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/r,routes/
--regex-clojure=/\(defresource[ \t]+([-[:alnum:]*+!_:\/.?]+)/\1/o,resource/
@mbuczko
mbuczko / equals.clj
Created June 28, 2019 10:34
clojure / equal on type
(deftype Employee [name id]
Object
(equals [a b] (= (.id a) (.id b)))
(hashCode [this] (.hashCode (.id this)))
(toString [this] (.name this)))
(def vince (Employee. "Vince" 42))
(def vincent (Employee. "Vincent" 42))
(defmacro assert-all
[& pairs]
`(do (when-not ~(first pairs)
(throw (IllegalArgumentException.
(str (first ~'&form) " requires " ~(second pairs) " in " ~'*ns* ":" (:line (meta ~'&form))))))
~(let [more (nnext pairs)]
(when more
(list* `assert-all more)))))
(defmacro when-let*
@mbuczko
mbuczko / validation.clj
Last active June 28, 2019 20:30 — forked from ewilazarus/validation.clj
clojure / validation
(ns sagaz-back.validation)
; Helper functions
(defn validate* [field value regexp]
(if-not (re-matches regexp value)
(keyword (str "invalid-" (name field)))))
(defn validate [data criteria]
(-> (reduce-kv (fn [acc k re] (conj acc (validate* k (k data) re)))
@mbuczko
mbuczko / blur.clj
Last active June 28, 2019 20:31
clojure / OpenCV scale image preserving ratio
(import '[org.opencv.core Mat Size CvType] '[org.opencv.highgui Highgui] '[org.opencv.imgproc Imgproc])
(clojure.lang.RT/loadLibrary org.opencv.core.Core/NATIVE_LIBRARY_NAME)
(def angelina (Highgui/imread "resources/angelina.jpg"))
(def blurred (Mat. 960 1280 CvType/CV_8UC3))
(Imgproc/GaussianBlur angelina blurred (Size. 5 5) 3 3)
(Highgui/imwrite "resources/images/blurred.png" blurred)
@mbuczko
mbuczko / rest.org
Created June 28, 2019 21:34
emacs / restclient + org-mode

rest

github api

:query-repos := <<
(graphql-query
 ((viewer
   (repositories
    :arguments
    ((first . 3)
@mbuczko
mbuczko / keybase.md
Created March 15, 2016 20:19
keybase / proof

Keybase proof

I hereby claim:

  • I am mbuczko on github.
  • I am mbuczko (https://keybase.io/mbuczko) on keybase.
  • I have a public key whose fingerprint is 0639 2B57 8F68 B2F8 7CE9 861E EA1C F2EE 7389 EE0E

To claim this, I am signing this object:

@mbuczko
mbuczko / mock.clj
Last active October 22, 2019 12:12
clojure / make-mock
(defmacro with-mock [vr & body]
`(with-redefs [~vr (with-meta
(fn [& ~'args] (swap! (-> ~vr meta :args) conj ~'args))
{:args (atom [])})]
(do ~@body)))
(defmacro make-mock
([m] `(make-mock ~m nil))
([m stub]
@mbuczko
mbuczko / pathom.clj
Last active October 22, 2019 12:15
clojure / pathom + timeouts
(pc/defresolver res-with-timeout [_ _]
{::pc/output [:foo]}
(async/go
(let [timeout-ch (async/timeout 3000)
[ch res] (async/alts!! [(async/go
(do-my-operation-here))
timeout-ch] :priority true)]
(if (= ch timeout-ch)
(throw (ex-info "Resolver timeout" {:timeout 3000}))
res))))
@mbuczko
mbuczko / visit-github-issue.el
Created November 8, 2019 14:15
emacs / visit github issue or PR
;; requires git-link package
(defun github--goto-issue-or-pr (id type)
"Opens a browser with issue or PR (denoted by TYPE) of given ID."
(let* ((origin-url (car (git-link--exec "config" "--get" "remote.origin.url")))
(repo-match (string-match "^git@github.com:\\([^\\.]+\\)" origin-url))
(repo-url (concat "https://github.com/" (match-string 1 origin-url)))
(sub-path (cond ((eq 'issue type) "/issues")
((eq 'pr type) "/pull"))))